背包解组合数学问题,n种物品,每种num[i]个,求取r个的方法数。

背包思想,f[j]表示当前取j个数的方法数,则状态转移方程为

f[j] += f[k](max(j - num[i], 0) <= k < j)

外层循环枚举物品,内层循环从大到小枚举空间,最内层枚举方法数。

#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. Leetcode 题解 Combinations:回溯+求排列组合

    罗列出从n中取k个数的组合数组. 首先,求C(n,k)这个实现,很粗糙,溢出也不考虑,好的方法也不考虑.笨蛋.心乱,上来就写.. 另外,发现在递归中,不能申请太大的数组?貌似不是这个问题,是我自己越界 ...

  2. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

  3. LeetCode OJ:Combinations (排列组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  4. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  5. leetcode-Combinations 复习复习排列组合

    Combinations 题意: 根据给定的n和k,生成从1到n范围内长度为k的排列组合 示例: n=4 k=2 [[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2 ...

  6. DFS实现排列组合

    所谓排列,是指从给定的元素序列中依次取出元素,需要考虑取出顺序.比如,取出元素3, 5,因取出顺序的不同,则形成的序列{3, 5}与{5, 3}是不同的排列序列.对于长度为n的元素序列取出k个元素,则 ...

  7. 【Python】排列组合itertools & 集合set

    ■itertools 利用python的itertools可以轻松地进行排列组合运算 itertools的方法基本上都返回迭代器 比如 •itertools.combinations('abcd',2 ...

  8. python排列组合之itertools模块

    1. 参考 几个有用的python函数 (笛卡尔积, 排列, 组合) 9.7. itertools — Functions creating iterators for efficient loopi ...

  9. Python实现排列组合

    # -*- coding: utf-8 -*-"""Created on Sat Jun 30 11:49:56 2018 @author: zhen"&quo ...

随机推荐

  1. 简单的哈希表实现 C语言

    简单的哈希表实现 简单的哈希表实现 原理 哈希表和节点数据结构的定义 初始化和释放哈希表 哈希散列算法 辅助函数strDup 哈希表的插入和修改 哈希表中查找 哈希表元素的移除 哈希表打印 测试一下 ...

  2. 20 BasicTaskScheduler0 基本任务调度类基类(二)——Live555源码阅读(一)任务调度相关类

    这是Live555源码阅读的第二部分,包括了任务调度相关的三个类.任务调度是Live555源码中很重要的部分. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/ol ...

  3. Droid4x安装busybox

      下载Busybox                                                                                         ...

  4. MySql大数据量恢复

    用下面方法解决(管理mysql用的是navicat).,设置以下几个参数的值后就正常了,以下语句也可以在mysql的控制台上执行 . show variables like '%timeout%'; ...

  5. ASIO插件 真的能提升 音质?(听音乐者必看)

    最近在倒弄HIFI音乐播放器footbar2000的配置时,发现了2011年的一个神贴, 最牛逼的是,这个神贴到现在还屹立不倒,还有很多无知的人在下面膜拜, 我真的想问:你这么优秀,都能逆天反转音质, ...

  6. stty命令使用

    stty [ -a ] [ -g ] [ Options ]   stty(set tty)命令用于显示和修改当前注册的终端的属性. UNIX系统为键盘的输入和终端的输出提供了重要的控制手段,可以通过 ...

  7. JQuery 遍历 - prev() 方法

    http://www.w3school.com.cn/jquery/traversing_prev.asp http://www.w3school.com.cn/jquery/jquery_ref_t ...

  8. float 在内存中如何存储的

    float类型数字在计算机中用4个字节存储.遵循IEEE-754格式标准:    一个浮点数有2部分组成:底数m和指数e 底数部分 使用二进制数来表示此浮点数的实际值指数部分 占用8bit的二进制数, ...

  9. Java for LeetCode 218 The Skyline Problem【HARD】

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  10. web.config中配置页面出错后跳转指定错误页面

    每当用户访问错误页面时,会出现不友好的404错误,所以为了防止这种不友好,我们在web.config中的<system.web>节点下配置 <customErrors>,在出现 ...