过了#1,3,7,8,11 其他WA了,求助
#include <iostream>
#include <cstring>
using namespace std;
const int N = 2;
long long mod;
struct matrix {
long long m[N][N];
matrix() {
memset(m, 0, sizeof(m));
}
};
matrix operator * (const matrix& a, const matrix& b) {
matrix c;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
c.m[i][j] = 0;
for (int k = 0; k < N; k++) {
c.m[i][j] = (c.m[i][j] + (a.m[i][k] % mod) * (b.m[k][j] % mod)) % mod;
}
}
}
return c;
}
matrix pow_matrix(matrix a, long long n) {
matrix ans;
for (int i = 0; i < N; i++) ans.m[i][i] = 1;
while (n) {
if (n & 1) ans = ans * a;
a = a * a;
n >>= 1;
}
return ans;
}
//test 2 3 1 2 30 23 out:7
int main() {
long long p, q, a1, a2, n, m;
scanf("%lld %lld %lld %lld %lld %lld", &p, &q, &a1, &a2, &n, &m);
mod = m;
a1 %= mod;
a2 %= mod;
if (n == 1) {
printf("%lld\n", a1 % mod);
return 0;
}
if (n == 2) {
printf("%lld\n", a2 % mod);
return 0;
}
matrix A;
A.m[0][0] = p % mod;
A.m[0][1] = 1;
A.m[1][0] = q % mod;
A.m[1][1] = 0;
matrix A_new = pow_matrix(A, n - 2);
long long result = (a2 * A_new.m[0][0] % mod + a1 * A_new.m[0][1] % mod) % mod;
printf("%lld\n", result);
return 0;
}