题面

题解

就是一个求\(\sum_{i= 1}^{n}x _ i = m\)的不重复多重集的个数, 我们可以由容斥原理得到:

\[ans = C_{n + m - 1}^{n - 1} - \sum_{i = 1}^{n}C_{n + m - f_i - 2}^{n - 1} + \sum_{1 \leq i < j \leq n}C_{n + m - f_i - f_j - 3}^{n - 1} - \cdots + (-1)^n C_{n + m - \sum_{k = 1}^{n}f_k -(n + 1)}^{n - 1}
\]

数据范围中\(1\leq N\leq 20\)告诉了我们什么?

我们考虑枚举\(x = 0 \sim 2 ^ n - 1\), 设\(x\)在二进制表示下共有\(p\)位为1, 分别是\(i_1, i_2, i_3, \cdots, i_p\), 则这个\(x\)对答案的贡献就是

\[(-1)^pC_{n+m-\sum_{k=1}^{p}f_{i_k}-(p+1)}^{n-1}
\]

注意到\(x\)为0时它对答案的贡献为\(C_{n + m - 1}^{n - 1}\)

还是因为\(N\)比较小, 我们可以将\(C_{n+m-1}^{n-1}\)转化为\(P_{n+m-1}^{n-1}/(n-1)!\)

由于\(P_{n+m-1}^{n-1}=(n+m-1)*(n+m-2)*\cdots*((n+m-1)-(n-1)+1)\), 再乘上一个\((n-1)!\)的逆元就可以算出来了, 最后对于每个\(x\)求个和即可

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#define itn int
#define reaD read
#define mod 1000000007
#define int long long
using namespace std; int n, m, f[22], inv[22], ans; inline int read()
{
int x = 0, w = 1; char c = getchar();
while(c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); }
while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * w;
} int fpow(int x, int y)
{
int res = 1;
while(y)
{
if(y & 1) res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
} int lucas(int n, int m)
{
if(n < 0 || m < 0 || n < m) return 0;
n %= mod;
if(!n || !m) return 1;
int res = 1;
for(int i = n; i >= n - m + 1; i--) res = res * i % mod;
res = res * inv[m] % mod;
return res;
} signed main()
{
n = read(); m = read(); inv[0] = 1;
for(int i = 1; i <= n; i++) f[i] = reaD();
for(int i = 1; i <= 20; i++) inv[i] = fpow(i, mod - 2);
for(int i = 1; i <= 20; i++) inv[i] = inv[i] * inv[i - 1] % mod;
for(int x = 0; x < (1 << n); x++)
{
if(!x) { ans = (ans + lucas(n + m - 1, n - 1)) % mod; continue; }
int num = n + m - 1, cnt = 0;
for(int i = 0; i < n; i++) if((x >> i) & 1) cnt++, num -= f[i + 1] + 1;
if(cnt % 2) ans = ((ans - lucas(num, n - 1)) % mod + mod) % mod;
else ans = (ans + lucas(num, n - 1)) % mod;
}
printf("%lld\n", ans);
return 0;
}

[题解] [CF451E] Devu and Flowers的更多相关文章

  1. CF451E Devu and Flowers 解题报告

    CF451E Devu and Flowers 题意: \(Devu\)有\(N\)个盒子,第\(i\)个盒子中有\(c_i\)枝花.同一个盒子内的花颜色相同,不同盒子的花颜色不同.\(Devu\)要 ...

  2. CF451E Devu and Flowers(容斥)

    CF451E Devu and Flowers(容斥) 题目大意 \(n\)种花每种\(f_i\)个,求选出\(s\)朵花的方案.不一定每种花都要选到. \(n\le 20\) 解法 利用可重组合的公 ...

  3. CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)

    Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...

  4. BZOJ1101 [POI2007]Zap 和 CF451E Devu and Flowers

    Zap FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d.作为FGD的同学,FGD希望得到 ...

  5. Luogu CF451E Devu and Flowers 题解报告

    题目传送门 [题目大意] 有n种颜色的花,第i种颜色的花有a[i]朵,从这些花中选m朵出来,问有多少种方案?答案对109+7取模 [思路分析] 这是一个多重集的组合数问题,答案就是:$$C_{n+m- ...

  6. CF451E Devu and Flowers(组合数)

    题目描述 Devu想用花去装饰他的花园,他已经购买了n个箱子,第i个箱子有fi朵花,在同一个的箱子里的所有花是同种颜色的(所以它们没有任何其他特征).另外,不存在两个箱子中的花是相同颜色的. 现在De ...

  7. CF451E Devu and Flowers 数论

    正解:容斥+Lucas定理+组合数学 解题报告: 传送门! 先mk个我不会的母函数的做法,,, 首先这个题的母函数是不难想到的,,,就$\left (  1+x_{1}^{1}+x_{1}^{2}+. ...

  8. CF451E Devu and Flowers

    多重集求组合数,注意到\(n = 20\)所以可以用\(2 ^ n * n\)的容斥来写. 如果没有限制那么答案就是\(C(n + s - 1, n - 1)\).对每一个限制依次考虑,加上有一种选多 ...

  9. CF451E Devu and Flowers (组合数学+容斥)

    题目大意:给你$n$个箱子,每个箱子里有$a_{i}$个花,你最多取$s$个花,求所有取花的方案,$n<=20$,$s<=1e14$,$a_{i}<=1e12$ 容斥入门题目 把取花 ...

随机推荐

  1. NPOI_winfrom导出Excel表格(一)(合并单元格、规定范围加外边框、存储路径弹框选择)

    1.导出 private void btn_print_Click(object sender, EventArgs e) { DataTable dtNew = new DataTable(); d ...

  2. HTTP请求管道事件BeginRequest和EndRequest

    这是一个测试示例,代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

  3. JS基础_质数练习,用到了标记flag

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. UVA571Jugs题解--简单数论(其实是瞎搞)

    题目链接 https://cn.vjudge.net/problem/UVA-571 分析 刚做了道倒水问题的题想看看能不能水二倍经验,结果发现了这道题 题意翻译:https://www.cnblog ...

  5. JavaSE基础知识之继承

    一.概述 继承描述的是事物之间的所属关系,这种关系是: is-a 的关系.例如,图中的兔子属于食草动物,食草动物又属于动物.继承可以使多种事物之间形成一种关系体系,让父类更通用,子类更具体. 1.1  ...

  6. echart tooltip问题(鼠标放上去显示所有和显示当个)

    先看效果 两种方式只要修改一下 echat option里面tooltip的属性即可 第一种: tooltip : { show: true, trigger: 'item' // trigger: ...

  7. js二维数组转一维数组

    方法一 利用es5的arr.reduce(callback[, initialValue])实现 var arr1 = [[0, 1], [2, 3], [4, 5]]; var arr2 = arr ...

  8. Windows环境用Xampp自制证书(certificate), Chrome(版本 60.0.3112.90通过)

    标题: Self signed certificate no longer valid as of Chrome 58(Chrome58以后自制证书不再有效)原文地址: https://github. ...

  9. Java注解【三、注解的分类】

    按运行机制分 源码注解 只在源码中存在 编译时注解 在class中依然存在,如@Deprecated 运行时注解 运行阶段起作用,如@Autowired 按来源分 JDK自带注解 三方注解 最常见 自 ...

  10. Redis的keyspace notification(键空间通知)

    文章来源https://www.cnblogs.com/tinywan/p/5903988.html 一.需求分析: 设置了生存时间的Key,在过期时能不能有所提示? 如果能对过期Key有个监听,如何 ...