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. winform中进行post上传文件

    winform中要上传文件到远程的服务器上面,我在本地用的是post方式传递数据,用的是HTTP协议,具体代码如下: 下面的代码就是一个上传的方法,参数需要路径和文件路径就可以了,我本地winform ...

  2. Springboot2.x整合SpringSecurity

    一.Spring Security是什么?有什么作用(核心作用)?以及如何阅读本篇文章 1.是什么 Spring Security是Spring家族的一个强大的安全框架,与Springboot整合的比 ...

  3. XMPP即时通讯协议使用(三)——订阅发布、断开重连与Ping

    package com.testV3; import java.util.List; import org.jivesoftware.smack.ConnectionListener; import ...

  4. shell函数的结束与返回值

  5. nginx部署静态资源

    第一步.推荐使用EditPlus中ftp工具 安装,然后点击File->FTP->FTPUPLOAD->Settings->add.然后进行配置: 这样只是为了方便编辑Linu ...

  6. 异常关机,同时出现:Last_IO_Errno: 1236,Last_SQL_Errno: 1594

    一主两从的结构,由于异常关机,导致两个从库都出现如下问题: mysql> show slave status \G;*************************** 1. row **** ...

  7. Ubuntu12.04安装配置vncserver

    安装 sudo apt-get install vnc4server 修改配置文件 sudo vim ~/.vnc/xstartup #!/bin/sh # Uncomment the followi ...

  8. Redis GeoHash

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11632810.html 背景 微信找附近的人,滴滴找附近的单车,饿了么找附近的餐馆 GeoHash算法 ...

  9. npm 是干什么的?(非教程)

    看了之后就很清楚什么叫NPM,以后它是干嘛的.谢谢楼主 网上的 npm 教程主要都在讲怎么安装.配置和使用 npm,却不告诉新人「为什么要使用 npm」.今天我就来讲讲这个话题. 本文目标读者是「不太 ...

  10. 【HDOJ6609】Find the answer(线段树)

    题意:给定一个n个正整数的数列,第i项为w[i],对于每个i,你要从[1,i-1]中选择一些变成0,使得变化后[1,i]的总和小于m,每次询问最少要变几个 n<=2e5,m<=1e9,1& ...