这两个解题报告比较详细:http://wutyyzchangde.blog.163.com/blog/static/172226566201132311311374/我主要是参考这个解题报告的
http://www.cnblogs.com/zjh10/articles/2035938.html这个可以看看。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 4 typedef __int64 ll; 5 struct node 6 { 7 ll matrix[2][2]; 8 }; 9 10 ll pow_mod(ll a,ll n,ll p) 11 { 12 ll sum = 1; 13 for(;n;n >>= 1) 14 { 15 if(n & 1) 16 { 17 sum = sum * a % p; 18 } 19 a = a * a % p; 20 } 21 return sum; 22 } 23 24 node matrixmul(node a,node b,ll p) 25 { 26 node c; 27 28 for(int i = 0;i < 2;i ++) 29 { 30 for(int j = 0;j < 2;j ++) 31 { 32 c.matrix[i][j] = 0; 33 for(int k = 0;k < 2;k ++) 34 { 35 c.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j] % p; 36 } 37 } 38 } 39 40 return c; 41 } 42 43 ll pow_matrixmul(node a,ll n,ll p) 44 { 45 node sum; 46 sum.matrix[0][0] = sum.matrix[1][1] = 1; 47 sum.matrix[0][1] = sum.matrix[1][0] = 0; 48 49 for(;n;n >>= 1) 50 { 51 if(n & 1) 52 { 53 sum = matrixmul(sum,a,p); 54 } 55 a = matrixmul(a,a,p); 56 } 57 58 return sum.matrix[0][0] % p; 59 } 60 61 int main() 62 { 63 int cas; 64 scanf("%d",&cas); 65 while(cas --) 66 { 67 ll a,b,n,p; 68 scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&p); 69 70 ll ans1 = (pow_mod(a,p / 2,p)+1) % p; 71 ll ans2 = (pow_mod(b,p / 2,p)+ 1) % p; 72 ll power; 73 if(n == 0) 74 { 75 power = 1; 76 } 77 else 78 { 79 node m1; 80 m1.matrix[0][0] = 0; 81 m1.matrix[0][1] = m1.matrix[1][0] = m1.matrix[1][1] = 1; 82 power = pow_matrixmul(m1,n+2,p-1) % (p-1); 83 } 84 power += (p - 1); 85 node m2; 86 m2.matrix[0][0] = (a + b)% p; 87 m2.matrix[0][1] = 2*a*b % p; 88 m2.matrix[1][0] = 2; 89 m2.matrix[1][1] = (a + b) % p; 90 91 ll ans = pow_matrixmul(m2,power,p) % p; 92 ans = 2 * ans % p; 93 printf("%I64d\n",((ans1*ans2)%p * ans) % p); 94 } 95 96 return 0; 97 }