题意:根据题目给出的公式,求最后的结果。
思路:看到递推,就去想矩阵快速幂吧。首先要把AoD(n) = AoD(n-1) + an-2 * bn-2 * AX * BX + an-2 * AX * BY + bn-2 * AY * BX + AY * BY;
可以看到,每次递推需要的项有 AoD, an * bn, an,bn.
这样,我们构造矩阵,分别表示他们的递推关系即可。
1 1 0 0 0
0 AX*BX AX*BY AY*BX AY*BY
0 0 AX 0 AY
0 0 0 BX BY
0 0 0 0 1
注意:需要注意的一点是,数据类型为long long,否则会爆int,在初始化的时候也要取模
代码如下:
#include <cstdio>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
const int MOD = 1000000007;
mat mul(mat & A, mat &B)
{
mat C(A.size(), vec(B[0].size()));
for (int i = 0; i < A.size(); ++i)
for (int k = 0; k < B.size(); k++)
for (int j = 0; j < B[0].size(); ++j)
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
return C;
}
mat pow(mat A, ll n)
{
mat B(A.size(), vec(A.size()));
for (int i = 0; i < A.size(); ++i)
B[i][i] = 1;
while (n){
if (n & 1) B = mul(B, A);
A = mul(A, A);
n >>= 1;
}
return B;
}
ll N,A0,AX,AY,B0,BX,BY;
int main(void)
{
//freopen("input.txt","r",stdin);
while(scanf("%I64d",&N) != EOF){
scanf("%I64d%I64d%I64d",&A0,&AX,&AY);
scanf("%I64d%I64d%I64d",&B0,&BX,&BY);
mat A(5,vec(5));
A[0][0] = A[0][1] = A[4][4] = 1;
A[1][1] = AX*BX % MOD, A[1][2] = AX*BY % MOD,A[1][3] = AY*BX % MOD,A[1][4] =AY*BY % MOD;
A[2][2] = AX, A[2][4] = AY;
A[3][3] = BX, A[3][4] = BY;
A = pow(A,N);
mat B(5,vec(1));
B[1][0] = A0 * B0 % MOD , B[2][0] = A0 % MOD, B[3][0] =B0 % MOD, B[4][0] = 1;
B = mul(A,B);
printf("%I64d\n",B[0][0]);
}
return 0;
}