UVaLive 7143 Room Assignment (组合数+DP)
题意:有 n 个客人,m个房间,每个房间可住ci个人,这 n 个人中有 t 对双胞胎,sum{ci} = n 问你有多少种住房方法。
析:计数DP,dp[i][j] 表示前 i 个房间,还剩下 j 对双胞胎未住,第 i+1 个房间,就从剩下的 j 对双胞胎中选 k 对,然后再从不是双胞胎的人选剩下的,每对先选一个,然后再从剩下的选全部的,
求组合数过程可能要用到逆元,可以提前打表。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const int dr[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int dc[] = {1, 0, -1, 0, 1, 1, -1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int t;
LL fact[maxn], inv[maxn];
int c[15];
LL dp[15][105]; inline LL f(int x){ return 1 == x ? 1LL : (mod - mod/x) * f(mod % x) % mod; } void init(){
fact[0] = 1;
for(int i = 1; i < maxn; ++i) fact[i] = fact[i-1] * i % mod;
for(int i = 0; i < maxn; ++i) inv[i] = f(fact[i]);
} inline LL C(int a, int b){ return (a < b || a < 0 || b < 0) ? 0 : fact[a] * inv[b] % mod * inv[a - b] % mod; } LL solve(){
memset(dp, 0, sizeof dp);
dp[0][t] = 1;
for(int i = 0, sum = n; i < m; ++i, sum -= c[i])
for(int j = 0; j <= t; ++j) if(dp[i][j])
for(int k = 0; k <= j && c[i+1] >= k; ++k)
dp[i+1][j-k] = (dp[i+1][j-k] + dp[i][j] * C(j, k) % mod * C(sum-2*j+k, c[i+1]-k)) % mod;
return dp[m][0];
} int main(){
init();
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d %d %d", &n, &m, &t);
for(int i = 1; i <= m; ++i) scanf("%d", c+i);
printf("Case #%d: %lld\n", kase, solve());
}
return 0;
}
UVaLive 7143 Room Assignment (组合数+DP)的更多相关文章
- UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7143 Room Assignment(组合数学+DP)
题目链接 参考自:http://www.cnblogs.com/oyking/p/4508260.html 题意 n个人,其中有k对双胞胎.现有m间房间,每间房间有容量ci问分配房间的方案数. 分析 ...
- 【bzoj4517】[Sdoi2016]排列计数 组合数+dp
题目描述 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是稳定的 满足条 ...
- acdream1412:2-3 trees 组合数dp
题意: 给出一个标准2-3树的叶子节点(最底层节点)个数 L,求2-3数的形成方案数并取余 分析: 如果有L个叶子枚举 每个 可以使x*2+y*3=L 的 x y 那么在最底层就有 c(x+y,x) ...
- CodeForces 146E Lucky Subsequence(组合数+DP)
题目描述 Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers w ...
- hdu6006 Engineer Assignment 状态dp 定义dp[i][s]表示前i个工程状态为s可以执行的最大工程数。s表示前i个工人选走了s状态的工程师。
/** 题目:hdu6006 Engineer Assignment 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6006 题意:已知n个工程,每个需要某 ...
- 【BZOJ1925】[Sdoi2010]地精部落 组合数+DP
[BZOJ1925][Sdoi2010]地精部落 Description 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从 ...
- UVALive 4261——Trip Planning——————【dp+打印路径】
Trip Planning Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Stat ...
- 放棋子:组合数/dp/容斥原理
题目传送门 啊又是一个考场上没拿到的水题,差一步!! 组合数,先打个杨辉三角吧. 显然棋子应该一种一种的放,这很dp. 而且棋子一旦放下,那么它所在的行列就只能放这种颜色的棋子了. 设dp[i][x] ...
随机推荐
- Acadia Lab 6 轮盘游戏机
WRTnode 肯定不是亲生的... 果断转投Acadia —.— 不是国军不给力,奈何共军有高达 为啥不转树莓派?因为选做实验肯定有很多人用树莓派做...我抢不过他们,只能挑点冷门的蹭分_(:з」 ...
- DOM元素querySelectorAll可能让你意外的特性表现
一.时间紧急,废话少说 本文所在的页面藏匿了下面这些代码: <img id="outside"> <div id="my-id"> &l ...
- LinckedhashMap原理
http://zhangshixi.iteye.com/blog/673789 TreeMap的key是有顺序的,是自然顺序,也可以指定比较函数. 但默认不是按插入的顺序. 为了让Map JSON ...
- 2016国内最值得期待的响应式前端框架pintuer(拼图)--http://www.pintuer.com
近期,需要将项目从pc端的应用扩展到移动端. 当然移动框架的第一选择必然是bootstrap,但是bootstrap作为移动端明显过于死板,而且作为国外的产品,对于国内的应用明显水土不服.框架里总有那 ...
- 使用Carthage管理iOS依赖库
Carthage安装和使用和CocoaPods类似: 1.安装: 终端执行以下命令: $ brew update $ brew install carthage 查看Carthage的版本号: $ c ...
- C#对于文件操作
//C#追加文件 StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt"); ...
- oracle分页查询sql
select * from( select shopid,rownum rn from p_shopinfo where is_hot=1 and rownum <=6 order by sho ...
- UItextFiled只能输入数字
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementS ...
- 【转】+【举例】ArcGIS中的坐标系统定义与投影转换
背景知识: UTM (Universal Transverse Mercator)坐标系是由美国军方在1947提出的.虽然我们仍然将其看作与"高斯-克吕格"相似的坐标系统,但实际上 ...
- 07-JAVA继承与接口
动手动脑: 源程序: public class FuGai { public static void main(String[] args) { // TODO Auto-generated meth ...