Combinations, Once Again
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1897   Accepted: 636

Description

Given n objects you'd have to tell how many different groups can be chosen if r objects are taken at a time.

Input

Input consists of less than 100 test cases. Each test case begins with two integers n (0 < n <= 50), m (0 <= m <= n). The next line will contain the labels (numbers in the range 1 to n) of the n objects you are to choose from. Two objects with the same label are considered equivalent. Then in the last line for that test case, you'd have m values for r. There will be a single space separating two consecutive numbers in a line. Input is terminated by a test case where n=0, you must not process this test case.

Output

For each test case, print the test case number. And for each query number r, print the number of different groups that can be formed if r objects are taken from the given n objects. You can assume that for all input cases, the output will always fit in a 64-bit unsigned integer and (0<=r<=n).

Sample Input

  1. 5 2
  2. 1 2 3 4 5
  3. 2 1
  4.  
  5. 4 1
  6. 1 2 3 4
  7. 2
  8.  
  9. 0 0

Sample Output

  1. Case 1:
  2. 10
  3. 5
  4. Case 2:
  5. 6

Source

题意:给你n个数,求取r个的方案数
题解:搜题解时博客上有人说这是泛化背包,说实话,这个东西看了很久都没理解,
只有等以后再来弄懂了………
代码:
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<algorithm>
  7. #include<map>
  8. #include<queue>
  9. using namespace std;
  10.  
  11. typedef long long LL;
  12. const int N = ;
  13. int num[N], que[N], n, m;
  14. LL f[N];
  15. int main(){
  16. freopen("1285.txt", "r", stdin);
  17. int cas = ;
  18. while(~scanf("%d %d", &n, &m ) && n){
  19. memset(num, , sizeof(num));
  20. int tp;
  21. for(int i = ; i < n ;i++){
  22. scanf("%d", &tp);
  23. tp--;
  24. num[tp]++;
  25. }
  26. for(int i = ; i < m; i++){
  27. scanf("%d", &que[i]);
  28. }
  29. memset(f, , sizeof(f));
  30. for(int i = ;i <= num[]; i++){
  31. f[i] = ;
  32. }
  33. for(int i = ; i < n; i++){
  34. for(int j = n; j >= ; j--){
  35. for(int k = max(j - num[i], ); k < j ; k ++){
  36. f[j] += f[k];
  37. }
  38. }
  39. }
  40. cas++;
  41. printf("Case %d:\n", cas);
  42. for(int i = ; i < m; i++){
  43. printf("%I64d\n", f[que[i]]);
  44. }
  45. }
  46. return ;
  47. }

poj1285 Combinations, Once Again(泛化背包)的更多相关文章

  1. [bzoj1296][SCOI2009]粉刷匠(泛化背包)

    http://www.lydsy.com:808/JudgeOnline/problem.php?id=1296 分析: 首先预处理出每一行的g[0..T]表示这一行刷0..T次,最多得到的正确格子数 ...

  2. POJ 1947 - Rebuilding Roads 树型DP(泛化背包转移)..

    dp[x][y]表示以x为根的子树要变成有y个点..最少需要减去的边树... 最终ans=max(dp[i][P]+t)  < i=(1,n) , t = i是否为整棵树的根 > 更新的时 ...

  3. POJ 1155 - TELE 树型DP(泛化背包转移)..

    dp[x][y]代表以x为根的子树..连接了y个终端用户(叶子)..所能获得的最大收益... dp[x][ ]可以看成当根为x时..有个背包空间为0~m...每个空间上记录了到到达这个空间的最大收益. ...

  4. POJ1285 Combinations, Once Again(背包 排列组合)

    背包解组合数学问题,n种物品,每种num[i]个,求取r个的方法数. 背包思想,f[j]表示当前取j个数的方法数,则状态转移方程为 f[j] += f[k](max(j - num[i], 0) &l ...

  5. zoj3623 Battle Ships ——完全背包?简单DP!|| 泛化背包

    link:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3623 看起来像完全背包,但是物品价值是变化的,所以很多人搞的很复 ...

  6. HDOJ 1561 - 树形DP,泛化背包

    刚看题...觉得这不是棵树...可能有回路...仔细一想..这还真是棵树(森林)...这是由于每个城堡所需要提前击破的城堡至多一个..对于一个城堡.其所需提前击破的城堡作为其父亲构图.... dp[k ...

  7. 树上背包O(n*m^2)|| 多叉树转二叉树 || o(n*m)???

    #. 选课 描述 提交 自定义测试 问题描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有 ...

  8. luogu P2066 机器分配[背包dp+方案输出]

    题目背景 无 题目描述 总公司拥有高效设备M台,准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值.其中M≤15 ...

  9. P1417 烹调方案[背包]

    题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...

随机推荐

  1. 攻防世界--re2-cpp-is-awesome

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/c5802869b8a24033b4a80783a67c858b 1.准备 获取信息 6 ...

  2. js字符串相关方法

    <script> // 使用索引位置来访问字符串中的每个字符: var carname = 'Volvo XC60'; var character = carname[7]; consol ...

  3. vue项目build打包后图片路径不对导致图片空白不显示问题解决方法

    1.在上篇文章src配置及引入的基础上修改项目配置: 文章链接地址:https://www.cnblogs.com/hsl-shiliang/p/10333022.html. 2.具体配置过程如图: ...

  4. 四、局域网连接SqlServer

    一.局域网连接SqlServer 一台服务器上装有四个数据库的时候,我们可以通过IP\实例名的方式进行访问. navicat 连接sqlserver数据库

  5. procixx 时钟的坑

    error 1.procixx设置800M,uboot中是720M 了解到整个过程是,当procixx配置后,通过FSBL中的ps_init.c反应出来 // ARM_PLL_FDIV = 48    ...

  6. procixx和最近调试的坑

    流程: 1.procixx/vivado 配置soc硬件信息,导出FSBL.out: 2.配置uboot dts,生成u-boot (需要打开的硬件 配置为status = "okay&qu ...

  7. 分布式架构的CAP原理

    CAP 定理的含义   一.分布式系统的三个指标 1998年,加州大学的计算机科学家 Eric Brewer 提出,分布式系统有三个指标. Consistency Availability Parti ...

  8. boost location-dependent times

    1. local_date_time #include <boost/date_time/local_time/local_time.hpp> #include <iostream& ...

  9. springmvc REST风格的URL

    1:需要配置一个filter <!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELET ...

  10. codeforces 848A - From Y to Y(构造)

    原题链接:http://codeforces.com/problemset/problem/848/A 题意:让我们构造一个字符串.这里有一种操作:取走这个字符串的若干部分,分成两部分,然后将这两部分 ...