hdu 4686 Arc of Dream
生活随笔
收集整理的這篇文章主要介紹了
hdu 4686 Arc of Dream
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接:?
http://acm.hdu.edu.cn/showproblem.php?pid=4686
題目描述:
題目上說的很清楚。
解題思路:
就是遞推,用快速冪+構(gòu)造矩陣解決,因?yà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;//貼這個代碼,我什么也不想說明,只想表明矩陣相乘取余要全面,要細(xì)心,(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 }?
轉(zhuǎn)載于:https://www.cnblogs.com/alihenaixiao/p/4376302.html
總結(jié)
以上是生活随笔為你收集整理的hdu 4686 Arc of Dream的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: object-c 1
- 下一篇: 《设计师要懂心理学》-第五章-人如何集中