题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1588
题意简单,化简成A^b+A^b*( B+B^2+……B^(n-1) ),其中B=A^k
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib> #include <cmath> #include <vector> #include <list> #include <deque> #include <queue> #include <iterator> #include <stack> #include <map> #include <set> #include <algorithm> #include <cctype> #include <ctime> using namespace std; typedef __int64 LL; const int N=1005; const double eps=1e-6; const int M=1<<12; const int INF=0x3f3f3f3f; const double PI=acos(-1.0); LL mod; LL n,b,m,k; struct MM { LL x[2][2]; }A,t; void prin(MM p) { for(int i=0;i<2;i++) printf("%I64d %I64d\n",p.x[i][0],p.x[i][1]); cout<<endl; } MM xh_cheng(MM a,MM b) { MM p; int i,j,k; for(i=0;i<2;i++) for(j=0;j<2;j++) { LL sum=0; for(k=0;k<2;k++) sum=(sum+a.x[i][k]*b.x[k][j])%mod; p.x[i][j]=sum; } return p; } MM xh_pow(MM a,LL b) { MM p; p.x[0][0]=p.x[1][1]=1; p.x[0][1]=p.x[1][0]=0; while(b) { if(b&1) p=xh_cheng(p,a); a=xh_cheng(a,a); b>>=1; } return p; } MM xh_add(MM a,MM b) { MM p; int i,j; for(i=0;i<2;i++) for(j=0;j<2;j++) p.x[i][j]=(a.x[i][j]+b.x[i][j])%mod; return p; } MM love(MM a,LL p) { MM x,o; if(p==1) { t=a; return t; } x=love(a,p/2); if(p&1) { o=xh_pow(a,p/2+1); return xh_add(xh_add(x,o),xh_cheng(x,o)); } else { o=xh_pow(a,p/2); return xh_add(x,xh_cheng(x,o)); } } int main() { while(~scanf("%I64d%I64d%I64d%I64d",&k,&b,&n,&mod)) { A.x[0][0]=A.x[0][1]=A.x[1][0]=1; A.x[1][1]=0; t=xh_pow(A,k); t=love(t,n-1); MM mb=xh_pow(A,b); MM p=xh_add(mb,xh_cheng(mb,t)); printf("%I64d\n",p.x[1][0]); } return 0; } /* 2 1 4 100 2 0 4 100 */