您的位置:首页 > 其它

tjut 4686

2016-07-31 08:39 281 查看
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int V = 100000 + 50;
const int mod = 1000000000 + 7;
const int MaxN = 5;
struct Matrix{
__int64 mat[MaxN][MaxN];
};
Matrix init, Pow;
__int64 n, A0, Ax, Ay, B0, Bx, By;
Matrix multi(Matrix a, Matrix b) {
Matrix ans;
for(int i = 0; i < MaxN; ++i)
for(int j = 0; j < MaxN; ++j) {
__int64 sum = 0;
for(int k = 0; k < MaxN; ++k)
sum = (sum + a.mat[i][k] * b.mat[k][j] % mod) % mod;
ans.mat[i][j] = sum;
}
return ans;
}
Matrix MatrixQuickPow(Matrix a, __int64 b) {
Matrix ans = init;
while(b) {
if(b & 1)
ans = multi(ans, a);
b /= 2;
a = multi(a, a);
}
return ans;
}
int main() {
while(~scanf("%I64d", &n)) {
scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &A0, &Ax, &Ay, &B0, &Bx, &By);
if(!n) {
printf("0\n");
continue;
}
init.mat[0][0] = 1;
init.mat[0][1] = A0;
init.mat[0][2] = B0;
init.mat[0][3] = init.mat[0][4] = A0 * B0 % mod;
Pow.mat[0][0] = Pow.mat[4][4] = 1;
Pow.mat[0][1] = Ay;
Pow.mat[0][2] = By;
Pow.mat[0][3] = Pow.mat[0][4] = Ay * By % mod;
Pow.mat[1][1] = Ax;
Pow.mat[2][2] = Bx;
Pow.mat[1][3] = Pow.mat[1][4] = Ax * By % mod;
Pow.mat[2][3] = Pow.mat[2][4] = Ay * Bx % mod;
Pow.mat[3][3] = Pow.mat[3][4] = Ax * Bx % mod;
/*
for(int i = 0; i < MaxN; ++i) {
for(int j = 0; j < MaxN; ++j)
printf("%I64d ", Pow.mat[i][j]);
printf("\n");
}*/
Matrix ans = MatrixQuickPow(Pow, n - 1);
printf("%I64d\n", ans.mat[0][4]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: