矩阵加速球捉虫!悬1关,先到先得!
查看原帖
矩阵加速球捉虫!悬1关,先到先得!
804607
rainygame楼主2023/4/11 17:49
#include <bits/stdc++.h>
using namespace std;

int g;
long long n, m, a, c, x_0;
long long K[3][3] = {
	{0, 0, 0},
	{0, 0, 1},
	{0, 0, 1},
}, A[3][3], C[3][3], E[3][3];

long long qmul(long long a, long long b){
	a %= m;
	b %= m;
	long long ans = 0;
	while (b){
		if (b & 1) ans = (ans + a) % m;
		a = (a >> 1) % m;
		b >>= 1;
	}
	return ans;
}

void mul(long long A[3][3], long long B[3][3], int a, int b, int c){
	memset(C, 0, sizeof(C));
	
	for (int i=1; i<=a; i++){
		for (int j=1; j<=c; j++){
			for (int k=1; k<=b; k++) C[i][j] = (C[i][j] + qmul(A[i][k], B[k][j])) % m;
		}
	}
	
	memcpy(A, C, sizeof(C));
}

void qpow(long long A[3][3], long long k){
	memset(E, 0, sizeof(E));
	for (int i=1; i<=2; i++) E[i][i] = 1;
	
	while (k){
		if (k & 1) mul(E, A, 2, 2, 2);
		mul(A, A, 2, 2, 2);
		k >>= 1;
	}
	
	memcpy(A, E, sizeof(E));
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	cin >> m >> a >> c >> x_0 >> n >> g;
	
	x_0 %= m;
	a %= m;
	c %= m;
	
	if (n == 0){
		cout << x_0;
		return 0;
	}
	
	K[1][1] = a;
	
//	for (int i=1; i<=2; i++){
//		for (int j=1; j<=2; j++) cout << K[i][j] << ' ';
//		cout << '\n';
//	}
//	cout << '\n';
	qpow(K, n);
	
//	for (int i=1; i<=2; i++){
//		for (int j=1; j<=2; j++) cout << K[i][j] << ' ';
//		cout << '\n';
//	}
//	cout << '\n';
	
	A[1][1] = x_0;
	A[2][1] = c;
	mul(K, A, 2, 2, 1);
	
	cout << K[1][1] % g;
	
	return 0;
}

2023/4/11 17:49
加载中...