背包解组合数学问题,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. java78_c

    import java.util.*; public class Main { public static void main(String args[]){ Scanner cin=new Scan ...

  2. Python自动化之常用模块

    1 time和datetime模块 #_*_coding:utf-8_*_ __author__ = 'Alex Li' import time # print(time.clock()) #返回处理 ...

  3. java文件和文件夹复制、删除、移动操作

    java文件和文件夹复制.删除.移动操作 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputS ...

  4. GIT文件的三种状态

    对于任何一个文件,在 Git 内都只有三种状态:已提交(committed),已修改(modified)和已暂存(staged).已提交表示该文件已经被安全地保存在本地数据库 中了:已修改表示修改了某 ...

  5. wxPython+Boa Constructor环境配置

    配置之前先完成eclipse + Pydev的配置环境.详见http://www.cnblogs.com/dflower/archive/2010/05/13/1734522.html 1. 安装 w ...

  6. Unity3d《Shader篇》绘制圆角图片

    Pass { CGPROGRAM // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a s ...

  7. ios Tabbar Item 的图标

    1,tabBarItem图片的推荐尺寸和最大支持尺寸 下面是标签栏(UITabBar)中tab按钮图标分别在1x.2x.3x下不会压缩变形的尺寸: @1x : 推荐 25 x 25   (最大: 48 ...

  8. VC++ 获取当前模块的路径(dll/exe)

    一般地,获取当前模块路径都是通过调用 GetModuleFileName() 来获取的. DWORD WINAPI GetModuleFileName( __in HMODULE hModule, _ ...

  9. linux下QT Creator常见错误及解决办法

    最近因为在做一个关于linux下计算机取证的小项目,需要写一个图形界面,所以想到了用QT来写,选用了linux下的集成开发环境QT Creator5.5.1,但刚刚安装好,竟然连一个"hel ...

  10. WCF服务跟踪

    如果在开发过程中,WCF服务出现问题,我们可以通过服务引用,然后直接断点调试进去.然而,对于已经发布的服务,出现错误时,寻找错误信息会变得麻烦. 幸好,微软提供了服务跟踪查看器工具 (SvcTrace ...