标签:code memset opened href https pac lan return its
题意:求 f(n) = f(n?1)+2*f(n?2)+n4,其中 f(1)=a,f(2)=b
利用矩阵加速~
比较坑的是mod=2147493647。。。。不是2147483647,用int WA了多次=_=||
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 const ll mod=2147493647; 5 const int maxn=7; 6 7 struct Matrix{ 8 ll n,m[maxn][maxn]; 9 10 void init(ll sz){n=sz;memset(m,0,sizeof(m));} 11 Matrix(ll sz){init(sz);} 12 void set_I(){ 13 for(int i=0;i<n;i++) m[i][i]=1; 14 } 15 Matrix operator* (Matrix a){ 16 Matrix ans(n); 17 for(ll k=0;k<n;k++) 18 for(ll j=0;j<n;j++) 19 for(ll i=0;i<n;i++){ 20 ans.m[i][j]=(ans.m[i][j]+m[i][k]*a.m[k][j]%mod)%mod; 21 } 22 return ans; 23 } 24 }; 25 26 27 int main(){ 28 int t; 29 scanf("%d",&t); 30 Matrix a(7); 31 a.set_I(); 32 a.m[0][1]=2;a.m[0][2]=1;a.m[0][3]=4; 33 a.m[0][4]=6;a.m[0][5]=4;a.m[0][6]=1; 34 a.m[1][0]=1;a.m[1][1]=0; 35 a.m[2][3]=4;a.m[2][4]=6;a.m[2][5]=4;a.m[2][6]=1; 36 a.m[3][4]=3;a.m[3][5]=3;a.m[3][6]=1; 37 a.m[4][5]=2;a.m[4][6]=1; 38 a.m[5][6]=1; 39 while(t--){ 40 ll n,s,b; 41 scanf("%lld%lld%lld",&n,&s,&b); 42 s%=mod; 43 b%=mod; 44 if(n==1){ 45 printf("%lld\n",s); 46 continue; 47 } 48 if(n==2){ 49 printf("%lld\n",b); 50 continue; 51 } 52 Matrix ans(7),p(7); 53 ans.set_I(); 54 p=a; 55 n=n-2; 56 while(n){ 57 if(n&1) ans=ans*p; 58 n>>=1; 59 p=p*p; 60 } 61 ll res=0; 62 res=b*ans.m[0][0]%mod; 63 res=(res+s*ans.m[0][1]%mod)%mod; 64 res=(res+ans.m[0][2]*16%mod)%mod; 65 res=(res+ans.m[0][3]*8%mod)%mod; 66 res=(res+ans.m[0][4]*4%mod)%mod; 67 res=(res+ans.m[0][5]*2%mod)%mod; 68 res=(res+ans.m[0][6])%mod; 69 printf("%lld\n",res); 70 } 71 return 0; 72 }
标签:code memset opened href https pac lan return its
原文地址:http://www.cnblogs.com/yijiull/p/7419386.html