HDU 4686 Arc of Dream (矩阵快速幂)

Arc of Dream

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 419    Accepted Submission(s): 148


Problem Description
An Arc of Dream is a curve defined by following function:
HDU 4686 Arc of Dream (矩阵快速幂)

where
a 0 = A0
a i = a i-1*AX+AY
b 0 = B0
b i = b i-1*BX+BY
What is the value of AoD(N) modulo 1,000,000,007?
 

 

Input
There are multiple test cases. Process to the End of File.
Each test case contains 7 nonnegative integers as follows:
N
A0 AX AY
B0 BX BY
N is no more than 10 18, and all the other integers are no more than 2×10 9.
 

 

Output
For each test case, output AoD(N) modulo 1,000,000,007.
 

 

Sample Input
1 1 2 3 4 5 6 2 1 2 3 4 5 6 3 1 2 3 4 5 6
 

 

Sample Output
4 134 1902
 

 

Author
Zejun Wu (watashi)
 

 

Source
 

 

Recommend
zhuyuanchen520
 

 http://www.cnblogs.com/liuxueyang/archive/2013/08/20/3270893.html

 

#include<iostream>

#include<cstdio>

#include<cstring>



using namespace std;



const int mod=1000000007;



struct Matrix{

    long long arr[5][5];

};



Matrix init,unit;

long long n,a0,ax,ay,b0,bx,by;  //注意用long long,否则会溢出



Matrix Mul(Matrix a,Matrix b){

    Matrix c;

    for(int i=0;i<5;i++)

        for(int j=0;j<5;j++){

            c.arr[i][j]=0;

            for(int k=0;k<5;k++)

                c.arr[i][j]=(c.arr[i][j]+a.arr[i][k]*b.arr[k][j]%mod)%mod;

            c.arr[i][j]%=mod;

        }

    return c;

}



Matrix Pow(Matrix a,Matrix b,long long k){

    while(k){

        if(k&1){

            b=Mul(b,a);

        }

        a=Mul(a,a);

        k>>=1;

    }

    return b;

}



void Init(){

    for(int i=0;i<5;i++)

        for(int j=0;j<5;j++){

            init.arr[i][j]=0;

            unit.arr[i][j]=0;

        }

    unit.arr[0][0]=1,   unit.arr[0][1]=a0%mod,    unit.arr[0][2]=b0%mod,    unit.arr[0][3]=a0*b0%mod,

    unit.arr[0][4]=a0*b0%mod;



    init.arr[0][0]=1,   init.arr[0][1]=ay%mod,    init.arr[0][2]=by%mod,    init.arr[0][3]=ay*by%mod,

    init.arr[0][4]=ay*by%mod,   init.arr[1][1]=ax%mod,  init.arr[1][3]=ax*by%mod,   init.arr[1][4]=ax*by%mod,

    init.arr[2][2]=bx%mod,  init.arr[2][3]=ay*bx%mod,   init.arr[2][4]=ay*bx%mod,   init.arr[3][3]=ax*bx%mod,

    init.arr[3][4]=ax*bx%mod,   init.arr[4][4]=1;

}



int main(){



    //freopen("input.txt","r",stdin);



    while(cin>>n){

        //scanf("%d%d%d%d%d%d",&a0,&ax,&ay,&b0,&bx,&by);

        cin>>a0>>ax>>ay>>b0>>bx>>by;

        if(n==0){

            puts("0");

            continue;

        }

        Init();

        Matrix ans=Pow(init,unit,n-1);

        cout<<ans.arr[0][4]<<endl;

    }

    return 0;

}

 

你可能感兴趣的:(HDU)