20pts求助 QWQ
查看原帖
20pts求助 QWQ
678858
ShiRoZeTsuHL卜奎BBQ!楼主2023/6/8 10:23

不知道为什么20pts,我感觉我写对了啊

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
ll mod;
ll p, q, a1, a2, n;

struct MATRIX {
	ll mtx[4][4];
	ll g, h;
	MATRIX() { memset(mtx, 0, sizeof(mtx)); g = h = 0; }
	void operator *= (const MATRIX& b) {
		MATRIX res; res.g = res.h = 2;
		for(int k = 1; k <= h; k++)
		for(int i = 1; i <= g; i++)
		for(int j = 1; j <= b.h; j++)
			res.mtx[i][j] = (res.mtx[i][j] + 1ll*mtx[i][k]*b.mtx[k][j]%mod + mod) % mod;
		*this = res;
	}
	void operator ^= (ll x) {
		MATRIX res;
		res.g = res.h = 2; res.mtx[2][1] = res.mtx[1][2] = 1;
		while(x) {
			if(x&1) res *= (*this);
			(*this) *= (*this);
			x >>= 1;
		}
		*this = res;
	}
};

int main() {
	scanf("%lld %lld %lld %lld %lld %lld", &p, &q, &a1, &a2, &n, &mod);
	if(n == 1) {
		printf("%lld\n", a1);
		return 0;
	}
	if(n == 2) {
		printf("%lld\n", a2);
		return 0;
	}
	
	MATRIX a;
	a.g = 1, a.h = 2;
	a.mtx[1][1] = a1, a.mtx[1][2] = a2;
	
	MATRIX b;
	b.g = 2, b.h = 2;
	b.mtx[1][1] = 0, b.mtx[1][2] = q;
	b.mtx[2][1] = 1, b.mtx[2][2] = p;
	
	b ^= (n-2);
	a *= b;
	
	printf("%lld\n", a.mtx[1][2]);
	return 0;
}
2023/6/8 10:23
加载中...