-
C - Recursive sequence
- HDU - 5950
- 关键在于矩阵的构造初始建立两个1*7的原始递推阵找出关系构造7*7的关系转移阵
-
#include <iostream> #include <cstring> using namespace std; #define ll long long #define maxn 10 struct node { ll mp[maxn][maxn]; } x,y,s; ll init[maxn][maxn]= {{1,1,0,0,0,0,0},{2,0,0,0,0,0,0},{1,0,1,0,0,0,0},{4,0,4,1,0,0,0},{6,0,6,3,1,0,0},{4,0,4,3,2,1,0},{1,0,1,1,1,1,1}}; node matric(node a,node b,int l,int r,int c) { node ans; for(int i=0; i<7; i++) for(int j=0; j<7; j++) ans.mp[i][j]=0; for(int i=0; i<l; i++) for(int k=0; k<c; k++) for(int j=0; j<r; j++) { ans.mp[i][j]+=(a.mp[i][k]*b.mp[k][j])%2147493647; ans.mp[i][j]%=2147493647; } return ans; } node qpow(node c,ll r) { node d; for(int i=0; i<7; i++) for(int j=0; j<7; j++) if(i==j) d.mp[i][j]=1; else d.mp[i][j]=0; while(r) { if(r%2) d=matric(d,c,7,7,7); c=matric(c,c,7,7,7); r/=2; } return d; } int t,n,one,two; int main() { memcpy(x.mp,init,sizeof(init)); cin>>t; while(t--) { cin>>n>>one>>two; y.mp[0][0]=two; y.mp[0][1]=one; y.mp[0][2]=16; y.mp[0][3]=8; y.mp[0][4]=4; y.mp[0][5]=2; y.mp[0][6]=1; s=matric(y,qpow(x,n-2),1,7,7); cout<<s.mp[0][0]<<endl; } return 0; }
C - Recursive sequence-矩阵快速幂
最新推荐文章于 2019-11-29 13:48:21 发布