2326: [HNOI2011] Math job time limit:10 Sec Memory limit:128 MB
submit:1388 solved:799
[Submit] [Status] [Discuss] Description
Input Output sample input sample output HINT Source
Matrix multiplication
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>#definell Long Longusing namespacestd;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; } returns;}voidMul (LL a[4][4],ll b[4][4],ll ans[4][4]) {ll t[4][4]; for(intI=1; i<=3; i++) for(intj=1; j<=3; j + +) {T[i][j]=0; for(intk=1; k<=3; k++) T[i][j]= (T[i][j]+mu (a[i][k],b[k][j))%m; } for(intI=1; i<=3; i++) for(intj=1; j<=3; j + +) Ans[i][j]=t[i][j];}voidCal (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/Ten+1; while(y) {if(y&1) Mul (a,b,a); Mul (B,B,B); Y>>=1; }}intMain () {scanf ("%lld%lld",&n,&m); for(intI=1; i<=3; i++) a[i][i]=1; ll T=Ten; while(n>=t) {cal (T,t-1); T*=Ten; } cal (T,n); printf ("%lld", a[3][1]); return 0;}
[HNOI2011] [BZOJ2326] Math Homework | dynamic programming | matrix multiplication | fast multiplication