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?
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.
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
import java.util.Arrays;
import java.util.Scanner;
public class Main{
static long N;
static long AO,AX,AY;
static long BO,BX,BY;
static long inf=1000000007;
static G_ multimod(G_ a,G_ b){//矩阵乘法
G_ res=new G_();
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
for(int k=0;k<5;k++){
res.dd[i][j]=(res.dd[i][j]+a.dd[i][k]*b.dd[k][j]%inf)%inf;
}
}
}
return res;
}
static G_ multi(G_ a,G_ b){//快速幂
G_ ans=new G_();
ans=b;
long n=N-1;
while(n>0){
if((n&1)==1){ans=multimod(a,ans); }
n>>=1;
a=multimod(a,a);
}
return ans;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
N=sc.nextLong();
AO=sc.nextLong();
AX=sc.nextLong();
AY=sc.nextLong();
BO=sc.nextLong();
BX=sc.nextLong();
BY=sc.nextLong();
if(N==0){
System.out.println("0");
continue;
}
G_ A=new G_();
long a1=AX*BX%inf;
long a2=AX*BY%inf;
long a3=AY*BX%inf;
long a4=AY*BY%inf;
long a5=AX%inf;
long a6=AY%inf;
long a7=BX%inf;
long a8=BY%inf;//初始化矩阵Q
A.dd[0][0]=1;A.dd[0][1]=a1;A.dd[0][2]=a2;A.dd[0][3]=a3;A.dd[0][4]=a4;
A.dd[1][0]=0;A.dd[1][1]=a1;A.dd[1][2]=a2; A.dd[1][3]=a3; A.dd[1][4]=a4;
A.dd[2][0]=0;A.dd[2][1]=0;A.dd[2][2]=a5;A.dd[2][3]=0; A.dd[2][4]=a6;
A.dd[3][0]=0;A.dd[3][1]=0;A.dd[3][2]=0; A.dd[3][3]=a7; A.dd[3][4]=a8;
A.dd[4][0]=0;A.dd[4][1]=0;A.dd[4][2]=0; A.dd[4][3]=0; A.dd[4][4]=1;
G_ B=new G_();
B.dd[0][0]=AO*BO%inf;//初始化矩阵p
B.dd[1][0]=AO*BO%inf;
B.dd[2][0]=AO%inf;
B.dd[3][0]=BO%inf;
B.dd[4][0]=1;
G_ res=new G_();
res= multi(A,B);
System.out.println(res.dd[0][0]);
}
}
}
class G_{
long dd[][]=new long[5][5];
G_(){
for(int i=0;i<5;i++)
Arrays.fill(dd[i],0);
}
}