思路:
ax+b cx+d
构造矩阵+矩阵快速幂
(需要加各种特判,,,,我好像加少了… )
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
const int mod=1000000007;
int n,m,a,b,c,d;char N[1000500],M[1000500];
struct Matrix{
int a[2][2];
void init(){memset(a,0,sizeof(a));}
friend Matrix operator*(Matrix a,Matrix b){
Matrix c;c.init();
for(int i=0;i<=1;i++)
for(int j=0;j<=1;j++){
for(int k=0;k<=1;k++)
c.a[i][j]=(a.a[i][k]*b.a[k][j]+c.a[i][j])%mod;
}
return c;
}
friend Matrix operator^(Matrix a,int b){
Matrix c;c.a[1][1]=c.a[0][0]=1,c.a[1][0]=c.a[0][1]=0;
for(;b;b>>=1,a=a*a)if(b&1)c=c*a;
return c;
}
}row,line,base,ans;
signed main(){
scanf("%s%s%lld%lld%lld%lld",N,M,&a,&b,&c,&d);
int lenn=strlen(N),lenm=strlen(M);
for(int i=0;i<lenn;i++)n=(n*10+N[i]-'0')%(mod-(a!=1));
for(int i=0;i<lenm;i++)m=(m*10+M[i]-'0')%(mod-(c!=1));
base.a[0][1]=base.a[0][0]=1;
row.a[1][1]=1,row.a[0][0]=a,row.a[1][0]=b;
line.a[1][1]=1,line.a[0][0]=c,line.a[1][0]=d;
row=row^(m-1),line=row*line;
line=line^(n-1),ans=base*line*row;
printf("%lld
",ans.a[0][0]%mod);
}