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] ...
随机推荐
- js 赋值 要用 toString() ; 太坑了。
js 赋值 要用 toString() ; 太坑了. js 赋值 要用 toString() ; 太坑了. js 赋值 要用 toString() ; 太坑了.
- jquery-1.3.2.js
Uncaught SyntaxError: Unexpected identifier
- funny_python 00 The Zen of Python
# 打算每天多动的时候尽量搜索一些和coding相关的funny stuff Day 00 - PEP 20 The Zen of Python 在shell里面输入python -m this 回车 ...
- xshell xftp
xshell : http://www.netsarang.com/xshell_download.html xftp:http://www.netsarang.com/products/xfp_ov ...
- 初步认识html以及表格的制作
12.21,冬至,天空中还下着小雨,雾霾也没有散去,但是也没有冲散节日的气氛,心情也是倍儿好. 今天学习了不少的内容,对我来说是对之前所学的一个巩固,内容比较多也比较杂乱一些,下面以例子的形式来表现: ...
- hdu 1849(Rabbit and Grass) 尼姆博弈
Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- c#窗体虚线图形验证码设计
/************************窗体验证码背景图形设计及核心代码**********/ using System;using System.Collections.Generic;u ...
- ZT 螨虫知识2
病情分析:过敏是治不好的,只能做到避免接触.指导意见:螨虫的话就不要跟狗多接触,狗的寄生虫很多,还有草地,尤其是狗经常去的地方,草地就是螨虫的传播介质.你是过敏性体质除了被免过敏性源外,还要增强体质, ...
- css 清除浮动最佳方法!
.clear:after{content:'\0020';display:block;height:0;clear:both} .clear{*zoom:1}
- ajax 请求如何解决乱码
今天遇到一个问题,有关ajax 请求中传输中文,遇到乱码的问题. 如下代码: function UpdateFolderInfoByCustId(folderId, folderName, custI ...