題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4686
根據題意可得:
遞推矩陣如下:
代碼如下:
#include <cstdio>
#include <cstring>
const int mod = 1000000007;
const int matSize = 5;
long long n, a0, ax, ay, b0, bx, by;
struct Mat
{
long long val[matSize][matSize];
void init() { memset(val,0,sizeof(val)); }
void set1() { // 把矩陣置爲單位矩陣
for(int i=0; i<matSize; i++)
for(int j=0; j<matSize; j++)
val[i][j] = (i == j);
}
friend Mat operator*(Mat a, Mat b) // 重載*號進行矩陣乘法
{
Mat res;
res.init();
for(int i=0; i<matSize; i++) {
for(int k=0; k<matSize; k++) {
if(a.val[i][k]) {
for(int j=0; j<matSize; j++) {
res.val[i][j] += a.val[i][k]*b.val[k][j];
res.val[i][j] %= mod;
}
}
}
}
return res;
}
friend Mat operator^(Mat a, long long x) // 重載^號進行快速冪運算
{
Mat res;
res.set1();
while(x)
{
if(x & 1) res = res * a;
a = a * a;
x >>= 1;
}
return res;
}
};
Mat base;
Mat A;
Mat B;
void init()
{
base.val[0][0] = 1;
base.val[1][0] = a0 % mod;
base.val[2][0] = b0 % mod;
base.val[3][0] = a0 * b0 % mod;
}
void init1()
{
A.val[0][0] = 1;
A.val[1][0] = ay % mod;
A.val[1][1] = ax % mod;
A.val[2][0] = by % mod;
A.val[2][2] = bx % mod;
A.val[3][0] = ay * by % mod;
A.val[3][1] = ax * by % mod;
A.val[3][2] = ay * bx % mod;
A.val[3][3] = ax * bx % mod;
A.val[4][3] = 1;
A.val[4][4] = 1;
}
int main()
{
while(~scanf("%I64d", &n)) {
scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &a0, &ax, &ay, &b0, &bx, &by);
base.init();
init();
A.init();
init1();
B = (A^n) * base;
printf("%I64d\n", B.val[4][0]);
}
return 0;
}