背包解组合数学问题,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. CH Round #54 - Streaming #5 (NOIP模拟赛Day1)解题报告

    最近参加了很多CH上的比赛呢~Rating--了..题目各种跪烂.各种膜拜大神OTZZZ T1珠 描述 萌蛋有n颗珠子,每一颗珠子都写有一个数字.萌蛋把它们用线串成了环.我们称一个数字串是有趣的,当且 ...

  2. Java for LeetCode 209 Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  3. 在Debian上用Bind 配置DNS服务器

    1 什么是DNS 初学者可能不理解DNS到底是什么,干什么用.我是在1998年大学毕业时才听说这个词的.那时我在聊天室碰到潍坊信息港的一个网管,我恬不知耻地说我也是个网管,他说也维护DNS吗?我说,D ...

  4. Effective C++ -----条款10: 令operator=返回一个reference to *this

    比如: Widget& operator=(const Widget& rhs) { ... return* this; } 令赋值(assignment)操作符返回一个referen ...

  5. 在微信浏览器中如何让他自动关闭当前页面回到会话框js

    <script type="text/javascript"> wx.config(jssdkconfig); require(['jquery', 'util'], ...

  6. PHP如何判断远程图片文件是否存在

    <?php $url = 'http://www.nowamagic.net/images/test.jpg'; if( @fopen( $url, 'r' ) ) { echo 'File E ...

  7. Express 4 中如何使用connect-mongo

    正在跟随上面的教程一步一步做,在会话支持那一节中安装 connect-mongo 后,添加: var MongoStore = require('connect-mongo')(express); v ...

  8. 在程序中使用geos.dll

    1 在项目->property->configuration properties->c/c++->general->additional include directo ...

  9. 在VC中创建并调用DLL

    转自:http://express.ruanko.com/ruanko-express_45/technologyexchange6.html 一.DLL简介 1.什么是DLL? 动态链接库英文为DL ...

  10. ssh配置免密码登录

    日常工作中很多情况下都需要登录服务器进行管理,一般都是用ssh进行连接,为了防止密码外泄,可以配置下ssh的免密码登录. 首先服务器两台: A:43.224.34.* B:104.238.161.* ...