hdu 4686 Arc of Dream
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4686
题目描述:
题目上说的很清楚。
解题思路:
就是递推,用快速幂+构造矩阵解决,因为n的取值范围好大,好全。
/* |1 0 0 0 0 | |1 ax*bx 0 0 0 | |0 ax*by ax 0 0 |*|s[n] f[n] a[n] b[n] 1|=|s[n+1] f[n+1] a[n+1] b[n+1] 1| |0 bx*ay 0 bx 0 | |0 by*ay ay by 1 | a[n+1]=a[n]*ax+ay; b[n+1]=b[n]*bx+by; f[n]=a[n]*b[n]; s[n+1]=s[n]+f[n]; */
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <cstring> 8 using namespace std; 9 10 const int maxn = 5; 11 #define LL long long 12 #define mod 1000000007 13 struct mat 14 { 15 LL p[maxn][maxn]; 16 }; 17 18 mat mul (mat a, mat b); 19 mat pow (LL n, mat a, mat b); 20 int main () 21 { 22 LL n, a, ax, ay, b, bx, by; 23 mat A, B; 24 while (scanf ("%lld", &n) != EOF) 25 { 26 memset (A.p, 0, sizeof(A.p)); 27 memset (B.p, 0, sizeof(B.p)); 28 scanf ("%lld %lld %lld %lld %lld %lld", &a, &ax, &ay, &b, &bx, &by); 29 a %= mod, b %= mod, ax %= mod, bx %= mod, ay %= mod, by %= mod; 30 B.p[0][0] = 0;//贴这个代码,我什么也不想说明,只想表明矩阵相乘取余要全面,要细心,(wa哭了) 31 B.p[0][1] = a*b%mod;//心好累,在上面取完模还不够,在这里也要取模 32 A.p[1][1] = ax*bx%mod; 33 A.p[2][1] = ax*by%mod; 34 A.p[3][1] = ay*bx%mod; 35 A.p[4][1] = ay*by%mod; 36 B.p[0][2] = a; 37 B.p[0][3] = b; 38 B.p[0][4] = 1; 39 A.p[0][0] = A.p[1][0] = 1; 40 41 A.p[2][2] = ax; 42 A.p[3][3] = bx; 43 A.p[4][4] = 1; 44 A.p[4][2] = ay; 45 A.p[4][3] = by; 46 B = pow (n, A, B); 47 printf ("%lld\n", B.p[0][0]); 48 } 49 return 0; 50 } 51 52 mat mul (mat a, mat b) 53 { 54 int i, j, k; 55 mat c; 56 memset (c.p, 0, sizeof(c.p)); 57 58 for (i=0; i<5; i++) 59 for (j=0; j<5; j++) 60 { 61 for (k=0; k<5; k++) 62 c.p[i][j] = (c.p[i][j] + a.p[i][k] * b.p[k][j]) % mod; 63 } 64 return c; 65 } 66 mat pow (LL n, mat a, mat b) 67 { 68 while (n) 69 { 70 if (n % 2) 71 { 72 b = mul (b, a); 73 } 74 a = mul (a, a); 75 n /= 2; 76 } 77 return b; 78 }
本文为博主原创文章,未经博主允许不得转载。