标签:algo roman strong output height 分享 关系 case ring
矩阵高速幂:
依据关系够建矩阵 , 高速幂解决.
1 1 2 3 4 5 6 2 1 2 3 4 5 6 3 1 2 3 4 5 6
4 134 1902
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long int LL; const LL mod=1000000007LL; struct Matrix { int x,y; LL m[6][6]; Matrix() {x=y=5;memset(m,0,sizeof(m));} void one() { for(int i=0;i<5;i++) m[i][i]=1LL; } void show() { cout<<x<<" * "<<y<<endl; for(int i=0;i<x;i++) { for(int j=0;j<y;j++) cout<<m[i][j]<<","; cout<<endl; } } }; Matrix Mul(Matrix& a,Matrix& b) { Matrix ret; ret.x=a.x; ret.y=b.y; for(int i=0;i<a.x;i++) { for(int j=0;j<b.y;j++) { LL temp=0; for(int k=0;k<b.y;k++) { temp=(temp+(a.m[i][k]*b.m[k][j])%mod)%mod; } ret.m[i][j]=temp%mod; } } return ret; } Matrix quickPow(Matrix m,LL x) { Matrix e; e.one(); while(x) { if(x&1LL) e=Mul(e,m); m=Mul(m,m); x/=2LL; } return e; } LL n,A0,B0,AX,AY,BX,BY; Matrix init_matrix() { Matrix ret; ret.m[0][0]=1; ret.m[1][0]=AY; ret.m[1][1]=AX; ret.m[2][0]=BY; ret.m[2][2]=BX; ret.m[3][0]=(BY*AY)%mod; ret.m[3][1]=(AX*BY)%mod; ret.m[3][2]=(BX*AY)%mod; ret.m[3][3]=(AX*BX)%mod; ret.m[4][3]=1LL; ret.m[4][4]=1LL; return ret; } Matrix Beg() { Matrix beg; beg.m[0][0]=1; beg.m[1][0]=A0; beg.m[2][0]=B0; beg.m[3][0]=A0*B0%mod; return beg; } int main() { while(cin>>n) { cin>>A0>>AX>>AY>>B0>>BX>>BY; A0=A0%mod; AX=AX%mod; AY=AY%mod; B0=B0%mod; BX=BX%mod; BY=BY%mod; Matrix m=init_matrix(); m=quickPow(m,n); Matrix beg=Beg(); LL ans=0; for(int i=0;i<5;i++) ans=(ans+beg.m[i][0]*m.m[4][i]%mod)%mod; cout<<ans<<endl; } return 0; }
标签:algo roman strong output height 分享 关系 case ring
原文地址:http://www.cnblogs.com/yfceshi/p/6731496.html