HDU 4686 Arc of Dream

矩阵快速幂

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

struct Matrix
{
    long long A[10][10];
    Matrix operator*(Matrix b);
};
const long long MOD=1000000007;
long long R,C;
long long N,A0,AX,AY,B0,BX,BY;

Matrix Matrix::operator*(Matrix b)
{
    Matrix c;
    int i,j,k;
    memset(c.A,0,sizeof(c.A));
    for(i=1; i<=R; i++)
        for(j=1; j<=C; j++)
            for(k=1; k<=5; k++)
                c.A[i][j]=(c.A[i][j]+(A[i][k]*b.A[k][j])%MOD)%MOD;
    return c;
}

int main()
{
    int i,j;
    while(~scanf("%lld",&N))
    {

        scanf("%lld%lld%lld",&A0,&AX,&AY);
        scanf("%lld%lld%lld",&B0,&BX,&BY);

        A0=A0%MOD;
        AX=AX%MOD;
        AY=AY%MOD;

        B0=B0%MOD;
        BX=BX%MOD;
        BY=BY%MOD;

        if(N==0)
        {
            printf("0\n");
            continue;
        }

        Matrix JZ;
        JZ.A[1][1]=1;JZ.A[1][2]=0;JZ.A[1][3]=0;JZ.A[1][4]=0;JZ.A[1][5]=0;
        JZ.A[2][1]=1;JZ.A[2][2]=(AX*BX)%MOD;JZ.A[2][3]=0;JZ.A[2][4]=0;JZ.A[2][5]=0;
        JZ.A[3][1]=0;JZ.A[3][2]=(AX*BY)%MOD;JZ.A[3][3]=AX%MOD;JZ.A[3][4]=0;JZ.A[3][5]=0;
        JZ.A[4][1]=0;JZ.A[4][2]=(AY*BX)%MOD;JZ.A[4][3]=0;JZ.A[4][4]=BX%MOD;JZ.A[4][5]=0;
        JZ.A[5][1]=0;JZ.A[5][2]=(AY*BY)%MOD;JZ.A[5][3]=AY%MOD;JZ.A[5][4]=BY%MOD;JZ.A[5][5]=1;

        Matrix c;
        for(i=1; i<=9; i++)
        {
            for(j=1; j<=9; j++)
            {
                if(i==j) c.A[i][j]=1;
                else c.A[i][j]=0;
            }
        }

        long long b=N-1;
        R=5;
        C=5;
        while(b>0)
        {
            if(b%2==1) c=c*JZ,b--;
            else JZ=JZ*JZ,b=b/2;
        }
        Matrix CS;
        CS.A[1][1]=(A0*B0)%MOD;
        CS.A[1][4]=((B0*BX)%MOD+BY)%MOD;
        CS.A[1][3]=((A0*AX)%MOD+AY)%MOD;
        CS.A[1][2]=(CS.A[1][3]*CS.A[1][4])%MOD;
        CS.A[1][5]=1;

        R=1;
        C=5;
        if(N!=1) CS=CS*c;

        printf("%lld\n",CS.A[1][1]);
    }
    return 0;
}

 

posted @ 2015-07-20 19:14  Fighting_Heart  阅读(131)  评论(0编辑  收藏  举报