zoukankan      html  css  js  c++  java
  • Bzoj4710 分特产(容斥原理+组合数)

    题面

    Bzoj

    题解

    考虑容斥原理,所有人都有特产的方案数等于:

    至少零个人没有特产$-$至少一个人没有特产$+$至少两个人有特产$-...$

    接着考虑其中一种情况怎么求(假设现在至少有$i$个人没有特产):

    对于每种特产,我们分开考虑,假设当前特产有$a[j]$个,则我们可以看作是将$a[j]$个相同的球放入$n-i$个相同的盒子中,允许出现空盒(因为之前说的是至少),利用插板法,方案数为:$C_{n-i+a[j]-1}^{n-i-1}$

    最后当前情况的贡献绝对值就是,$C[n][i]$乘上每种特产的贡献之积,$C[n][i]$表示使得$n$个同学中的$i$个没有特产。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using std::min; using std::max;
    using std::swap; using std::sort;
    typedef long long ll;
    
    template<typename T>
    void read(T &x) {
        int flag = 1; x = 0; char ch = getchar();
        while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
        while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
    }
    
    const int N = 2e3 + 10, P = 1e9 + 7;
    int n, m, a[N], c[N][N], ret;
    
    int main () {
    	int lim = N - 10; c[0][0] = 1;
    	for(int i = 1; i <= lim; ++i) {
    		c[i][0] = c[i][i] = 1;
    		for(int j = 1; j < i; ++j)
    			c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % P;
    	}
    	read(n), read(m);
    	for(int i = 1; i <= m; ++i) read(a[i]);
    	for(int i = 0, gx = 1; i <= n; ++i) {
    		int dq = 1;
    		for(int j = 1; j <= m; ++j)
    			dq = 1ll * dq * c[n + a[j] - i - 1][n - i - 1] % P;
    		if(gx > 0) ret = (ret + 1ll * c[n][i] * dq % P) % P;
    		else ret = (ret + P - 1ll * c[n][i] * dq % P) % P;
    		gx = -gx;
    	} printf("%d
    ", ret);
        return 0;
    } 
    
  • 相关阅读:
    socket发送文字、图片、文件---基于python实现
    python socket详解
    loadrunner socket协议问题归纳(6)
    Hash算法解决冲突的四种方法
    while循环 运算符和编码
    python初识
    js中forEach,for in,for of循环的用法详解
    设计模式之MVC和MVT
    mac 下的 tree 命令 终端展示你的目录树结构
    Mac查看进程
  • 原文地址:https://www.cnblogs.com/water-mi/p/10328740.html
Copyright © 2011-2022 走看看