# Problem

## Description

$$n$$ 个在范围 $$[1,D]$$ 内的整数均匀随机变量。

## Range

$$0\le m\le 10^9,1\le n\le 10^9,1\le D\le 10^5$$

# Algorithm

$$FFT$$ ，生成函数

# Mentality

$$f_i$$ 为恰好有 $$i$$ 种颜色出现次数为奇数的方案数，则有：

$ans = \sum_{i=0}^{n-2m} f_i$

$$g_i = \tbinom{D}{i} h_i$$ ，也就是对于每种不同的钦定方案，我们将其求和。

$g_i=\sum \tbinom{j}{i} f_j$

$h_i = n![x^n](\frac{e^x-e^{-x}}{2})^i(e^x)^{D-i}\\ =\frac{n!}{2^i}[x^n]\sum_{j=0}^i \tbinom{i}{j} e^{jx}(-e^{-x})^{i-j}e^{D-i}\\ =\frac{n!}{2^i}\sum_{j=0}^i \tbinom{i}{j} (-1)^{i-j}[x^n]e^{D+2j-2i}\\ =\frac{n!}{2^i}\sum_{j=0}^i \tbinom{i}{i-j} (-1)^{j}[x^n]e^{D-2j}\\$

$h_i =\frac{n!}{2^i}\sum_{j=0}^i \tbinom{i}{i-j} (-1)^{j}\frac{(D-2j)^n}{n!}\\ =\frac{i!}{2^i}\sum_{j=0}^i \frac{1}{(i-j)!}\frac{(-1)^j(D-2j)^n}{j!}$

$f_i=\sum_{j=i}^{D} (-1)^{j-i} \tbinom{j}{i} g_j\\ = \sum_{j=0}^{D - i} (-1)^{j} \tbinom{j + i}{i} g_{j + i}\\ = \frac{1}{i!}\sum_{j=0}^{D - i} \frac{(-1)^{j}}{j!} (j+i)!g_{j + i}\\$

$$p_{D-j-i}=g_{j+i}(j+i)!$$，则有：

$f_i= \frac{1}{i!}\sum_{j=0}^{D - i} \frac{(-1)^{j}}{j!} p_{D-j-i}\\$

# Code

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
#define go(G, x, i, v) \
for (int i = G.hd[x], v = G.to[i]; i; v = G.to[i = G.nx[i]])
#define inline __inline__ __attribute__((always_inline))
LL x = 0, w = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') w = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * w;
}

const int Max_n = 8e5 + 5, mod = 998244353;

int D, n, m;
int fac[Max_n], ifac[Max_n];
int f[Max_n], g[Max_n], G[Max_n];

int ksm(int a, int b = mod - 2) {
int res = 1;
for (; b; b >>= 1, a = (LL)a * a % mod)
if (b & 1) res = (LL)res * a % mod;
return res;
}

namespace Poly {
int len, bit, rev[Max_n];
void init(int n) {
len = 1 << (bit = log2(n) + 1);
for (int i = 0; i < len; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << bit - 1);
}
void dft(int *f, bool t) {
for (int i = 0; i < len; i++)
if (rev[i] > i) swap(f[i], f[rev[i]]);
for (int l = 1; l < len; l <<= 1) {
int Wn = ksm(3, (mod - 1) / (l << 1));
if (t) Wn = ksm(Wn);
for (int i = 0; i < len; i += l << 1) {
int Wnk = 1;
for (int j = i; j < i + l; j++, Wnk = (LL)Wnk * Wn % mod) {
int x = f[j], y = (LL)f[j + l] * Wnk % mod;
f[j] = (x + y) % mod, f[j + l] = (x - y + mod) % mod;
}
}
}
if (t)
for (int i = 0, Inv = ksm(len); i < len; i++) f[i] = (LL)f[i] * Inv % mod;
}
void Mul(int *f, int *g, int N) {
init(N);
dft(f, 0), dft(g, 0);
for (int i = 0; i < len; i++) f[i] = (LL)f[i] * g[i] % mod;
dft(f, 1), dft(g, 1);
}
}  // namespace Poly
using namespace Poly;

namespace Input {
}  // namespace Input

namespace Init {
void main() {
fac[0] = 1;
for (int i = 1; i <= D; i++) fac[i] = (LL)fac[i - 1] * i % mod;
ifac[D] = ksm(fac[D]);
for (int i = D; i; i--) ifac[i - 1] = (LL)ifac[i] * i % mod;
}
}  // namespace Init

namespace Solve {
int C(int n, int m) {
if (n < m || m < 0) return 0;
return (LL)fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
void main() {
if (2 * m > n) {
puts("0");
return;
}
if (D <= n - 2 * m) {
printf("%d\n", ksm(D, n));
return;
}
for (int i = 0, t = 1; i <= D; i++, t *= -1) {
G[i] = ifac[i];
g[i] =
(LL)(t + mod) * ksm((D - 2 * i + mod) % mod, n) % mod * ifac[i] % mod;
}
Mul(G, g, D + 1 << 1);
for (int i = 0; i <= D; i++) G[i] = (LL)ksm(ksm(2, i)) * G[i] % mod * fac[i] % mod * C(D, i) % mod;
for (int i = 0, t = 1; i <= D; i++, t *= -1) {
g[i] = (LL)G[D - i] * fac[D - i] % mod;
f[i] = (LL)(t + mod) * ifac[i] % mod;
}
Mul(f, g, D + 1 << 1);
int ans = 0;
for (int i = 0; i <= n - 2 * m; i++)
(ans += (LL)f[D - i] * ifac[i] % mod) %= mod;
cout << ans << endl;
}
}  // namespace Solve

int main() {
#ifndef ONLINE_JUDGE
freopen("3120.in", "r", stdin);
freopen("3120.out", "w", stdout);
#endif
Input::main();
Init::main();
Solve::main();
}

posted @ 2020-05-08 20:31  洛水·锦依卫  阅读(170)  评论(0编辑  收藏  举报