Dwango Programming Contest 6th Task C. Cookie Distribution

The answer is number of N-tuples (d[1], d[2], ..., d[N]) in all outcomes, where d[i] means on which day the i-th child get a cookie.

Now consider in how many outcomes is there a particular N-tuple (d[1], d[2], ..., d[N]) as described. The answer turns out to be C(N - b[1], a[1] - b[1]) * C(N - b[2], a[2] - b[2]) * ... * C(N - b[K], a[K] - b[K]) where a[i] means number of children chosen on the i-th day, and b[i] means the number of times that i appears in the N-tuple (d[1], d[2], ..., d[N]).

We see that the number that a particular N-tuple is counted is determined by the corresponding K-tuple (b[1], b[2], ..., b[K]).

Now consider how many N-tuple (d[1], d[2], ..., d[N]) corresponds to a particular K-tuple (b[1], b[2], ..., b[K]). This problem can be rephrased as follows: distribute N children over K days, such that there are b[i] children on the i-th day, for i = 1, ..., K. This is a classical problem and the answer is N! / (b[1]! * b[2]! * ... * b[K]!).

Now we need to add up C(N - b[1], a[1] - b[1]) * C(N - b[2], a[2] - b[2]) * ... * C(N - b[K], a[K] - b[K]) N! / (b[1]! b[2]! ... b[K]!) over all possible K-tuples (b[1], b[2], ..., b[K]).

It should be too slow to enumerate all possible K-tuples (b[1], b[2], ..., b[K]) and calculate the corresponding product.

The summation of all the products can be done efficiently with DP. Let f(i, s) be the sum of products of C(N - b[j], a[j] - b[j]) / b[j]! for j = 1, 2, ..., i where b[1], ...., b[i] add up to s. What we need is f(K, N). This DP works in O(N^2 * K). The answer is N! * f(K, N).

This solution works in O(KN^2) time.

原文地址:https://www.cnblogs.com/Patt/p/12200496.html

时间: 2024-07-30 21:44:39

Dwango Programming Contest 6th Task C. Cookie Distribution的相关文章

Dwango Programming Contest 6th C

Cookie Distribution 题意概述 : 有 \(N\) 个孩子,用 \(K\) 天给孩子们发糖果 第 \(i\) 天有 \(a_i\) 个糖果,等概率地发给这 \(n\) 个孩子(每一天每个孩子最多可以获得一个糖果),设 \(K\) 天后第 \(i\) 个孩子获得的糖果为 \(c_i\) 求 \(\prod_{i = 1}^n c_i\) 的期望乘上 \(\prod_{i = 1}^n \binom N {a_i}\) ,答案对 \(10^9 + 7\) 取模.. \(N \le

[Dwango Programming Contest 6th C] Cookie Distribution

传送门 组合意义,妙啊(弱菜 swk 不会数数实锤了 考虑 \(\prod c_i\) 的组合意义,它代表每个孩子在他得到的饼干中选择一个拿出来,最终的方案数. 因此得到了一个几乎显然的 dp: 设 \(f_{i, j}\) 为前 \(i\) 天,有 \(j\) 个孩子已经 "选择了" 他的饼干的方案数.转移时枚举新增了 \(u\) 个孩子,则转移系数是一系列组合数,详见代码. #include <bits/stdc++.h> #define R register #def

Dwango Programming Contest 6th -B

期望 按每个空隙计算 对于第$i$个空隙 对于第$j$个跨过这段的概率是$\frac{1}{i-j+1}$ 因为跨过当且仅当$[j+1,i]$之间都不先于$j$合并 求一个逆元前缀和即可 #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5, P = 1e9 + 7; int n; int x[maxn], inv[maxn]; int main() { ios::sync_with_stdio(false

Dwango Programming Contest V 翻车记

A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 110 char getc(){char c=getchar();while ((c<'A'||c&

【AtCoder】Dwango Programming Contest V题解

A - Thumbnail 根据题意写代码 #include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define pdi pair<db,int> #define mp make_pair #define pb push_back #define enter putchar('\n') #define space putchar(' ') #define

NIKKEI Programming Contest 2019-2 Task D. Shortest Path on a Line

Observations ① 从 $1$ 到 $N$ 的最短路一定是不走回头路的.所谓回头路是指从序号大的点走到序号小的点. 证明:首先,任意从 $1$ 到 $N$ 的路径的最后一步一定不是回头路.假设存在一条从 $1$ 到 $N$ 的最短路走了回头路,并设这条路最后一次回头是从 $u$ 到 $v$ 且从 $v$ 开始直到终点经过的点依次是 $v = v_0, v_1, \dots v_k = N$.我们有 $v < u < N$,$v = v_0 < v_1 < v_2 <

AtCoder AISing Programming Contest 2019 Task D. Nearest Card Game

题目分析在代码注释里. int main() { #if defined LOCAL && !defined DUIPAI ifstream in("main.in"); cin.rdbuf(in.rdbuf()); // ofstream out("main.out"); // cout.rdbuf(out.rdbuf()); #endif int n, q; scan(n, q); vi a(n); scan(a); vl sum(n); sum

ZOJ 3703 Happy Programming Contest(0-1背包)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3703 Happy Programming Contest Time Limit: 2 Seconds      Memory Limit: 65536 KB In Zhejiang University Programming Contest, a team is called "couple team" if it consists of only two s

Happy Programming Contest zoj3703 dp

Description In Zhejiang University Programming Contest, a team is called "couple team" if it consists of only two students loving each other. In the contest, the team will get a lovely balloon with unique color for each problem they solved. Sinc