「PKUWC2018」猎人杀
题面
题解
考虑开枪时,如果打到死掉的猎人就再来一枪
而不是不能打死掉的猎人
假设$A$集合中$(1\notin A)$所有都在$1$之后死
设$\sum_{i=1}^nw[i]=S,\sum_{i\in a}w[i]=T$,则概率为$\frac {w_1}{w_1+T}$
前面的容斥系数可以用生成函数$\prod_{i>1}(1-x^{w_i})$求
这个东西可以分治$\text{+FFT}$
直到现在我才知道分治$\text{FFT}$居然可以这样写(详见代码)
但是由于使用过量$\text{STL}$,常数巨大。。。
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
inline int read()
{
int data = 0, w = 1;
char ch = getchar();
while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if(ch == '-') w = -1, ch = getchar();
while(ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return data*w;
}
const int maxn(100010), Mod(998244353), g(3), Invg(332748118);
int fastpow(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;
}
typedef std::vector<int> vector;
vector ploy[maxn << 1], *l, *r, R;
template<int opt> void FFT(vector &p, int n)
{
for(RG int i = 1; i < n; i++) if(i < R[i]) std::swap(p[i], p[R[i]]);
for(RG int i = 1; i < n; i <<= 1)
{
int rot = fastpow(opt == 1 ? g : Invg, (Mod - 1) / (i << 1));
for(RG int j = 0; j < n; j += (i << 1))
{
int w = 1;
for(RG int k = 0; k < i; ++k, w = 1ll * w * rot % Mod)
{
int x = p[j + k], y = 1ll * w * p[i + j + k] % Mod;
p[j + k] = (x + y) % Mod, p[i + j + k] = (x - y + Mod) % Mod;
}
}
}
}
void Mul(vector &a, vector &b, vector &c)
{
int newL = a.size() + b.size() - 1, N = 1, P;
for(P = 0; N < newL; N <<= 1, ++P);
a.resize(N); b.resize(N);
c.resize(N); R.resize(N);
for(RG int i = 1; i < N; i++)
R[i] = (R[i >> 1] >> 1) | ((i & 1) << (P - 1));
FFT<1>(a, N); FFT<1>(b, N);
for(RG int i = 0; i < N; i++) c[i] = 1ll * a[i] * b[i] % Mod;
FFT<-1>(c, N); a.clear(); b.clear();
for(RG int i = 0, inv = fastpow(N, Mod - 2); i < N; i++)
c[i] = 1ll * c[i] * inv % Mod;
}
int w[maxn], n;
int main()
{
n = read(); if(n == 1) return puts("1"), 0;
for(RG int i = 1; i <= n; i++)
ploy[i].resize((w[i] = read()) + 1),
ploy[i][0] = 1, ploy[i][w[i]] = Mod - 1;
l = ploy + 2, r = ploy + n;
while(l != r) // magic! It means Div + FFT!!!
{
++r; Mul(*l, *(l + 1), *r);
l += 2;
}
int ans = 0;
for(RG int i = r -> size() - 1; ~i; i--)
ans = (ans + 1ll * *(r -> begin() + i) *
fastpow((i + w[1]) % Mod, Mod - 2) % Mod) % Mod;
printf("%lld\n", 1ll * ans * w[1] % Mod);
return 0;
}