标签:
#include<cstdio> #include<cstring> using namespace std; int m; int f(int x) { int s1[5][5],s2[5][5],s3[5][5],i,a=0,j,k,b=0,c=0,d=0; memset(s1,0,sizeof(s1)); memset(s2,0,sizeof(s2)); s1[0][1]=1;s2[0][1]=1; s1[1][3]=1;s2[1][3]=1; s1[2][0]=1;s2[2][0]=1; s1[2][1]=1;s2[2][1]=1; s1[3][2]=1;s2[3][2]=1; s1[3][3]=1;s2[3][3]=1; while (x) { if (x&1) { memset(s3,0,sizeof(s3)); for (i=0;i<4;i++) for (j=0;j<4;j++) for (k=0;k<4;k++) s3[i][j]+=s1[i][k]*s2[k][j]; for (i=0;i<4;i++) for (j=0;j<4;j++) s2[i][j]=s3[i][j]%m; } memset(s3,0,sizeof(s3)); for (i=0;i<4;i++) for (j=0;j<4;j++) for (k=0;k<4;k++) s3[i][j]+=s1[i][k]*s1[k][j]; for (i=0;i<4;i++) for (j=0;j<4;j++) s1[i][j]=s3[i][j]%m; x>>=1; } for (i=0;i<4;i++) { a+=s2[i][0]; b+=s2[i][1]; c+=s2[i][2]; d+=s2[i][3]; } return a+b+c+d; } int main() { int l,p; while (~scanf("%d%d",&l,&m)) { if (l==0) {printf("0\n");continue;} if (l==1) {printf("%d\n",2%m);continue;} if (l==2) {printf("%d\n",4%m);continue;} l-=3; p=f(l)%m; printf("%d\n",p); } return 0; }
Description
Input
Output
Sample Input
Sample Output
标签:
原文地址:http://www.cnblogs.com/pblr/p/4690278.html