uva4686(矩阵快速幂)

来源:互联网 发布:游戏锁定方框软件 编辑:程序博客网 时间:2024/06/04 21:56
题意:0 = A0 
i = a i-1*AX+AY 
0 = B0 

i = b i-1*BX+BY 让你求

题解:ai*bi = i-1*AX*bi-1*BX+a i-1*AX*BY+b i-1*BX*AY+AY*BY

然后可以用矩阵方程为下图:

接着跑个快速幂即可

#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>using namespace std;typedef long long int ll;const int  mod = 1e9+7;struct mat{    ll a[5][5];    mat operator*(const mat &y)const{        mat z;        for(int i = 0; i < 5; i++)            for(int j = 0; j < 5; j++){                z.a[i][j] = 0;                for(int k = 0; k < 5; k++)                    z.a[i][j] = (z.a[i][j]+a[i][k]*y.a[k][j]%mod)%mod;            }        return z;    }};ll a0,ax,ay,b0,bx,by;mat quick(ll n){    n--;    mat ans = {a0*b0%mod,0,0,0,0,a0*b0%mod,0,0,0,0,a0,0,0,0,0,b0,0,0,0,0,1,0,0,0,0};    mat x = {1,ax*bx%mod,ax*by%mod,bx*ay%mod,ay*by%mod,0,ax*bx%mod,ax*by%mod,bx*ay%mod,ay*by%mod,0,0,ax,0,ay,0,0,0,bx,by,0,0,0,0,1};    while(n){        if(n&1) ans = x*ans;        x = x*x;        n /= 2;    }    return ans;}int main(){    ll n;    while(scanf("%lld",&n)==1){        scanf("%lld%lld%lld",&a0,&ax,&ay);        scanf("%lld%lld%lld",&b0,&bx,&by);        if(n==0){            puts("0");            continue;        }        a0 %= mod;        ax %= mod;        ay %= mod;        b0 %= mod;        bx %= mod;        by %= mod;        mat ans = quick(n);        printf("%lld\n",ans.a[0][0]%mod);    }    return 0;}

 


原创粉丝点击