HDU4686Arc of Dream 矩阵快速幂
An Arc of Dream is a curve defined by following function:
where
a 0 = A0
a i = a i-1*AX+AY
b 0 = B0
b i = b i-1*BX+BY
What is the value of AoD(N) modulo 1,000,000,007?
where
a 0 = A0
a i = a i-1*AX+AY
b 0 = B0
b i = b i-1*BX+BY
What is the value of AoD(N) modulo 1,000,000,007?
InputThere are multiple test cases. Process to the End of File.
Each test case contains 7 nonnegative integers as follows:
N
A0 AX AY
B0 BX BY
N is no more than 10
18, and all the other integers are no more than 2×10
9.OutputFor each test case, output AoD(N) modulo 1,000,000,007.Sample Input
1 1 2 3 4 5 6 2 1 2 3 4 5 6 3 1 2 3 4 5 6
Sample Output
4 134 1902
构造矩阵 过程爆longlong WA 了很多次,很蓝瘦
1 #include <bits/stdc++.h> 2 #define LL long long 3 using namespace std; 4 const int N = 5; 5 int mod = 1e9+7; 6 7 LL k; 8 LL A0,Ax,Ay,B0,Bx,By; 9 10 struct Matrix 11 { 12 LL mat[N][N]; 13 Matrix operator*(const Matrix &b)const 14 { 15 Matrix temp; 16 for(int i = 0; i < N; i++) 17 { 18 for(int j = 0; j < N; j++) 19 { 20 temp.mat[i][j] = 0; 21 for(int k = 0; k < N; k++) 22 { 23 temp.mat[i][j]+=mat[i][k]*b.mat[k][j]%mod; 24 temp.mat[i][j]%=mod; 25 } 26 } 27 } 28 return temp; 29 } 30 }; 31 void Init(Matrix &m) 32 { 33 memset(m.mat,0,sizeof(m.mat)); 34 m.mat[0][0]=Ax*Bx%mod;m.mat[0][1]=Ax*By%mod;m.mat[0][2]=Ay*Bx%mod;m.mat[0][3]=Ay*By%mod; 35 m.mat[1][1]=Ax%mod;m.mat[1][3]=Ay%mod; 36 m.mat[2][2]=Bx%mod;m.mat[2][3]=By%mod; 37 m.mat[3][3]=1; 38 m.mat[4][0]=1;m.mat[4][4]=1; 39 } 40 LL qpow(Matrix &m,LL k) 41 { 42 Matrix ans; 43 memset(ans.mat,0,sizeof(ans.mat)); 44 for(int i = 0; i < N; i++) 45 ans.mat[i][i] = 1; 46 while(k) 47 { 48 if(k&1)ans = ans*m; 49 m = m*m; 50 k>>=1; 51 } 52 LL sum = 0; 53 sum = (sum+ans.mat[4][0] *A0%mod*B0%mod)%mod; 54 sum = (sum+ans.mat[4][1] *A0%mod)%mod; 55 sum = (sum+ans.mat[4][2] *B0%mod)%mod; 56 sum = (sum+ans.mat[4][3]%mod)%mod; 57 //sum = (sum+ans.mat[4][4] *0%mod)%mod; 58 return sum; 59 } 60 int main() 61 { 62 while(cin>>k) 63 { 64 cin>>A0>>Ax>>Ay>>B0>>Bx>>By; 65 //scanf("%d%d%d%d%d%d",&A0,&Ax,&Ay,&B0,&Bx,&By); 66 if(k==0){ 67 printf("0\n"); 68 continue; 69 } 70 Matrix m; 71 Init(m); 72 cout<<qpow(m,k)<<endl;; 73 } 74 return 0; 75 }