求助,甚至样例过不了
查看原帖
求助,甚至样例过不了
612722
JustPureH2O楼主2024/1/23 20:49
#include <bits/stdc++.h>
#define N 15
using namespace std;

typedef long long ll;

ll MOD;

struct Matrix {
	ll mat[N][N];
	
	Matrix() {
		memset(mat, 0, sizeof mat);
	}	
	
	void I() {
		for (int i = 1; i <= 2; i++) mat[i][i] = 1;
	}
};

ll qmul(ll a, ll b) {
	ll res = 0;
	while (b) {
		if (b & 1) res = (res + a) % MOD;
		a = (a + a) % MOD;
		b >>= 1;
	}
	return res;
}

Matrix operator *(const Matrix &l, const Matrix &r) {
	Matrix res;
	for (int k = 1; k <= 2; k++) {
		for (int i = 1; i <= 2; i++) {
			for (int j = 1; j <= 2; j++) {
				res.mat[i][j] += qmul(l.mat[i][k], r.mat[k][j]) % MOD;
			}
		}
	}	
	return res;
}

Matrix qpow(Matrix a, ll b) {
	Matrix res;
	res.I();
	while (b) {
		if (b & 1) res = res * a;
		a = a * a;
		b >>= 1;
	}
	return res;
}

//void out(Matrix m) {
//	for (int i = 1; i <= 2; cout<<endl, i ++) {
//		for (int j = 1; j <= 2; j++) {
//			cout<<m.mat[i][j]<<' ';
//		}
//	}	
//}

int main() {
	ll a, c, x0, n, g;
	cin>>MOD>>a>>c>>x0>>n>>g;
	Matrix A, M;
	A.mat[1][1] = x0;
	A.mat[1][2] = c;
	M.mat[1][1] = a;
	M.mat[2][1] = M.mat[2][2] = 1;
	A = A * qpow(M, n);
	cout<<(A.mat[1][1] % g)<<endl;
	return 0;
}
2024/1/23 20:49
加载中...