http://hzwer.com/2831.html
#include<cstdio> #include<iostream> #include<vector> using namespace std; typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; ll n,MOD; mat operator * (const mat &a,const mat &b) { mat c(a.size(),vec(b[0].size())); for(int i=0;i<a.size();++i) for(int j=0;j<b[0].size();++j) for(int k=0;k<b.size();++k) c[i][j]=(c[i][j]+(a[i][k]%MOD)*(b[k][j]%MOD)%MOD)%MOD; return c; } mat Quick_Pow(mat x,ll p) { if(!p) { mat t(3,vec(3)); for(int i=0;i<3;++i) t[i][i]=1; return t; } mat res=Quick_Pow(x,p>>1); res=res*res; if(p&1) res=res*x; return res; } int main() { cin>>n>>MOD; ll now=10; mat base(3,vec(3)); base[0][0]=now; base[0][1]=base[0][2]=base[1][1]=base[1][2]=base[2][2]=1; mat fir(3,vec(1)); fir[2][0]=1; while(now<=n) { mat kbase=Quick_Pow(base,now-now/10); fir=kbase*fir; now*=10; base[0][0]=now; } mat kbase=Quick_Pow(base,n-now/10+1); fir=kbase*fir; cout<<fir[0][0]<<endl; return 0; }