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
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
注意点 :输入的a0,ax,ay,b0,bx,by在初始化时相乘要记得先取模不然lld*lld 会爆
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<math.h>
#include<vector>
#include<iostream>
using namespace std;
typedef long long ll;
const int mod=1000000007;
ll n,a0,ax,ay,b0,bx,by;
struct node
{
int n;
ll a[5][5];
node()
{
memset(a,0,sizeof(a));
}
}A,B;
node operator*(node a,node b)
{
node ans;
ans.n=a.n;
for(int i=0;i<a.n;i++)
for(int j=0;j<5;j++)
for(int k=0;k<5;k++)
ans.a[i][j]=((ans.a[i][j]+a.a[i][k]*b.a[k][j])%mod+mod)%mod;
return ans;
}
void aa(ll x)
{
node ans=A,p=B;
while(x)
{
if(x&1)
ans=ans*p;
p=p*p;
x=x/2;
}
ll k=(ans.a[0][4]%mod+mod)%mod;
cout<<k<<endl;
}
int main()
{
A.n=1;
B.n=5;
while(~scanf("%I64d",&n))
{
scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a0,&ax,&ay,&b0,&bx,&by);
A.a[0][0]=(a0%mod)*(b0%mod)%mod,A.a[0][1]=a0%mod,A.a[0][2]=b0%mod,A.a[0][3]=1,A.a[0][4]=0;
B.a[0][0]=(ax%mod)*(bx%mod)%mod,B.a[0][1]=0, B.a[0][2]=0, B.a[0][3]=0,B.a[0][4]=1;
B.a[1][0]=(ax%mod)*(by%mod)%mod,B.a[1][1]=ax%mod,B.a[1][2]=0, B.a[1][3]=0,B.a[1][4]=0;
B.a[2][0]=(ay%mod)*(bx%mod)%mod,B.a[2][1]=0, B.a[2][2]=bx%mod,B.a[2][3]=0,B.a[2][4]=0;
B.a[3][0]=(ay%mod)*(by%mod)%mod,B.a[3][1]=ay%mod,B.a[3][2]=by%mod,B.a[3][3]=1,B.a[3][4]=0;
B.a[4][0]=0, B.a[4][1]=0, B.a[4][2]=0, B.a[4][3]=0,B.a[4][4]=1;
// if(n==0)
// cout<<0<<endl;
// else
aa(n);
}
}