1#include <iostream>2#include <cstring>3#include <cstdio>4 5 using namespacestd;6 7 //Maximum matrix size8 Const intsiz= -;9 intMod= -;Ten One //The matrix size is n*m and the initialization is all 0 A structMat - { - intn,m; the intAr[siz][siz]; - Mat () - { -memset (AR,0,sizeof(AR)); +n=m=SIZ; - }; + }; A at //matrix multiplication -Matoperator*(Mat A,mat b) - { - Mat C; -C=Mat (); -c.n=A.N; inc.m=b.m; - for(intI=1; i<=a.n;i++) to for(intj=1; j<=b.n;j++) + if(a.ar[i][j]!=0) - for(intk=1; k<=a.m;k++) the { *c.ar[i][k]+= (A.ar[i][j]*b.ar[j][k])%MOD; $c.ar[i][k]%=MOD;Panax Notoginseng } - returnC; the } + A //Matrix Fast Power theMatoperator^ (Mat A,Long Longk) + { - Mat C; $C=Mat (); $c.n=A.N; -c.m=a.m.; - for(intI=1; i<=a.n;i++) thec.ar[i][i]=1; - while(k)Wuyi { the if(k&1) -c=c*A; Wua=a*A; -K/=2; About } $ returnC; - } - - intMain () A { + intT; the while(SCANF ("%d", &t)! =EOF) - { $ if(t==0) the Break; the for(intL=1; l<=t;l++) the { the Long LongN; - Mat A; ina.m=a.n=4; the for(intI=1; i<=4; i++) the { About for(intj=1; j<=4; j + +) the { thea.ar[i][j]=1; the if(i==j) +a.ar[i][j]=2; - if(i+j==5) thea.ar[i][j]=0;Bayi } the } thescanf"%i64d",&n); -Mat b=a^N; - intans=b.ar[1][1]; thecout<<" Case"<<l<<": "<<ans<<Endl; the } thecout<<Endl; the } - return 0; the}View Code
Recursive + matrix fast power HDU 2065