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 10^3, K \le 20\)

正解 :

期望乘上那个组合数就是所有方案下的答案了对吧

直接 dp 肯定不太好做,要记录每一个人选了多少个曲奇,考虑换一种方式来表示原问题

首先原问题可以转化成这样一个问题

每一个人从拥有的曲奇里选择某一天得到的那一个曲奇, 求不同的选法

答案其实也正好是 \(\prod c_i\) (神奇的是这样做与原问题是等价的)

所以我们只需要关心每个人选择的那个曲奇就好了

设 \(x_i\) 表示有 \(x_i\) 个孩子在第 \(i\) 天得到了它们选择的曲奇

先不考虑顺序, 钦定就是前面 \(x_i\) 个人得到了这 \(x_i\) 个曲奇

转移系数是 \(\binom {N - x_i} {a_i - x_i}\) (将剩下还没被选的曲奇随便分给其他人)

最后再将人排序, 乘上 \(\frac {N!} {\prod x_i!}\) (同一天选的与顺序没有关系, 所以要除 \(x_i!\))

答案其实就是
\[
N! \prod_{i = 1}^n \frac {\binom {N - x_i} {a_i - x_i}} {x_i!}
\]
根据这个式子可以设出状态 \(f(i, j)\) 表示前 \(i\) 天有 \(j\) 个人得到了选择的那个曲奇的方案数

转移时枚举 \(x_i\) 并乘上有关于 \(x_i\) 的系数

状态 \(O(NK)\), 转移 \(O(N)\), 时间复杂度 \(O(N^2 K)\)

\(\color {DeepSkyBlue} {Code}\)

/*
    f[i][j] = \sum f[i - 1][j - x] * comb(n - x, a[i] - x) * ifac[x]
*/
#include <bits/stdc++.h>
#define K 25
#define N 1005

using namespace std;

const int mod = 1e9 + 7;

int n, k;
int a[K], f[K][N];
int fac[N], ifac[N];

inline int fpm(int x, int y) {
    int r = 1;
    while(y) {
        if(y & 1) r = 1LL * r * x % mod;
        x = 1LL * x * x % mod, y >>= 1;
    }
    return r;
}
inline int perm(int x, int y) { return 1LL * fac[x] * ifac[x - y] % mod; }
inline int comb(int x, int y) { return 1LL * perm(x, y) * ifac[y] % mod; }

int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= k; ++i)
        scanf("%d", &a[i]);

    fac[0] = 1;
    for(int i = 1; i <= n; ++i) fac[i] = 1LL * i * fac[i - 1] % mod;
    ifac[n] = fpm(fac[n], mod - 2);
    for(int i = n; i; --i) ifac[i - 1] = 1LL * i * ifac[i] % mod;

    f[0][0] = 1;
    for(int i = 0; i < k; ++i) {
        for(int j = 0; j <= n; ++j) {
            if(!f[i][j]) continue;
            for(int x = 0; x <= a[i + 1] && j + x <= n; ++x) {
                f[i + 1][j + x] = (f[i + 1][j + x] +
                1LL * f[i][j] * comb(n - x, a[i + 1] - x) % mod * ifac[x]) % mod;
            }
        }
    }

    int ans = 1LL * fac[n] * f[k][n] % mod;
    printf("%d\n", ans);
    return 0;
}

原文地址:https://www.cnblogs.com/Lskkkno1/p/12188300.html

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

Dwango Programming Contest 6th C的相关文章

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 turn

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 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 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

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

ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018

ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018 Problem A. Can Shahhoud Solve it? Problem B. Defeat the Monsters Problem C. UCL Game Night Problem

The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元

题目来源 The 2018 ACM-ICPC China JiangSu Provincial Programming Contest 35.4% 1000ms 65536K Persona5 Persona5 is a famous video game. In the game, you are going to build relationship with your friends. You have N friends and each friends have his upper b