HDU 4686 Arc of Dream (2013多校9 1001 题,矩阵)
Arc of Dream
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
![](http://acm.hdu.edu.cn/data/images/C463-1001-1.jpg)
where
a0 = A0
ai = ai-1*AX+AY
b0 = B0
bi = bi-1*BX+BY
What is the value of AoD(N) modulo 1,000,000,007?
Each test case contains 7 nonnegative integers as follows:
N
A0 AX AY
B0 BX BY
N is no more than 1018, and all the other integers are no more than 2×109.
很明显是要构造矩阵,然后用矩阵快速幂求解。
| AX 0 AXBY AXBY 0 |
| 0 BX AYBX AYBX 0 |
{a[i-1] b[i-1] a[i-1]*b[i-1] AoD[i-1] 1}* | 0 0 AXBX AXBX 0 | = {a[i] b[i] a[i]*b[i] AoD[i] 1}
| 0 0 0 1 0 |
| AY BY AYBY AYBY 1 |
然后就可以搞了
注意n==0的时候,输出0
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013/8/20 12:21:51 4 File Name :F:\2013ACM练习\2013多校9\1001.cpp 5 ************************************************ */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 #include <time.h> 19 using namespace std; 20 const int MOD = 1e9+7; 21 struct Matrix 22 { 23 int mat[5][5]; 24 void clear() 25 { 26 memset(mat,0,sizeof(mat)); 27 } 28 void output() 29 { 30 for(int i = 0;i < 5;i++) 31 { 32 for(int j = 0;j < 5;j++) 33 printf("%d ",mat[i][j]); 34 printf("\n"); 35 } 36 } 37 Matrix operator *(const Matrix &b)const 38 { 39 Matrix ret; 40 for(int i = 0;i < 5;i++) 41 for(int j = 0;j < 5;j++) 42 { 43 ret.mat[i][j] = 0; 44 for(int k = 0;k < 5;k++) 45 { 46 long long tmp = (long long)mat[i][k]*b.mat[k][j]%MOD; 47 ret.mat[i][j] = (ret.mat[i][j]+tmp); 48 if(ret.mat[i][j]>MOD) 49 ret.mat[i][j] -= MOD; 50 } 51 } 52 return ret; 53 } 54 }; 55 Matrix pow_M(Matrix a,long long n) 56 { 57 Matrix ret; 58 ret.clear(); 59 for(int i = 0;i < 5;i++) 60 ret.mat[i][i] = 1; 61 Matrix tmp = a; 62 while(n) 63 { 64 if(n&1)ret = ret*tmp; 65 tmp = tmp*tmp; 66 n>>=1; 67 } 68 return ret; 69 } 70 int main() 71 { 72 //freopen("in.txt","r",stdin); 73 //freopen("out.txt","w",stdout); 74 long long n; 75 int A0,AX,AY; 76 int B0,BX,BY; 77 while(scanf("%I64d",&n) == 1) 78 { 79 scanf("%d%d%d",&A0,&AX,&AY); 80 scanf("%d%d%d",&B0,&BX,&BY); 81 if(n == 0) 82 { 83 printf("0\n"); 84 continue; 85 } 86 Matrix a; 87 a.clear(); 88 a.mat[0][0] = AX%MOD; 89 a.mat[0][2] = (long long)AX*BY%MOD; 90 a.mat[1][1] = BX%MOD; 91 a.mat[1][2] = (long long)AY*BX%MOD; 92 a.mat[2][2] = (long long)AX*BX%MOD; 93 a.mat[3][3] = 1; 94 a.mat[4][0] = AY%MOD; 95 a.mat[4][1] = BY%MOD; 96 a.mat[4][2] = (long long)AY*BY%MOD; 97 a.mat[4][4] = 1; 98 a.mat[0][3] = a.mat[0][2]; 99 a.mat[1][3] = a.mat[1][2]; 100 a.mat[2][3] = a.mat[2][2]; 101 a.mat[4][3] = a.mat[4][2]; 102 //a.output(); 103 a = pow_M(a,n-1); 104 //a.output(); 105 long long t1 = (long long)A0*B0%MOD; 106 long long ans = t1*a.mat[2][3]%MOD + t1*a.mat[3][3]%MOD; 107 if(ans > MOD)ans -= MOD; 108 ans += (long long)A0*a.mat[0][3]; 109 ans %= MOD; 110 ans += (long long)B0*a.mat[1][3]; 111 ans %= MOD; 112 ans += (long long)a.mat[4][3]; 113 ans %= MOD; 114 printf("%d\n",(int)ans); 115 } 116 return 0; 117 }