题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=648&page=show_problem&problem=5155

There are N guests checking in at the front desk of the hotel. 2K (0 ≤ 2K ≤ N) of them are twins.
There are M rooms available. Each room has capacity ci which means how many guests it can hold.
It happens that the total room capacity is N, i.e. c1 + c2 + . . . + cM = N.
The hotel receptionist wonders how many different room assignments to accommodate all guests.
Since the, receptionist cannot tell the two twins in any pair of twins apart, two room assignments are
considered the same if one can be generated from the other by swapping the two twins in each of some
number of pairs. For rooms with capacity greater than 1, it only matters which people are in the room;
they are not considered to be in any particular order within the room.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts
with three integers N, M, and K, which indicates the number of guests, the number of rooms, and the
number of pairs of twins. The following line contains M integers, c1, c2, …, cM, which indicates the i-th
room’s capacity.
Output
For each test case, first output one line containing ‘Case #x: y’, where x is the test case number
(starting from 1) and y is the number of different room assignments modulo 1,000,000,007 (109 + 7).

题目大意:有n个颜色的球,其中有k对球颜色相同,别的都是完全不同的。给m个盒子,每个盒子的容量为c[i],有sum{c[i]}=n。问:有多少种姿势可以把n个球全部放入盒子中。

思路:首先这是一条组合计数的动态规划。

用dp[i][r]表示,前i个盒子已经放完了,手上还拿着r对同色球。

设对于状态dp[i][r],在第 i+1 个盒子中,我们要从 r 对同色球中取出 a 对,拿其中一个放入盒子 i+1 ;从剩下的 r-a 对同色球中,拿出 b 对,全部放入盒子 i+1 中;再从其他剩下的未放入盒子的球里面(假设有 sum 个),取 c[i]-a-2*b 个放入睇 i+1 个盒子中。这样便转移到了状态dp[i+1][r-a-b]。

状态转移方程为:dp[i+1][r-a-b] = dp[i][r] * comb(r, a) * comb(r - a, b) * comb(sum - 2 * r, c[i] - a - 2 * b).

其中comb(p, q)表示从 p 个物体中选出 q 个的组合数。

至于在同色球中只选出其中一个球的问题,可以考虑:给两个球强行编号为1、2,然后强行要求1必需放在2的前面,这样就不会产生重复。当我们放入球1之后,球2就与其他普通的球无异了,无需任何处理。

然后预处理一下阶乘及其逆元,利用公式comb(p, q) = p! / q! / (p-q)!,在O(1)时间内求出组合数。

总的时间复杂度为O(mk^3)。

代码(1.252S):

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL; const int MAXN = ;
const int MOD = 1e9 + ; int inv[MAXN], fact[MAXN]; int _inv(int x) {
if(x == ) return ;
return LL(MOD - MOD / x) * _inv(MOD % x) % MOD;
} void init(int n = ) {
fact[] = ;
for(int i = ; i <= n; ++i)
fact[i] = fact[i - ] * LL(i) % MOD;
for(int i = ; i <= n; ++i)
inv[i] = _inv(fact[i]);
} LL comb(int a, int b) {
if(a < b) return ;
return LL(fact[a]) * inv[b] % MOD * LL(inv[a - b]) % MOD;
} int dp[][];
int c[];
int T, n, m, k; int mulmul(LL a, LL b, LL c, LL d) {
return a * b % MOD * c % MOD * d % MOD;
} void update_add(int &a, int b) {
a += b;
if(a >= MOD) a -= MOD;
} int solve() {
memset(dp, , sizeof(dp));
dp[][k] = ;
for(int i = , sum = n; i < m; ++i) {
for(int r = ; r <= k; ++r) if(dp[i][r]) {
if(sum < * r) break;
for(int a = ; a <= r; ++a) {
for(int b = ; b + a <= r; ++b) {
if(c[i + ] - a - * b < ) break;
int t = mulmul(dp[i][r], comb(r, a), comb(r - a, b), comb(sum - * r, c[i + ] - a - * b));
update_add(dp[i + ][r - a - b], t);
}
if(i == m - ) break; /// if i = m - 1 then a must be zero
}
}
sum -= c[i + ];
}
return dp[m][];
} int main() {
init();
//printf("%d\n", comb(10, 1));
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d%d", &n, &m, &k);
for(int i = ; i <= m; ++i) scanf("%d", &c[i]);
printf("Case #%d: %d\n", t, solve());
}
}

UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)的更多相关文章

  1. dp --- 2014 Asia AnShan Regional Contest --- HDU 5074 Hatsune Miku

    Hatsune Miku Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5074 Mean: 有m种音符(note),现在要从 ...

  2. UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  3. UVALive 7141 BombX(离散化+线段树)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  4. UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  5. UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  6. UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)

    Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...

  7. UVALive 7148 LRIP(树的分治+STL)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  8. UVaLive 7143 Room Assignment (组合数+DP)

    题意:有 n 个客人,m个房间,每个房间可住ci个人,这 n 个人中有 t 对双胞胎,sum{ci}  = n 问你有多少种住房方法. 析:计数DP,dp[i][j] 表示前 i 个房间,还剩下 j ...

  9. HDU 5074 Hatsune Miku 2014 Asia AnShan Regional Contest dp(水

    简单dp #include <stdio.h> #include <cstring> #include <iostream> #include <map> ...

随机推荐

  1. React使用jquery方式动态获取数据

    好久没写react了,今天有空写一下来react实现实时请求数据,并刷新数据的小demo. 首先我还是选择了jquery方式中自带的ajax获取数据,首先要引用所需的js包 接下来要写一个自定义的js ...

  2. 写JQuery 插件 什么?你还不会写JQuery 插件

    http://www.cnblogs.com/Leo_wl/p/3409083.html 前言 如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui ...

  3. Mybatis拦截器 mysql load data local 内存流处理

    Mybatis 拦截器不做解释了,用过的基本都知道,这里用load data local主要是应对大批量数据的处理,提高性能,也支持事务回滚,且不影响其他的DML操作,当然这个操作不要涉及到当前所lo ...

  4. bzoj2500: 幸福的道路(树形dp+单调队列)

    好题.. 先找出每个节点的树上最长路 由树形DP完成 节点x,设其最长路的子节点为y 对于y的最长路,有向上和向下两种情况: down:y向子节点的最长路g[y][0] up:x的次长路的g[x][1 ...

  5. Intellij如何设置编译后自动重新加载class文件?

    前段时间突然发现Intellij不能自动重新加载类了,每次编译后都要重新启动项目,才能显示更新效果,后来网上查询Intellij下如何配置热部署,都说是要配置构件,然后在web容器的编辑页面选择upd ...

  6. linq To DataTable

    //将IEnumerable<T>类型的集合转换为DataTable类型 1111 public static DataTable LINQToDataTable<T>(IEn ...

  7. 《Linux内核设计与实现》读书笔记 第一、二章

    第一章    Linux内核简介 1.1Unix历史 Unix特点:1.很简洁 2.所有东西都被当成文件对待 3.Unix内核和相关的系统工具软件都是用C语言编写而成 4.进程创建非常迅速 所以Uni ...

  8. mysql 主主互备

    双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据库数据一致. 这样做的好处多. 1. 可以做灾备,其中一个坏了可以切换到 ...

  9. 【PC网站前端架构探讨系列】关于中小型PC网站前端架构方案的讨论与实践

    目   录 1.遇到的问题 2.目标 3.探讨 4.架构设想 5.流程 6.初步实现 7.存在问题 8.最后 遇到的问题 我在这个系列上篇文章 已经讲解并开始逐步应用模块化思想,不知大家还记不记得,题 ...

  10. Python强化训练笔记(五)——找出多个字典中的公共键

    在这个问题中,我们期望得到的结果是找到这三轮比赛中,每轮都进球的球员都有谁.下面用python来模拟一下,先生成一批数据: >>> from random import randin ...