求调矩乘板子,样例可过
  • 板块学术版
  • 楼主__er
  • 当前回复4
  • 已保存回复4
  • 发布时间2023/2/16 19:51
  • 上次更新2023/10/24 00:38:24
查看原帖
求调矩乘板子,样例可过
713955
__er楼主2023/2/16 19:51

rt,斐波那契第n项的板子

//#pragma GCC optimize(3,"Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include <bits/stdc++.h>
#define JS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;

struct Matrix {
	int m[3][3];
};
Matrix a1, a2;
int n, m;

Matrix mat_mul(Matrix x, Matrix y) {
	Matrix res;
	memset(res.m, 0, sizeof(res.m));
	for (int i = 1; i <= 2; i++) {
		for (int j = 1; j <= 2; j++) {
			for (int k = 1; k <= 2; k++) {
				res.m[i][j] = (res.m[i][j] + (x.m[i][k] % m) * (y.m[k][j] % m)) % m;
			}
		}
	}
	return res;
}

Matrix mat_pow(int x) {
	Matrix t1, t2;
	t1.m[1][1] = 1, t1.m[1][2] = 0, t1.m[2][1] = 0, t1.m[2][2] = 1;
	t2.m[1][1] = 1, t2.m[1][2] = 1, t2.m[2][1] = 1, t2.m[2][2] = 0;
	while (x) {
		if (x & 1) {
			t1 = mat_mul(t1, t2);
		}
		x >>= 1, t2 = mat_mul(t2, t2);
	}
	return t1;
}

int main() {
	JS;
	cin >> n >> m;
	Matrix ans;
	ans = mat_pow(n - 1);
	cout << ans.m[1][1];
	return 0;
}
2023/2/16 19:51
加载中...