跳到正文

个人记录

好用的小玩意儿

liangbowen约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月13日
篇幅
约 4 分钟
获赞
2

版本

  1. v1当前
  • 简单组合数工具
C++
namespace Math {
	int qpow(int x, int y)
	{
		int ans = 1;
		while (y)
		{
			if (y & 1) ans = 1ll * ans * x % mod;
			x = 1ll * x * x % mod;
			y >>= 1;
		}
		return ans;
	}
	int inv(int x) {return qpow(x, mod - 2);}
	int fac[N], infac[N];
	void init(int n)
	{
		fac[0] = infac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
		infac[n] = inv(fac[n]); for (int i = n - 1; i; i--) infac[i] = 1ll * infac[i + 1] * (i + 1) % mod;
	}
	int C(int n, int m) {if (n < 0 || m < 0 || n - m < 0) return 0; return 1ll * fac[n] * infac[m] % mod * infac[n - m] % mod;}
	int A(int n, int m) {if (n < 0 || m < 0 || n - m < 0) return 0; return 1ll * fac[n] * infac[n - m] % mod;}
} using namespace Math;
  • 超级快读

    • return 0 记得改成 return flush(), 0。
    • 快读可以负数,但快写不支持负数,这个不难改,但我实在是太懒了。
C++
char ibuf[1 << 20], *is = ibuf, *it = ibuf;
char obuf[1 << 20], *os = obuf, *ot = obuf + (1 << 20);
#define getchar() is == it && (it = (is = ibuf) + fread(ibuf, 1, 1 << 20, stdin), is == it) ? EOF : *is++
#define flush() (fwrite(obuf, 1, os - obuf, stdout), os = obuf)
#define putchar(ch) *os++ = ch, os == ot ? flush() : 0
inline int read() {char op = getchar(); bool f = 0; int x = 0; while (op < 48 || op > 57) op = getchar(), f |= (op == '-'); while (48 <= op && op <= 57) x = (x << 1) + (x << 3) + (op ^ 48), op = getchar(); return f == 0 ? x : -x;}
inline long long Lread() {char op = getchar(); bool f = 0; long long x = 0; while (op < 48 || op > 57) op = getchar(), f |= (op == '-'); while (48 <= op && op <= 57) x = (x << 1) + (x << 3) + (op ^ 48), op = getchar(); return f == 0 ? x : -x;}
inline void write(long long x) {static int tmp[20], len = -1; do tmp[++len] = x % 10, x /= 10; while (x); while(~len) putchar(tmp[len--] + 48);}
  • 超级 GCD
C++
inline int gcd(int a, int b) {int az = __builtin_ctz(a), bz = __builtin_ctz(b), z = (az > bz) ? bz : az, t; b >>= bz; while (a) a >>= az, t = a - b, az = __builtin_ctz(t), a = t < 0 ? -t : t, b = a < b ? a : b; return b << z;}
  • 超级 rand()
C++
inline int rnd() {static int seed = 2333; return seed = (((seed * 666666ll + 20050818) % 998244353) ^ 1000000007) % 1004535809;}
原文