题意:有 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)的更多相关文章

  1. UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)

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

  2. UVALive 7143 Room Assignment(组合数学+DP)

    题目链接 参考自:http://www.cnblogs.com/oyking/p/4508260.html 题意 n个人,其中有k对双胞胎.现有m间房间,每间房间有容量ci问分配房间的方案数. 分析 ...

  3. 【bzoj4517】[Sdoi2016]排列计数 组合数+dp

    题目描述 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是稳定的 满足条 ...

  4. acdream1412:2-3 trees 组合数dp

    题意: 给出一个标准2-3树的叶子节点(最底层节点)个数 L,求2-3数的形成方案数并取余 分析: 如果有L个叶子枚举 每个 可以使x*2+y*3=L 的 x y 那么在最底层就有  c(x+y,x) ...

  5. CodeForces 146E Lucky Subsequence(组合数+DP)

    题目描述 Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers w ...

  6. 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个工程,每个需要某 ...

  7. 【BZOJ1925】[Sdoi2010]地精部落 组合数+DP

    [BZOJ1925][Sdoi2010]地精部落 Description 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从 ...

  8. UVALive 4261——Trip Planning——————【dp+打印路径】

    Trip Planning Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Stat ...

  9. 放棋子:组合数/dp/容斥原理

    题目传送门 啊又是一个考场上没拿到的水题,差一步!! 组合数,先打个杨辉三角吧. 显然棋子应该一种一种的放,这很dp. 而且棋子一旦放下,那么它所在的行列就只能放这种颜色的棋子了. 设dp[i][x] ...

随机推荐

  1. VFP不同句柄 同一事务处理 统一提交或回滚

    SQLSetprop(m.lnAccHandle,[Transactions],2)SQLSetprop(m.lnSetHandle,[Transactions],2) SQLSetprop(m.ln ...

  2. 机器学习--Classifier comparison

    最近在学习机器学习,学习和积累和一些关于机器学习的算法,今天介绍一种机器学习里面各种分类算法的比较 #!/usr/bin/python # -*- coding: utf-8 -*- "&q ...

  3. quartz 框架定时任务,使用spring @Scheduled注解执行定时任务

    配置quartz 在spring中需要三个jar包: quartz-1.6.5.jar.commons-collections-3.2.jar.commons-logging-1.1.jar 首先要配 ...

  4. jasoncpp读取jason数据如何判断某一字段是否存在

    如标题,使用json_data.isMember("XXX")来判断 jason_data["XXX"].isNull()是用来判断“XXX”字段后边的值是否为 ...

  5. sql group by datetime on day

    SELECT        DATEADD(dd, DATEDIFF(dd, 0, postDate), 0)FROM          tableWHERE        (postDate > ...

  6. 编程风格(Coding Style)要求

    编程风格(Coding Style)要求2.1.1 文件(1) 每个模块(module)一般应存在于单独的源文件中,通常源文件名与所包含模块名相同.(2) 每个设计文件开头应包含如下注释内容:? 年份 ...

  7. jquery的滑动

    (1)slideDown(speed,callback)方法:用于想下滑动的方法. $("#flip").click(function(){ $("#panel" ...

  8. Ptex源码学习笔记-2

    写入纹理数据: 主要分为五种写入方式:新建纹理.编辑已有纹理.编辑ExtHeader中的指定项.写入元数据和写入指定面的纹理数据.写入过程中数据存在一个临时文件中,在close时才会把临时文件的内容拷 ...

  9. 第11章 .NET Remoting

    11.1理解remoting 11.1.1应用程序域基本概念 .NET提供了一项技术,使得跨应用程序域中的对象也可以相互访问,该技术就是.NET remoting.(185) 11.1.2应用程序域的 ...

  10. Java对象的序列化和反序列化

    对象的序列化是指将对象转换为字节序列的过程 对象的反序列化是指将字节序列恢复对象的过程 主要有两种用途: 1.把对象的字节序列永久地保存在硬盘上,通常放在一个文件中. 2.在网络上传输对象的字节序列. ...