acdream 群赛5 组合数学
2012-12-08 16:37
295 查看
Problem B: Dice Dice Dice
Time Limit: 10 Sec Memory Limit:128 MB
Submit: 81 Solved: 17
[Submit][Status][Web Board]
Description
There are 1111 ways in which five 6-sided dice (sides numbered 1 to 6) can be rolled so that the top three numbers sum to 15. Some examples are:D1,D2,D3,D4,D5 = 4,3,6,3,5
D1,D2,D3,D4,D5 = 4,3,3,5,6
D1,D2,D3,D4,D5 = 3,3,3,6,6
D1,D2,D3,D4,D5 = 6,6,3,3,3
Now we have a extended problem:
In how many ways can n m-sided dice (sides numbered 1 to m) be rolled so that the top k numbers sum to p?
Input
There are multiple test cases. (about 15 groups)For each test case, there is only four integers n, m, k, p. (1 <= k <= n <= 20, 3 <= m <= 12)
Output
For each test case, output an integer indicating the answer.Sample Input
5 6 3 156 6 3 15Sample Output
11117770HINT
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> #include <stack> #include <map> using namespace std; #define LL long long int n,m,k,p; int re[109]; LL ans; LL N(int t){ LL ret = 1; for(int i=2;i<=t;i++) ret*=i; return ret; } void dfs(int t,int lim) { if(t<k){ for(int i=lim;i>0;i--) { re[t] = i; dfs(t+1,i); } }else if(t==k){ int s = 0; for(int i=0;i<k;i++) s+=re[i]; if(s!=p) return ; // for(int i=0;i<k;i++) // cout<<re[i]<<" ";cout<<endl; if(k<n) for(int i=lim;i>0;i--) { re[t] = i; dfs(t+1,i); } else { re =lim; dfs(t+1,lim); } return ; }else if(t>k&&t<n) { for(int i=lim;i>0;i--) { re[t] = i; dfs(t+1,i); } return ; } if(t>=n){ LL tmp = N(n); // cout<<"con:"<<endl; // for(int i=0;i<n;i++) // cout<<re[i]<<" ";cout<<endl; // cout<<tmp<<endl; int c=1; re = -1; for(int i=1;i<=n;i++) { if(re[i]!=re[i-1]) { tmp/=N(c); c=1; } else c++; } ans+=tmp; return ; } } int main() { // freopen("in.txt","r",stdin); while(~scanf("%d%d%d%d",&n,&m,&k,&p)) { // cout<<"<<<<<<<<<<<"<<endl; ans = 0; dfs(0,m); cout<<ans<<endl; } return 0; }
相关文章推荐
- Codeforces 451D - Count Good Substrings(组合数学)
- 组合数学之母函数
- [HNOI2008] [BZOJ1008] 越狱|组合数学
- Codeforces Beta Round #95 (Div. 2) C 组合数学
- 数学 ACdream 1196 KIDx's Triangle
- HDOJ 2048 神、上帝以及老天爷(组合数学,全错位排列)
- bzoj3997 组合数学 递推
- joj 1172 The Equation Problem 组合数学
- poj——1833(组合数学之排列数)
- bzoj 3997: [TJOI2015]组合数学
- ACdream 1095 EOF女神的相反数(数学:二进制处理)
- codeforces838D IndiaHacks 2nd Elimination 2017 #Airplane Arrangements -- 组合数学
- 走格子(数学组合/动态规划)
- pku 1942 Paths on a Grid 组合数学——排列组合
- 组合数学——Nim取子游戏 太有趣了
- 组合数学 - 置换群的幂运算 --- poj CARDS (洗牌机)
- hdoj 5525 Product 【组合数学+质因子处理】
- 组合数学 - 放苹果问题
- poj 3270 Cow Sorting (组合数学 置换应用)
- UVA 19889 组合数学