zoukankan      html  css  js  c++  java
  • loj2541 「PKUWC2018」猎人杀 【容斥 + 分治NTT】

    题目链接

    loj2541

    题解

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

    觉得十分难求,考虑容斥
    由于(1)号可能不是最后一个被杀的,我们容斥一下(1)号之后至少有几个没被杀
    我们令(A = sumlimits_{i = 1}^{n} w_i),令(S)表示选出那几个在(i)之后的(w_i)
    我们淘汰人之后概率的分母就改变了,很不好求
    我们考虑转化一下问题,每个人被杀后依旧存在,只不过再次选中他时再选一次,是等价的
    那么此时那几个人在(1)之后的概率

    [egin{aligned} P &= sumlimits_{i = 0}^{infty} (1 - frac{S + w_1}{A})^{i} frac{w_1}{A} \ &= frac{w_1}{A}sumlimits_{i = 0}^{infty} (1 - frac{S + w_1}{A})^{i} \ &= frac{w_1}{A} imes 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}))
    那么我们只需求出(prodlimits_{i = 2}^{n} (1 - x^{w_i}))
    (S)的系数就是(x^{S})的系数
    分治(NTT)即可
    (m = sumlimits_{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
    ",ans);
    	return 0;
    }
    
    
  • 相关阅读:
    锻炼记录
    PHP学习笔记
    返回一个整数数组中最大子数组的和(2)
    四则运算的在线答题(判断对错,记录错题)
    返回一个二维整数数组的最大子数组的和
    返回一个整数数组中最大子数组的和
    程序2:支持真分数的四则运算
    程序1:四则运算
    四则运算3.0版本
    返回二维数组子数组和最大值
  • 原文地址:https://www.cnblogs.com/Mychael/p/9185153.html
Copyright © 2011-2022 走看看