效率有点堪忧啊。。。
其实矩乘还是挺明显的
然后按维来讨论 方程也挺好写的 然后就没有然后了。。
#include<bits/stdc++.h>
#define me(a,x) memset(a,x,sizeof a)
using namespace std;
typedef long long LL;
inline int read()
{
char ch=getchar(); int x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0'; ch=getchar();}
return x*f;
}
LL m;
struct node{LL a[3][3]; node(){me(a,0);} };
void cheng(node &a,node b,int al){
LL c[3][3]={0};
for(int i=0;i<al;i++)
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
(c[i][j]+=a.a[i][k]*b.a[k][j])%=m;
for(int i=0;i<al;i++)for(int j=0;j<3;j++)a.a[i][j]=c[i][j];
}
int main()
{
LL n; unsigned long long u=1; scanf("%lld%lld",&n,&m);
node a,b; a.a[0][2]=1;
if(n>1)for(;;){
LL k;
if(n>=u*10)k=u*9; else k=n-u+1; k--;
b.a[0][0]=u*10%m,b.a[1][0]=b.a[1][1]=b.a[2][0]=b.a[2][1]=b.a[2][2]=1;
cheng(a,b,1);
while(k){
if(k&1)cheng(a,b,1);
cheng(b,b,3); k>>=1;
}
if(n>=u*10)u*=10; else break;
}
printf("%lld\n",a.a[0][0]);
return 0;
}