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. 工作流引擎 springmvc SSM 流程审批 Java Activiti 后台框架源码

    工作流模块  1.模型管理    :web在线流程设计器.预览流程xml.导出xml.部署流程 2.流程管理    :导入导出流程资源文件.查看流程图.根据流程实例反射出流程模型.激活挂起 3.运行中 ...

  2. shell脚本-巡检内存使用率

    #!/bin/bash # by dreamer Q # 巡检内存脚本 #总内存大小 mem_total=`free -m | sed -n '2p' |awk '{print $2}'` #已使用内 ...

  3. 【记录】logstash 的filter 使用

    概述 logstash 之所以强大和流行,与其丰富的过滤器插件是分不开的 过滤器提供的并不单单是过滤的功能,还可以对进入过滤器的原始数据进行复杂的逻辑处理,甚至添加独特的新事件到后续流程中 强大的文本 ...

  4. 17-正交矩阵和Gram-Schmidt正交化

    一.视频链接 1)正交矩阵 定义:如果一个矩阵,其转置与自身的乘积等于单位向量,那么该矩阵就是正交矩阵,该矩阵一般用Q来表示,即$Q^TQ=QQ^T=I$,也就是$Q^T=Q^{-1}$,即转置=逆 ...

  5. python关键字global和nonlocal总结

    函数中使用全局变量 a = 100 b = 200 def func(): def sub(): return b return a + b + sub() 执行fun()后返回值为:500 a, b ...

  6. linux篇之Nginx web服务器简单部署

    一.安装部署nginx 1. 部署前先对nginx介绍下别嫌BB: 如果你听说或使用过Apache软件,那么很快就会熟悉Nginx软件,与Apache软件类似, Nginx(“engine x”)是一 ...

  7. Opacity函数-transparentize()、 fade-out()函数

    transparentize() 和 fade-out() 函数所起作用刚好与 opacify() 和 fade-in() 函数相反,让颜色更加的透明.这两个函数会让透明值做减法运算,当计算出来的结果 ...

  8. [BZOJ2341][Shoi2011]双倍回文 manacher+std::set

    题目链接 发现双倍回文串一定是中心是#的回文串. 所以考虑枚举#点.发现以\(i\)为中心的双倍回文的左半部分是个回文串,其中心一定位于\(i-\frac{pal[i]-1}2\)到\(i-1\)之间 ...

  9. CQOI2007 余数之和

    Time Limit: 5 Sec Memory Limit: 128 MB Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + ...

  10. CentOS7.5 安装部署Apache+Mysql+Php

    系统:CentOS7.5 安装Apache 安装 yum -y install httpd 开启apache服务 systemctl start httpd.service 设置apache服务开机启 ...