HDU 4686 Arc of Dream

来源:互联网 发布:无锡软件测试培训 编辑:程序博客网 时间:2024/06/05 17:00

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

4134

1902

找到递推式,画出矩阵即可

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;const ll M=1e9+7;const ll size=6;ll a0,b0,ax,bx,ay,by,n;struct abc{    ll a[size][size];    abc(){ memset(a,0,sizeof(a));};};abc operator *(const abc &a,const abc &b){    abc c;    for (int i=1;i<size;i++)        for (int j=1;j<size;j++)            for (int k=1;k<size;k++)            {                (c.a[i][k]+=a.a[i][j]*b.a[j][k])%=M;                (c.a[i][k]+=M)%=M;            }    return c;}int main(){  while (cin>>n>>a0>>ax>>ay>>b0>>bx>>by)  {     if (n==0) {puts("0"); continue;}     abc r,A;     r.a[1][2]=(a0*ax+ay)%M;     r.a[1][3]=(b0*bx+by)%M;     r.a[1][1]=(r.a[1][2]*r.a[1][3])%M;     r.a[1][4]=1;     r.a[1][5]=(a0*b0)%M;     A.a[1][1]=(ax*bx)%M;     A.a[1][5]=1;     A.a[2][1]=(ax*by)%M;     A.a[2][2]=ax%M;     A.a[3][1]=(ay*bx)%M;     A.a[3][3]=bx%M;     A.a[4][1]=(ay*by)%M;     A.a[4][2]=ay%M;     A.a[4][3]=by%M;     A.a[4][4]=1;     A.a[5][5]=1;     for (--n;n>0;n>>=1)     {         if (n&1) r=r*A;         A=A*A;     }     cout<<r.a[1][5]<<endl;  }  return 0;}


0 0