BZOJ4710 [Jsoi2011]分特产 容斥
2024-09-06 01:41:26
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=4710
题解
本来想去找一个二项式反演的题的,结果被 https://www.cnblogs.com/GXZlegend/p/11407185.html 骗了,给的最后一道题是一个基础容斥的题。
(不过反演的本质就是容斥呢,如果二项式反演的 \(g(n)\) 的 \(n\) 是 \(0\) 的话也就跟最常见的容斥差不多了)
考虑如果钦定有 \(k\) 个同学没有拿到特产,那么特产中剩下的同学就可以随便分了。对于第 \(i\) 个特产,划分给 \(n-k\) 个同学的方案数显然就是插板法一下就没了,\(\binom {a_i+n-k-1}{n-k-1}\)。
然后容斥上一下就可以了。最后答案就是
\[\sum_{i=0}^n(-1)^i\binom ni \prod_{j=1}^m \binom{a_j+n-i-1}{n-i-1}
\]
\]
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I>
inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
const int N = 2000 + 7;
const int P = 1e9 + 7;
int n, m, mxa;
int a[N];
inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
int ans = 1;
for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
return ans;
}
int fac[N], inv[N], ifac[N];
inline void ycl(const int &n = ::n) {
fac[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = (ll)fac[i - 1] * i % P;
inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = (ll)(P - P / i) * inv[P % i] % P;
ifac[0] = 1; for (int i = 1; i <= n; ++i) ifac[i] = (ll)ifac[i - 1] * inv[i] % P;
}
inline int C(int x, int y) {
if (x < y) return 0;
return (ll)fac[x] * ifac[y] % P * ifac[x - y] % P;
}
inline void work() {
ycl(n + mxa);
int ans = 0;
for (int i = 0; i <= n; ++i) {
int f = C(n, i);
if (i & 1) f = P - f;
for (int j = 1; j <= m; ++j) f = (ll)f * C(a[j] + n - i - 1, n - i - 1) % P;
sadd(ans, f);
}
printf("%d\n", ans);
}
inline void init() {
read(n), read(m);
for (int i = 1; i <= m; ++i) read(a[i]), smax(mxa, a[i]);
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
最新文章
- 将Centos的yum源更换为国内的阿里云源
- 医学CT图像特征提取算法(matlab实现)
- ASP.NET4.5从零开始(1)
- Node相关参考资料
- LVS-三种负载均衡方式比较
- 如何在外网中访问自己在另一个局域网中的某个机器(SSH为例)
- an&#39;gularjs 环境搭建之NodeJS、NPM安装配置步骤(windows版本)
- sql插入多条数据的sql语句
- IIS 6.0 401 错误
- CSS之全屏背景图
- swift闭包传值
- [转]Delphi中,让程序只运行一次的方法
- ThinkPHP框架下基于RBAC的权限控制模式详解
- java 泛型深入之Set有用工具 各种集合泛型深入使用演示样例,匿名内部类、内部类应用于泛型探讨
- ssl通关的概念(一个)
- IOS开发-UI学习-sqlite数据库的操作
- webpack之前端性能优化(史上最全,不断更新中。。。)
- console报错类型
- [css]将textarea前的文字设置在左上角
- Stm32常见英文缩写