hdu 4686 Arc of Dream(矩阵快速幂乘法)

时间:2022-10-07 15:38:32
Problem Description
An Arc of Dream is a curve defined by following function:

hdu 4686 Arc of Dream(矩阵快速幂乘法)

where
a0 = A0
ai = ai-*AX+AY
b0 = B0
bi = bi-*BX+BY
What is the value of AoD(N) modulo ,,,?
Input
There are multiple test cases. Process to the End of File.
Each test case contains nonnegative integers as follows:
N
A0 AX AY
B0 BX BY
N is no more than , and all the other integers are no more than ×.
Output
For each test case, output AoD(N) modulo ,,,.
Sample Input

Sample Output

Author
Zejun Wu (watashi)
Source

因为:a[i]*b[i]=(a[i-1]*AX+AY)*(b[i-1]*BX+BY)

      =(a[i-1]*b[i-1]*AX*BX+a[i-1]*AX*BY+b[i-1]*BX*AY+AY*BY)

构造矩阵:

                                                          |  1         0    0     0      0   |

                                                          |  AX*BY   AX   0     AX*BY    0   |

           {AoD(n-1),a[i-1],b[i-1],a[i-1]*b[i-1],1}*      |  BX*AY    0   BX    BX*AY    0   |      ={AoD(n),a[i],b[i],a[i]*b[i],1}

                                                          |  AX*BX    0   0      AX*BX   0   |

                                                          |  AY*BY   AY   BY    AY*BY    1   |

 

 

另外注意:

if(n==0){//这个判断条件很重要,没有就会超时
printf("0\n");
continue;
}

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1000000
#define inf 1e12
ll n;
ll A0,Ax,Ay,B0,Bx,By;
struct Matrix{
ll mp[][];
};
Matrix Mul(Matrix a,Matrix b){
Matrix res;
for(ll i=;i<;i++){
for(ll j=;j<;j++){
res.mp[i][j]=;
for(ll k=;k<;k++){
res.mp[i][j]=(res.mp[i][j]+(a.mp[i][k]*b.mp[k][j])%MOD+MOD)%MOD;
}
}
}
return res;
}
Matrix fastm(Matrix a,ll b){
Matrix res;
memset(res.mp,,sizeof(res.mp));
for(ll i=;i<;i++){
res.mp[i][i]=;
}
while(b){
if(b&){
res=Mul(res,a);
}
a=Mul(a,a);
b>>=;
}
return res;
}
int main()
{
while(scanf("%I64d",&n)==){
scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&A0,&Ax,&Ay,&B0,&Bx,&By); if(n==){//这个判断条件很重要,没有就会超时
printf("0\n");
continue;
} ll a0=A0;
ll b0=B0; Matrix tmp;
memset(tmp.mp,,sizeof(tmp.mp));
tmp.mp[][]=%MOD;
tmp.mp[][]=Ax*By%MOD;
tmp.mp[][]=Ax%MOD;
tmp.mp[][]=Ax*By%MOD;
tmp.mp[][]=Bx*Ay%MOD;
tmp.mp[][]=Bx%MOD;
tmp.mp[][]=Bx*Ay%MOD;
tmp.mp[][]=Ax*Bx%MOD;
tmp.mp[][]=Ax*Bx%MOD;
tmp.mp[][]=Ay*By%MOD;
tmp.mp[][]=Ay%MOD;
tmp.mp[][]=By%MOD;
tmp.mp[][]=Ay*By%MOD;
tmp.mp[][]=%MOD; Matrix cnt=fastm(tmp,n-); Matrix g;
memset(g.mp,,sizeof(g.mp));
g.mp[][]=a0*b0%MOD;
g.mp[][]=a0%MOD;
g.mp[][]=b0%MOD;
g.mp[][]=a0*b0%MOD;
g.mp[][]=%MOD;
Matrix ans=Mul(g,cnt);
printf("%I64d\n",ans.mp[][]);
}
return ;
}