博主的腦抽日常

A-Problem-Solver發表於2024-11-17

線性求逆元:

ll fac[3000005], inv[3000005], ans[3000005];

ll qpow(ll a, ll x, ll m) {
	ll ans = 1;
	while (x) {
		if (x & 1) {
			ans = ans * a % m;
		}
		x >>= 1;
		a = a * a % m;
	}
	return ans;
}

int main() {
	int n;
	ll p;
	scanf("%d %lld", &n, &p);
	fac[0] = 1;
	for (int i = 1; i <= n; i++) {
		fac[i] = fac[i - 1] * i % p;
	}
	inv[n] = qpow(fac[n], p - 2, p);
	for (int i = n - 1; i >= 0; i--) {
		inv[i] = inv[i + 1] * (i + 1) % p;
	}
	for (int i = 1; i <= n; i++) {
		ans[i] = fac[i - 1] * inv[i] % p;
		printf("%lld\n", ans[i]);
	}
}

相關文章