社区讨论

不会卡常的看这里

P15343「RedStone OI R1 C」Super Fib参与者 6已保存回复 6

讨论操作

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

当前回复
6 条
当前快照
1 份
快照标识符
@mlp1a5es
此快照首次捕获于
2026/02/16 18:30
3 天前
此快照最后确认于
2026/02/17 09:52
前天
查看原帖
  • 稀疏矩阵乘法优化(为 00 就直接跳过):
CPP
mat operator * (const mat& x, const mat& y) {
	mat z; z.clear();
	for (int i = 0; i < 7; i++)
		for (int k = 0; k < 7; k++) if (x.c[i][k]) // HERE
			for (int j = 0; j < 7; j++) if (y.c[k][j])  { // HERE
				mx.x = x.c[i][k], my.x = y.c[k][j];
				z.c[i][j] += (mx * my).x;
				if (z.c[i][j] >= mod) z.c[i][j] -= mod;
			}
	return z;
}
  • Barrett 约乘板子
Code & Example
具体用法写在代码里了。
CPP
#include <bits/stdc++.h>
using namespace std;
struct Barrett {
	unsigned int m;
	unsigned long long im;
	Barrett(unsigned int m = 998244353) : m(m), im((unsigned long long)(-1) / m + 1) {}
	inline unsigned int umod() const { return m; }
	inline unsigned int mul(unsigned int a, unsigned int b) const {
		unsigned long long z = (unsigned long long)a * b;
		unsigned long long x = (unsigned long long)(((unsigned __int128)z * im) >> 64);
		unsigned long long v = z - x * m;
		if (m <= v) v += m;
		return (unsigned int)v;
	}
};
static Barrett mod_ctx;
struct mint {
	unsigned int x;
	mint() : x(0) {}
	mint(int a) : mint((long long)a) {}
	mint(long long a) {
		long long m = mod_ctx.umod();
		x = (unsigned int)((a % m + m) % m);
	}
	mint(unsigned long long a) : x((unsigned int)(a % mod_ctx.umod())) {}
	mint(__int128 a) {
		unsigned long long m = mod_ctx.umod();
		a %= m;
		if (a < 0) a += m;
		x = (unsigned int)a;
	}
	static void set_mod(unsigned int m) { mod_ctx = Barrett(m); }
	static unsigned int mod() { return mod_ctx.umod(); }
	unsigned int val() const { return x; }
	explicit operator unsigned int() const { return x; }
	explicit operator long long() const { return x; }
	mint& operator+=(const mint &a) {
		x += a.x;
		if (x >= mod_ctx.umod()) x -= mod_ctx.umod();
		return *this;
	}
	mint& operator-=(const mint &a) {
		if (x < a.x) x += mod_ctx.umod();
		x -= a.x;
		return *this;
	}
	mint& operator*=(const mint &a) {
		x = mod_ctx.mul(x, a.x);
		return *this;
	}
	mint& operator/=(const mint &a) { return *this *= a.inv(); }
	mint operator+() const { return *this; }
	mint operator-() const { return mint() - *this; }
	mint& operator++() {
		x++; if (x == mod_ctx.umod()) x = 0;
		return *this;
	}
	mint& operator--() {
		if (x == 0) x = mod_ctx.umod();
		x--;
		return *this;
	}
	mint operator++(int) { mint r = *this; ++*this; return r; }
	mint operator--(int) { mint r = *this; --*this; return r; }
	friend mint operator+(const mint &a, const mint &b) { return mint(a) += b; }
	friend mint operator-(const mint &a, const mint &b) { return mint(a) -= b; }
	friend mint operator*(const mint &a, const mint &b) { return mint(a) *= b; }
	friend mint operator/(const mint &a, const mint &b) { return mint(a) /= b; }
	friend bool operator==(const mint &a, const mint &b) { return a.x == b.x; }
	friend bool operator!=(const mint &a, const mint &b) { return a.x != b.x; }
	mint pow(long long t) const {
		mint res = 1, a = *this;
		if (t < 0) { a = a.inv(); t = -t; }
		while (t > 0) {
			if (t & 1) res *= a;
			a *= a; t >>= 1;
		}
		return res;
	}
	mint inv() const {
		long long a = x, b = mod_ctx.umod(), u = 1, v = 0;
		while (b) {
			long long t = a / b;
			a -= t * b; swap(a, b);
			u -= t * v; swap(u, v);
		}
		if (u < 0) u += mod_ctx.umod();
		return mint(u);
	}
	friend istream& operator>>(istream& is, mint& a) {
		long long t; is >> t; a = mint(t); return is;
	}
	friend ostream& operator<<(ostream& os, const mint& a) {
		return os << a.x;
	}
};

// --- Example Usage ---
int main() {
	long long P, A, B;
	if (cin >> P >> A >> B) {
		mint::set_mod(P);
		mint a = A;
		mint b = B;
		cout << "Mod: " << mint::mod() << "\n";
		cout << "a = " << a << ", b = " << b << "\n";
		cout << "a + b = " << (a + b) << "\n";
		cout << "a - b = " << (a - b) << "\n";
		cout << "a * b = " << (a * b) << "\n";
		if (gcd(b.val(), P) == 1) {
			 cout << "a / b = " << (a / b) << "\n";
		} else {
			 cout << "a / b = Undefined (gcd != 1)\n";
		}
		cout << "a ^ 10 = " << a.pow(10) << "\n";
	}
	return 0;
}

回复

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

正在加载回复...