矩阵快速幂只过了#1
查看原帖
矩阵快速幂只过了#1
1063896
Mickey_miqi楼主2024/8/26 22:25

转移矩阵为 1 x0 乘 1 c 的n方 0 a

#include <bits/stdc++.h>
using namespace std;
long long n, m, a, c, xx, g;
struct matrix {
	long long c[2][2];
	matrix() {memset(c, 0, sizeof(c));}
} A, res;
long long mul(long long a, long long b) {
	long long ret = 0;
	while(b)
	{
		if(b & 1) ret = (ret+a)%g;
		b >>= 1;
		a = a*2%g;
	}
	return ret;
}
matrix operator* (matrix &x, matrix &y) {
	matrix t;
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 2; j++)
			for (int k = 0; k < 2; k++) 
				t.c[i][j] = (t.c[i][j]+mul(x.c[i][k], y.c[k][j]))%g;
	return t;
}
void quickpow() {
	A.c[0][0] = 1, A.c[0][1] = c, A.c[1][1] = a;
	for (int i = 0; i < 2; i++) res.c[i][i] = 1;
	while (n) {
		if (n & 1) res = res*A;
		n >>= 1;
		A = A*A; 
	}
}
int main() {
	cin >> m >> a >> c >> xx >> n >> g;
	quickpow();
	long long ans = (res.c[0][1]+res.c[1][1]*xx)%g;
	printf("%lld", ans);
	return 0;
}

2024/8/26 22:25
加载中...