POJ1285 Combinations, Once Again(背包 排列组合)
背包解组合数学问题,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(背包 排列组合)的更多相关文章
- Leetcode 题解 Combinations:回溯+求排列组合
罗列出从n中取k个数的组合数组. 首先,求C(n,k)这个实现,很粗糙,溢出也不考虑,好的方法也不考虑.笨蛋.心乱,上来就写.. 另外,发现在递归中,不能申请太大的数组?貌似不是这个问题,是我自己越界 ...
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- LeetCode OJ:Combinations (排列组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [leetcode] 题型整理之排列组合
一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...
- leetcode-Combinations 复习复习排列组合
Combinations 题意: 根据给定的n和k,生成从1到n范围内长度为k的排列组合 示例: n=4 k=2 [[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2 ...
- DFS实现排列组合
所谓排列,是指从给定的元素序列中依次取出元素,需要考虑取出顺序.比如,取出元素3, 5,因取出顺序的不同,则形成的序列{3, 5}与{5, 3}是不同的排列序列.对于长度为n的元素序列取出k个元素,则 ...
- 【Python】排列组合itertools & 集合set
■itertools 利用python的itertools可以轻松地进行排列组合运算 itertools的方法基本上都返回迭代器 比如 •itertools.combinations('abcd',2 ...
- python排列组合之itertools模块
1. 参考 几个有用的python函数 (笛卡尔积, 排列, 组合) 9.7. itertools — Functions creating iterators for efficient loopi ...
- Python实现排列组合
# -*- coding: utf-8 -*-"""Created on Sat Jun 30 11:49:56 2018 @author: zhen"&quo ...
随机推荐
- 7.7---找只含3,5,7的数(CC150)
----思路:利用三个队列,一个存3,一个存5,一个存7. 然后,3*3的都放第一个.然后3*5,5*5的放第二个.然后,3*7,5*7,7*7的都放第三个. 答案: public static in ...
- ubuntu下编码转换工具
ubuntu打开windows下的txt或者代码文件,经常会出现乱码, ubuntu自带一种转换工具,是命令行的,下面提供一种最简单的方法进行转换 比如要转换的文件为1.txt,进入1.txt的目录 ...
- Openstack Neutron 允许VM流量转发
neutron port-update <port-id> --port-security-enabled=False --no-security-groups
- 项目中Ajax调用ashx页面中的Function的实战
前台页面: 使用几个display=none的空间存储DropdownList中的值,点击Search Button后刷新页面再次给DropdownList赋值使用 <%@ Page Langu ...
- Post方法调用公司发Mail的接口
调用公司发Mail的接口. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...
- 【leetcode】Next Permutation
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...
- CEF3开发者系列之CEF3入门
CEF全称Chromium Embedded Framework,是一个基于Google Chromium 的开源项目.Google Chromium项目主要是为Google Chrome应用开发的, ...
- 4个http常用的content type
转的: http://www.aikaiyuan.com/6324.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TR ...
- iOS基础框架的搭建/国际化操作
1.基础框架的搭建 1.1 pod引入常用的第三方类库 1.2 创建基础文件夹结构/目录结构 Resource———存放声音/图片/xib/storyboard 等资源文件 Define——宏定义, ...
- 【leetcode】Largest Number ★
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...