http://codeforces.com/problemset/problem/678/D
矩阵快速幂(需要构造,早出来了,很兴奋)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a,b,n,x;
const int mod=1e9+7;
typedef struct Martix{
ll arr[4][4];
}Mar;
Mar E,st;
void init_(){
memset(E.arr,0,sizeof(E.arr));
memset(st.arr,0,sizeof(st.arr));
for(int i=1;i<=3;i++)
E.arr[i][i]=1;
st.arr[1][1]=st.arr[2][2]=a;
st.arr[1][3]=st.arr[2][3]=b;
st.arr[3][3]=1;
}
Mar mul(Mar a,Mar b){
Mar c;
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
c.arr[i][j]=0;
for(int k=1;k<=3;k++){
c.arr[i][j]+=(a.arr[i][k]*b.arr[k][j])%mod;
c.arr[i][j]%=mod;
}
}
}
return c;
}
Mar bit(ll x,Mar init){
Mar ans=E;
Mar base=init;
while(x){
if(x&1)
ans=mul(ans,base);
base=mul(base,base);
x/=2;
}
return ans;
}
int main(){
scanf("%lld %lld %lld %lld",&a,&b,&n,&x);
ll g0=x%mod,g1=((a*x)%mod+b)%mod;
init_();
if(n<2){
if(n==0)
printf("%lld\n",g0);
else
printf("%lld\n",g1);
}
else{
Mar ans=bit(n,st);
ll ed=((ans.arr[1][1]*g0)%mod+(ans.arr[1][2]*g1)%mod+(ans.arr[1][3])%mod)%mod;
printf("%lld\n",ed);
}
return 0;
}