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
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
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=; struct Matrix{
long long arr[][];
}; 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=;i<;i++)
for(int j=;j<;j++){
c.arr[i][j]=;
for(int k=;k<;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&){
b=Mul(b,a);
}
a=Mul(a,a);
k>>=;
}
return b;
} void Init(){
for(int i=;i<;i++)
for(int j=;j<;j++){
init.arr[i][j]=;
unit.arr[i][j]=;
}
unit.arr[][]=, unit.arr[][]=a0%mod, unit.arr[][]=b0%mod, unit.arr[][]=a0*b0%mod,
unit.arr[][]=a0*b0%mod; init.arr[][]=, init.arr[][]=ay%mod, init.arr[][]=by%mod, init.arr[][]=ay*by%mod,
init.arr[][]=ay*by%mod, init.arr[][]=ax%mod, init.arr[][]=ax*by%mod, init.arr[][]=ax*by%mod,
init.arr[][]=bx%mod, init.arr[][]=ay*bx%mod, init.arr[][]=ay*bx%mod, init.arr[][]=ax*bx%mod,
init.arr[][]=ax*bx%mod, init.arr[][]=;
} 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==){
puts("");
continue;
}
Init();
Matrix ans=Pow(init,unit,n-);
cout<<ans.arr[][]<<endl;
}
return ;
}
上一篇:第一篇英文短文《It All Starts With A Dream》


下一篇:POJ 2411 Mondriaan's Dream