首页 > 代码库 > UVaLive 7143 Room Assignment (组合数+DP)
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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。