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 ...
随机推荐
- No enum constant org.apache.ibatis.type.JdbcType.xxx 错误
配置文件Mapper中JdbcType错误,有可能是大小写,有可能是拼写错误
- ndk学习12: 线程
- 在Navicat for MySQL中打开视图时,提示视图没有主键的问题
一直把视图理解为一个select语句而已,视图一般就是用于查询,不会通过视图来更新表或视图本身的数据,所以视图根本不需要什么主键.今天自己建了一个视图view_test: drop view if e ...
- 使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)
使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe) 原理: 1.使用FileReader 读取图片的base64编码 2.使用ajax,把图片的base64编码 ...
- Best Meeting Point
Total Accepted: 701 Total Submissions: 1714 Difficulty: Medium A group of two or more people wants t ...
- 【转】Git如何Check Out出指定文件或者文件夹
[转]Git如何Check Out出指定文件或者文件夹http://www.handaoliang.com/a/20140506/195406.html 在进行项目开发的时候,有时候会有这样的需求那就 ...
- javascript开发 ios和android app的简单介绍
先看几个名词解释: nodejs ionic,Cordova,phoneGap,anjularjs react-native,reactjs nodeJs 的介绍参见这里,写的很好http://www ...
- win8内置管理员用户无法激活此应用
在运行中输入:“gpedit.msc”,就会启动组策略编辑器, 计算机配置 --> Windows设置 --> 安全设置 --> 本地策略 --> 安全选项 :::: 用 ...
- ACM/ICPC 之 最长公共子序列计数及其回溯算法(51Nod-1006(最长公共子序列))
这道题被51Nod定为基础题(这要求有点高啊),我感觉应该可以算作一级或者二级题目,主要原因不是动态规划的状态转移方程的问题,而是需要理解最后的回溯算法. 题目大意:找到两个字符串中最长的子序列,子序 ...
- ffmpeg-20160714-git-bin.7z
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...