标签:
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #define ll long long using namespace std; ll n,m,a[4][4],b[4][4]; ll mu(ll x,ll y) { ll s=0; while (y) { if (y&1) s=(s+x)%m; x=(x<<1)%m; y>>=1; } return s; } void mul(ll a[4][4],ll b[4][4],ll ans[4][4]) { ll t[4][4]; for (int i=1;i<=3;i++) for (int j=1;j<=3;j++) { t[i][j]=0; for (int k=1;k<=3;k++) t[i][j]=(t[i][j]+mu(a[i][k],b[k][j]))%m; } for (int i=1;i<=3;i++) for (int j=1;j<=3;j++) ans[i][j]=t[i][j]; } void cal(ll t,ll last) { memset(b,0,sizeof(b)); b[1][1]=t; b[2][1]=b[2][2]=b[3][1]=b[3][2]=b[3][3]=1; ll y=last-t/10+1; while (y) { if (y&1) mul(a,b,a); mul(b,b,b); y>>=1; } } int main() { scanf("%lld%lld",&n,&m); for (int i=1;i<=3;i++) a[i][i]=1; ll t=10; while (n>=t) { cal(t,t-1); t*=10; } cal(t,n); printf("%lld",a[3][1]); return 0; }
[HNOI2011][BZOJ2326] 数学作业|动态规划|矩阵乘法|快速乘
标签:
原文地址:http://www.cnblogs.com/ws-fqk/p/4737636.html