Arc of Dream
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5544 Accepted Submission(s): 1731
Problem Description
An Arc of Dream is a curve defined by following function:
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?
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.
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1000000007;
struct node
{
ll mx[5][5];
}a,b;
void init()
{
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
a.mx[i][j]=b.mx[i][j]=0;
}
node cheng(node x,node y)
{
node tmp;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
tmp.mx[i][j]=0;
for(int k=0;k<5;k++)
{
tmp.mx[i][j]+=(x.mx[i][k]*y.mx[k][j]%mod);
tmp.mx[i][j]%=mod;
}
}
}
return tmp;
}
void mx_pow(ll n)
{
while(n)
{
if(n&1)b=cheng(a,b);
a=cheng(a,a);
n=n/2;
}
}
int main()
{
ll n;
while(~scanf("%lld",&n))
{
init();
ll A0,AX,AY,B0,BX,BY;
scanf("%lld%lld%lld%lld%lld%lld",&A0,&AX,&AY,&B0,&BX,&BY);
if(!n)
{
printf("0\n");continue;
}
A0%=mod;AX%=mod;AY%=mod;
B0%=mod;BX%=mod;BY%=mod;
a.mx[0][0]=AX*BX;a.mx[0][1]=AX*BY;
a.mx[0][2]=AY*BX;a.mx[0][3]=AY*BY;a.mx[0][4]=0;
a.mx[1][0]=0;a.mx[1][1]=AX;a.mx[1][2]=0;
a.mx[1][3]=AY;a.mx[1][4]=0;
a.mx[2][0]=0;a.mx[2][1]=0;a.mx[2][2]=BX;
a.mx[2][3]=BY;a.mx[2][4]=0;
a.mx[3][0]=0;a.mx[3][1]=0;a.mx[3][2]=0;
a.mx[3][3]=1;a.mx[3][4]=0;
a.mx[4][0]=AX*BX;a.mx[4][1]=AX*BY;
a.mx[4][2]=AY*BX;a.mx[4][3]=AY*BY;a.mx[4][4]=1;
b.mx[0][0]=A0*B0;b.mx[1][0]=A0;b.mx[2][0]=B0;
b.mx[3][0]=1;b.mx[4][0]=A0*B0;
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
{
a.mx[i][j]%=mod;
b.mx[i][j]%=mod;
}
mx_pow(n-1);
printf("%lld\n",b.mx[4][0]);
}
return 0;
}