cf451E. Devu and Flowers
题意:
有n个箱子,第i个箱子里有ai朵花,同一个箱子里花的颜色一样,不同箱子里的花颜色不一样。现在在这些箱子里选出m朵花组成一束,求一共有多少种方案。要求任意两束花都不一样
题解:
设第i个箱子里花的颜色是Bi,则本题就等价于从多集合S={A1 * B1,A2 * B2 …An * Bn}中选出M个元素能够产生的不同多重集的数量。根据多重集组合数的结论有:
CN+M−1N−1−∑i=1NCN+M−Ai−1N−1+∑1<=i<j<=NCN+M−Ai−Aj−3N−1−....+(−1)NCN+M−∑i=1NAi−(N+1)N−1C_{N+M-1}^{N-1}-\sum_{i=1}^{N}C_{N+M-A_{i}-1}^{N-1}+\sum_{1<=i<j<=N}C_{N+M-A_{i}-A_{j}-3}^{N-1}-....+(-1)^{N}C_{N+M-\sum_{i=1}^{N}A_{i}-(N+1)}^{N-1}CN+M−1N−1−∑i=1NCN+M−Ai−1N−1+∑1<=i<j<=NCN+M−Ai−Aj−3N−1−....+(−1)NCN+M−∑i=1NAi−(N+1)N−1
具体证明略
对于这种容斥,我们一般用x在二进制表示下,第i位为1则说明选中第i位,x的二进制下共有p个1,就相当于选了p位,就可以用x来枚举上述式子
(−1)p∑N+M−Ai1−Ai2....−Aip−(p+1)N−1(-1)^p\sum_{N+M-A_{i_{1}}-A_{i_{2}}....-A_{i_{p}}-(p+1)}^{N-1}(−1)p∑N+M−Ai1−Ai2....−Aip−(p+1)N−1
这样就可以得到容斥原理计算多重集组合数的公式的每一项
在求组合数过程中我们可以用Lucas进行优化
详细看代码
代码:
// Problem: E. Devu and Flowers
// Contest: Codeforces - Codeforces Round #258 (Div. 2)
// URL: https://codeforces.com/contest/451/problem/E
// Memory Limit: 256 MB
// Time Limit: 4000 ms
// Data:2021-09-02 11:04:13
// By Jozky#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{x= 0;char c= getchar();bool flag= 0;while (c < '0' || c > '9')flag|= (c == '-'), c= getchar();while (c >= '0' && c <= '9')x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();if (flag)x= -x;read(Ar...);
}
template <typename T> inline void write(T x)
{if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
// const int maxn=;
const int mod= 1e9 + 7;
ll a[30];
ll inv[30];
ll poww(ll a, ll b)
{ll ans= 1;while (b) {if (b & 1)ans= ans * a % mod;a= a * a % mod;b>>= 1;}return ans % mod;
}
void init()
{for (int i= 1; i <= 20; i++)inv[i]= poww(i, mod - 2);
}
ll n, m;
ll C(ll n, ll m)
{if (m < 0 || n < 0 || n < m)return 0;n%= mod;if (n == 0 || m == 0)return 1;ll a= 1, b= 1;for (int i= 0; i < m; i++) {a= (a * (n - i)) % mod;b= (b * inv[i + 1]) % mod;}return a * b % mod;
}
ll Lucas(ll n, ll m)
{if (n < m)return 0;if (m == 0)return 1;return Lucas(n / mod, m / mod) * C(n % mod, m % mod) % mod;
}
int main()
{//rd_test();init();cin >> n >> m;for (int i= 1; i <= n; i++)read(a[i]);ll ans= 0;for (int x= 0; x < (1 << n); x++) {if (x == 0) {ans= (ans + Lucas(n + m - 1, n - 1)) % mod;}else {ll t= n + m;int p= 0;for (int i= 0; i < n; i++) {if ((x >> i) & 1) {p++;t-= a[i + 1];}}t-= (p + 1);// cout << "t=" << t << endl;if (p & 1) {ans= (ans - Lucas(t, n - 1)) % mod;}else {ans= (ans + Lucas(t, n - 1)) % mod;}}// cout << "ans=" << ans << endl;}cout << (ans + mod) % mod << endl;//Time_test();
}