题面
解法
矩阵乘法sb题
注意整数乘法要使用龟速乘,否则会爆long long
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct Matrix {
int a[4][4];
void Clear() {memset(a, 0, sizeof(a));}
};
int n, m, a, c, x, g;
int mul(int x, int y) {
int ret = 0;
while (y) {
if (y & 1) ret = (ret + x) % m;
y >>= 1, x = (x + x) % m;
}
return ret;
}
Matrix operator * (Matrix x, Matrix y) {
Matrix ret; ret.Clear();
for (int k = 1; k <= 2; k++)
for (int i = 1; i <= 2; i++)
for (int j = 1; j <= 2; j++)
ret.a[i][j] = (ret.a[i][j] + mul(x.a[i][k], y.a[k][j])) % m;
return ret;
}
Matrix operator ^ (Matrix x, int y) {
Matrix ret = x; y--;
while (y) {
if (y & 1) ret = ret * x;
y >>= 1, x = x * x;
}
return ret;
}
main() {
cin >> m >> a >> c >> x >> n >> g;
Matrix tx, ret; tx.Clear();
tx.a[1][1] = a, tx.a[1][2] = c;
tx.a[2][1] = 0, tx.a[2][2] = 1;
ret.a[1][1] = x, ret.a[2][1] = 1;
tx = tx ^ n; ret = tx * ret;
cout << ret.a[1][1] % g << "\n";
return 0;
}
原文地址:https://www.cnblogs.com/copperoxide/p/9476730.html
时间: 2024-10-11 15:05:36