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

来源:互联网 发布:淘宝卖家评价怎么写 编辑:程序博客网 时间:2024/05/23 00:18
Problem Description
An Arc of Dream is a curve defined by following function:

where
a0 = A0
ai = ai-1*AX+AY
b0 = B0
bi = bi-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 1018, and all the other integers are no more than 2×109.
 

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

Sample Input
11 2 34 5 621 2 34 5 631 2 34 5 6
 

Sample Output
41341902
 
矩阵快速幂:关键是如何构造矩阵。
我们发现Aod0=a0*b0*ax*bx+a0*ax*by+b0*ay*bx+ay*by.跟a0,b0,a0*b0有关。
构造矩阵时可以如下构造:
此题的坑点在于输入的数据中可能有大于1e9+7的,所以不取模就会WA或者TLE。
还有要考虑n=0的情况。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;#define N 5const int INF = 0x3f3f3f3f;const int MOD=1e9+7;struct Matrix{    LL mat[N][N];    void Clear()    {        CLEAR(mat,0);    }};Matrix Mult(Matrix m1,Matrix m2){    Matrix ans;    for(int i=0;i<N;i++)        for(int j=0;j<N;j++){            ans.mat[i][j]=0;            for(int k=0;k<N;k++)                ans.mat[i][j]=(ans.mat[i][j]+m1.mat[i][k]*m2.mat[k][j])%MOD;        }    return ans;}Matrix Pow(Matrix m1,LL b){    Matrix ans;    for(int i=0;i<N;i++)        for(int j=0;j<N;j++)            ans.mat[i][j]=(i==j);    while(b){        if(b&1)            ans=Mult(ans,m1);        m1=Mult(m1,m1);        b>>=1;    }    return ans;}int main(){    LL n;    LL a0,ax,ay;    LL b0,bx,by;    while(~scanf("%I64d",&n))    {        scanf("%I64d%I64d%I64d",&a0,&ax,&ay);        scanf("%I64d%I64d%I64d",&b0,&bx,&by);        if(n==0)        {            puts("0");            continue;        }        Matrix A;A.Clear();        A.mat[0][0]=ax%MOD;A.mat[0][2]=ax*by%MOD;        A.mat[0][3]=ax%MOD*by%MOD;A.mat[1][1]=bx%MOD;        A.mat[1][2]=ay%MOD*bx%MOD;A.mat[1][3]=ay%MOD*bx%MOD;        A.mat[2][2]=ax%MOD*bx%MOD;A.mat[2][3]=ax%MOD*bx%MOD;        A.mat[3][3]=1;A.mat[4][0]=ay%MOD;A.mat[4][1]=by%MOD;        A.mat[4][2]=ay*by%MOD;A.mat[4][3]=ay*by%MOD;A.mat[4][4]=1;        A=Pow(A,n-1);        LL ans=0;        ans=(ans+a0%MOD*A.mat[0][3])%MOD;ans=(ans+b0%MOD*A.mat[1][3])%MOD;        ans=(ans+a0%MOD*b0%MOD*A.mat[2][3])%MOD;ans=(ans+a0%MOD*b0*A.mat[3][3])%MOD;        ans=(ans+A.mat[4][3])%MOD;        printf("%I64d\n",ans);    }    return 0;}


0 0
原创粉丝点击