一道NOI水题。。没题目描述这题能做?
矩乘还要处理超longlong的计算,昨天自己的方法过得有些侥幸,就看了下LSJ教主的代码
那么矩乘在这就先告段落
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(i=l;i<=r;i++) 3 #define dec(i,l,r) for(i=l;i>=r;i--) 4 #define mem(a) memset(a,0,sizeof(a)) 5 #define ll long long 6 #define succ(x) (1<<x) 7 using namespace std; 8 ll read(){ 9 ll x=0,f=1;char ch=getchar(); 10 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 11 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 12 return x*f; 13 } 14 struct mat{ 15 int n,m; 16 ll a[3][3]; 17 }a,b,tmp; 18 ll n,m,inf; 19 ll cal(ll a,ll b){ 20 ll ans=0; 21 for(;b;b>>=1){ 22 if(b&1)ans+=a; 23 if(ans>=m)ans-=m; 24 a<<=1; 25 if(a>=m)a-=m; 26 } 27 return ans; 28 } 29 mat operator*(const mat&x,const mat&y){ 30 mat s; 31 int i,j,k; 32 mem(s.a); 33 s.n=x.n;s.m=y.m; 34 inc(i,1,s.n) 35 inc(j,1,s.m) 36 inc(k,1,x.m) 37 (s.a[i][j]+=cal(x.a[i][k],y.a[k][j]))%=m; 38 return s; 39 } 40 mat mul(ll k){ 41 mat s=a,t=a;k--; 42 while(k>0){ 43 if(k%2)s=s*t; 44 t=t*t;k/=2; 45 } 46 return s; 47 } 48 int main(){ 49 m=read(); 50 b.n=a.n=a.m=2; 51 a.a[1][1]=1;a.a[1][2]=0;a.a[2][2]=read();a.a[2][1]=read(); 52 b.m=1;b.a[1][1]=1;b.a[2][1]=read(); 53 n=read();inf=read(); 54 tmp=mul(n); 55 b=tmp*b; 56 printf("%lld ",b.a[2][1]%inf); 57 return 0; 58 }