首页 技术 正文
技术 2022年11月15日
0 收藏 373 点赞 3,450 浏览 2493 个字

题目链接

loj2541

题解

思路很妙啊, 人傻想不到啊

觉得十分难求,考虑容斥

由于\(1\)号可能不是最后一个被杀的,我们容斥一下\(1\)号之后至少有几个没被杀

我们令\(A = \sum\limits_{i = 1}^{n} w_i\),令\(S\)表示选出那几个在\(i\)之后的\(w_i\)和

我们淘汰人之后概率的分母就改变了,很不好求

我们考虑转化一下问题,每个人被杀后依旧存在,只不过再次选中他时再选一次,是等价的

那么此时那几个人在\(1\)之后的概率

\[\begin{aligned}
P &= \sum\limits_{i = 0}^{\infty} (1 – \frac{S + w_1}{A})^{i} \frac{w_1}{A} \\
&= \frac{w_1}{A}\sum\limits_{i = 0}^{\infty} (1 – \frac{S + w_1}{A})^{i} \\
&= \frac{w_1}{A} \times \frac{1}{1 – 1 + \frac{S + w_1}{A}} \\
&= \frac{w_1}{S + w_1}
\end{aligned}
\]

我们只需求出所有组合下该式的值即可

但这样显然很暴力

考虑到题目中\(\sum w_i \le 10^5\)的条件

我们求出各个\(S\)的系数和

注意到\(S\)是由\(w_i\)组合而成的,每有个\(w_i\)就乘上一个\(-1\)

容易发现每个\(w_i\)可以写成生成函数\((1 – x^{w_i})\)

那么我们只需求出\(\prod\limits_{i = 2}^{n} (1 – x^{w_i})\)

\(S\)的系数就是\(x^{S}\)的系数

分治\(NTT\)即可

令\(m = \sum\limits_{i = 2}^{n} w_i\),分治每一层的复杂度为\(O(mlogm)\)

总复杂度\(O(mlog^2m)\)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 400005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
const int G = 3,P = 998244353;
inline int qpow(int a,LL b){
int re = 1;
for (; b; b >>= 1,a = 1ll * a * a % P)
if (b & 1) re = 1ll * re * a % P;
return re;
}
int R[maxn];
inline void NTT(int* a,int n,int f){
for (int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
for (int i = 1; i < n; i <<= 1){
int gn = qpow(G,(P - 1) / (i << 1));
for (int j = 0; j < n; j += (i << 1)){
int g = 1,x,y;
for (int k = 0; k < i; k++,g = 1ll * g * gn % P){
x = a[j + k],y = 1ll * g * a[j + k + i] % P;
a[j + k] = (x + y) % P,a[j + k + i] = ((x - y) % P + P) % P;
}
}
}
if (f == 1) return;
int nv = qpow(n,P - 2); reverse(a + 1,a + n);
for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P;
}
int A[30][maxn],deg[maxn],cnt;
int n,w[maxn];
void solve(int l,int r){
if (l == r){
++cnt;
deg[cnt] = w[l];
A[cnt][0] = 1; A[cnt][w[l]] = -1;
for (int i = 1; i < w[l]; i++) A[cnt][i] = 0;
return;
}
int mid = l + r >> 1;
solve(l,mid); solve(mid + 1,r);
int n = 1,L = 0,a = cnt - 1,b = cnt,m = deg[a] + deg[b];
while (n <= m) n <<= 1,L++;
for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
for (int i = deg[a] + 1; i < n; i++) A[a][i] = 0;
for (int i = deg[b] + 1; i < n; i++) A[b][i] = 0;
NTT(A[a],n,1); NTT(A[b],n,1);
for (int i = 0; i < n; i++) A[a][i] = 1ll * A[a][i] * A[b][i] % P;
NTT(A[a],n,-1);
cnt--; deg[cnt] = m;
}
int main(){
n = read();
if (n == 1){puts("1"); return 0;}
int sum = 0,ans = 0;
REP(i,n) w[i] = read(),sum += w[i]; sum -= w[1];
solve(2,n);
for (int i = 0; i <= sum; i++)
ans = (ans + 1ll * w[1] * A[1][i] % P * qpow(i + w[1],P - 2) % P) % P;
ans = (ans + P) % P;
printf("%d\n",ans);
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,122
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,594
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,439
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,210
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,846
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,931