矩阵快速幂:
根据关系够建矩阵 , 快速幂解决.

<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPGgxPgpBcmMgb2YgRHJlYW08L2gxPgo8c3Ryb25nPlRpbWUgTGltaXQ6IDIwMDAvMjAwMCBNUyAoSmF2YS9PdGhlcnMpICAgIE1lbW9yeSBMaW1pdDogNjU1MzUvNjU1MzUgSyAoSmF2YS9PdGhlcnMpPGJyPgpUb3RhbCBTdWJtaXNzaW9uKHMpOiAyMTY0ICAgIEFjY2VwdGVkIFN1Ym1pc3Npb24ocyk6IDY4MDxicj4KPC9zdHJvbmc+PGJyPgo8YnI+CgpQcm9ibGVtIERlc2NyaXB0aW9uCgpBbiBBcmMgb2YgRHJlYW0gaXMgYSBjdXJ2ZSBkZWZpbmVkIGJ5IGZvbGxvd2luZyBmdW5jdGlvbjo8YnI+CjxjZW50ZXI+PGltZyBzcmM9"https://www.cppentry.com/upload_files/article/49/1_wyldc__.jpg" alt="\">
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 2013 Multi-University Training Contest 9
#include#include #include #include #include using namespace std; typedef long long int LL; const LL mod=1000000007LL; struct Matrix { int x,y; LL m[6][6]; Matrix() {x=y=5;memset(m,0,sizeof(m));} void one() { for(int i=0;i<5;i++) m[i][i]=1LL; } void show() { cout< >n) { cin>>A0>>AX>>AY>>B0>>BX>>BY; A0=A0%mod; AX=AX%mod; AY=AY%mod; B0=B0%mod; BX=BX%mod; BY=BY%mod; Matrix m=init_matrix(); m=quickPow(m,n); Matrix beg=Beg(); LL ans=0; for(int i=0;i<5;i++) ans=(ans+beg.m[i][0]*m.m[4][i]%mod)%mod; cout<