社区讨论

巨常数蒟蒻求优化

P3390【模板】矩阵快速幂参与者 2已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@mli0o715
此快照首次捕获于
2026/02/11 20:39
上周
此快照最后确认于
2026/02/13 20:15
6 天前
查看原帖
过了,但是……总耗时 2.13s2.13s,常数到底大在哪里?
CPP
#include<bits/stdc++.h>
#define inline __inline __attribute__((always_inline))
#define int long long
#define endl '\n'
using namespace std;
namespace IO {
	const int BUF_SIZE = 1 << 20;
	const int L = 20;
	char rbuf[BUF_SIZE], obuf[BUF_SIZE], b[L];
	int p1, p2, t;
	inline void refill() {
		fread(rbuf, 1, BUF_SIZE, stdin);
		p1 = 0;
	}
	inline char gc() {
		if(p1 == BUF_SIZE) refill();
		return rbuf[p1 ++];
	}
	inline int read() {
		int f = 1, x = 0;
		char ch = gc();
		while(!isdigit(ch)) { if(ch == '-') f = -f; ch = gc(); }
		while(isdigit(ch)) { x = x * 10 + (ch ^ 48); ch = gc(); }
		return f * x;
	}
	inline void flush() {
		fwrite(obuf, 1, p2, stdout);
		p2 = 0;
	}
	inline void pc(char c) {
		if(p2 == BUF_SIZE) flush();
		obuf[p2 ++] = c;
	}
	inline void write(int x) {
		t = 0;
		if(x < 0) pc('-'), x = -x;
		if(x == 0) { pc('0'); return; }
		while(x > 0) b[++ t] = x % 10 ^ 48, x /= 10;
		for(int i = t; i > 0; i--) pc(b[i]);
	}
}
const int N = 100 + 5;
const int mod = 1e9 + 7;
struct matrix { int m[N][N]; };
matrix operator * (matrix a, matrix b) {
	matrix c;
	for(int i = 1; i < N; i++) 
		for(int j = 1; j < N; j++) c.m[i][j] = 0;
	for(int i = 1; i < N; i++)
		for(int j = 1; j < N; j++)
			for(int k = 1; k < N; k++)
				c.m[i][j] = (a.m[i][k] * b.m[k][j] + c.m[i][j]) % mod;
	return c;
}
matrix matrix_pow(matrix a, int b) {
	matrix res;
	for(int i = 1; i < N; i++)
		for(int j = 1; j < N; j++)
			res.m[i][j] = (i == j) ? 1 : 0;
	while(b > 0) {
		if(b & 1) res = res * a;
		a = a * a;
		b >>= 1;
	}
	return res;
}
matrix a;
int n, k;
signed main() {
	n = IO :: read(), k = IO :: read();
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++) a.m[i][j] = IO :: read();
	a = matrix_pow(a, k);
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++) IO :: write(a.m[i][j]), IO :: pc(' ');
		IO :: pc('\n');
	}
	IO :: flush();
	return 0;
}

回复

4 条回复,欢迎继续交流。

正在加载回复...