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

5 2
1 2 3 4 5
2 1 4 1
1 2 3 4
2 0 0

Sample Output

Case 1:
10
5
Case 2:
6

Source

题意:给你n个数,求取r个的方案数
题解:搜题解时博客上有人说这是泛化背包,说实话,这个东西看了很久都没理解,
只有等以后再来弄懂了………
代码:
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
using namespace std; typedef long long LL;
const int N = ;
int num[N], que[N], n, m;
LL f[N];
int main(){
freopen("1285.txt", "r", stdin);
int cas = ;
while(~scanf("%d %d", &n, &m ) && n){
memset(num, , sizeof(num));
int tp;
for(int i = ; i < n ;i++){
scanf("%d", &tp);
tp--;
num[tp]++;
}
for(int i = ; i < m; i++){
scanf("%d", &que[i]);
}
memset(f, , sizeof(f));
for(int i = ;i <= num[]; i++){
f[i] = ;
}
for(int i = ; i < n; i++){
for(int j = n; j >= ; j--){
for(int k = max(j - num[i], ); k < j ; k ++){
f[j] += f[k];
}
}
}
cas++;
printf("Case %d:\n", cas);
for(int i = ; i < m; i++){
printf("%I64d\n", f[que[i]]);
}
}
return ;
}

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. 98-基于FPGA Spartan6 的双路光纤PCIe采集卡(2路光纤卡)

    基于FPGA Spartan6 的双路光纤PCIe采集卡(2路光纤卡) 1.板卡概述  板卡采用xilinx Spartan6系列芯片,支持 PCI Express Base Specificatio ...

  2. 关于Insufficient space for shared memory file解决办法

    发现这个目录使用率100%,但是这个只是逻辑卷,具体是由于/tmp目录下,日志文件太多,导致空间被占满了.

  3. 《x的奇幻之旅》:有趣的数学科普

    本书是相对比较少见的数学方面的科普书.从最简单的阿拉伯数字.加减法,一直到概率统计.微积分.群论.拓扑.微分几何,每个主题都用几千字做一些深入浅出的介绍.写的相当的有趣. 在书中又一次看到这个有趣的事 ...

  4. 工欲善其事,必先利其器——React Native的 IDE

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yayayaya20122012/article/details/51119801之前的文章中,我们已 ...

  5. win10搭建ftp环境

    1.进入程序,并点击“启用或关闭windows功能” 2.勾选对应的功能,并点击“确定” 3.进入IIS 4.如下图 5.如图 6.如图 7.如下图 8.创建新用户,用于登录ftp(自行创建) 9.禁 ...

  6. HTML5知识点总结(一)

    最近在复习前端的基础知识,在这里做一个总结,这是HTML5篇. 新特性 取消了过时的显示效果标记<font></font>和<center></center& ...

  7. pytest 教程

    https://blog.csdn.net/lb245557472/article/details/90341297 https://blog.csdn.net/yyang3121/article/d ...

  8. 纯JSP简单登录实例

    记一下,免得以后忘记了,又要去查. 文件共有四个web.xml.login.jsp.logout.jsp.welcome.jsp四个文件 测试环境:Tomcat 6.0.x 假设项目名称是LoginS ...

  9. 【JVM】JVM参数

    JVM参数的含义  参数名称 含义 默认值   -Xms 初始堆大小 物理内存的1/64(<1GB) 默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,JVM就会增大堆 ...

  10. Web项目改名的带来的404not found问题

    为了保留上一次编辑的billsys web项目,把项目复制一份到同一个工作空间后,对原来项目名进行了重命名,如右图: 结果再去访问,一直报404错误 解决思路如下: 其实仔细观察,会在项目部署界面发现 ...