Matrix multiplication.
10^k,0,0
(f[i+1],i+1,1) = (f[i],i,1) (1, 1,0)
1.1,0)
K is the number of digits (i+1) . This is important, so every time we count to 999 ... 9, then K will be +1. So both L and R in the title are actual values of +1. (Need yy for a bit).
#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;unsignedLong LongN,mod;structMatrix {Long Longa[3][3]; Long Long*operator[] (intx) {returnA[x]; } Matrixoperator*(Matrix B) {matrix res; for(intI=0;i<3; i++) for(intj=0;j<3; j + +) for(intk=0;k<3; k++) Res[i][j]= (Res[i][j]+a[i][k]*b[k][j])%MoD; returnRes; } Matrixoperator^ (Long Longe) {Matrix res (1), tmp=* This; while(e) {if(e&1) res=res*tmp; TMP=tmp*tmp; E>>=1; } returnRes; } voidBuildLong Longx) {memset (A,0,sizeof(a)); a[0][0]=x%MoD; for(intI=1;i<3; i++) for(intj=0; j<=i;j++) A[i][j]=1; } Matrix (Long Longx=0) {memset (A,0,sizeof(a)); for(intI=0;i<3; i++) a[i][i]=x; }}cur;structVector {Long Longa[3]; Long Long&operator[] (intx) {returnA[x]; } Vectoroperator*(Matrix b) {Vector res; for(intI=0;i<3; i++) for(intk=0;k<3; k++) Res[i]= (Res[i]+a[k]*b[k][i])%MoD; returnRes; } voidbuild () {memset (A,0,sizeof(a)); a[2]=1; } Vector () {memset (A,0,sizeof(a)); }}res;intMain () {scanf ("%lld%lld",&n,&MoD); Res.build (); Long LongL,r; for(l=1, r=Ten; r<=n;l=r,r=l*Ten) {cur.build (R); Res=res* (cur^ (Rl)); } cur.build (R); Res=res* (cur^ (n-l+1)); printf ("%lld\n", res[0]); return 0;}
bzoj2326: [HNOI2011] Math Homework