【POJ - 2229】Sumsets(完全背包)
2024-09-01 03:53:41
Sumsets
直接翻译了
Descriptions
Farmer John 让奶牛们找一些数加起来等于一个给出的数N。但是奶牛们只会用2的整数幂。下面是凑出7的方式
1) 1+1+1+1+1+1+1
2) 1+1+1+1+1+2
3) 1+1+1+2+2
4) 1+1+1+4
5) 1+2+2+2
6) 1+2+4
帮助FJ找到 N的分配数 (1 <= N <= 1,000,000).
Input
N
Output
排列方式总数。由于这个数可能很大,只需要保留最后9位
Sample Input
7
Sample Output
6
Hint
打表的会被系统自动识别判为WA
题目链接
https://vjudge.net/problem/POJ-2229
处理出2的幂次方的所有的数字,当做物品,每个物品次数不限,求凑出体积为N的方案数
类似完全背包,先枚举物品,再正序枚举体积,转移状态dp[i][j]表示前i件物品凑出的体积为j的方案数
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - w[i]]
1<<i 相当于 2i
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 1000005
using namespace std;
int n;
int w[Maxn];
int cnt=;
int dp[Maxn];
int main()
{
scanf("%d",&n);
for(int i=;(<<i)<=n;i++)//构造所有物品
w[cnt++]=(<<i);
dp[]=;
for(int i=;i<cnt;i++)
for(int j=w[i];j<=n;j++)
dp[j]=(dp[j]+dp[j-w[i]])%;//取余 printf("%d\n",dp[n]);
return ;
}
最新文章
- android学习第一篇 基本概念
- Mac下Android Studio中获取SHA1和MD5
- BZOJ-1015 StarWar星球大战 并查集+离线处理
- 【poj1740】 A New Stone Game
- NetCDF 格式化数据概述
- 新建的表如果还没有数据,用exp导的时候会忽略
- Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析(转载)
- 彩票APP将演绎“快鱼吃慢鱼”的发展轨迹
- oracle 设置标识列自增
- Eclipse查看历史代码
- codeforces 518A. Vitaly and Strings
- 锅巴H264播放器地址和说明
- 草料Chrome浏览器插件,让二维码更好用
- 访问远程的docker
- phantomjs的安装和使用链接
- Oracle 如何开启归档模式
- Mybatis多个in查询
- [转]用国内软件源为Ubuntu的apt-get提速方法
- UVA-10273 Cyborg Genes (DP)
- C# 爬取网页上的数据
热门文章
- Docker环境下的前后端分离项目部署与运维(十二)使用Portainer管理Docker
- scrapy基础知识之 scrapy 三种模拟登录策略:
- 《C#并发编程经典实例》学习笔记—2.7 避免上下文延续
- python3 连接数据库注意点
- 嵊州D2T3 玛利亚∙多斯普拉泽雷斯 完美配对
- Spring Cloud 之 Hystrix.
- SQL Server 根据日期分组、 根据时间段分组(每三个小时一组)
- android_alertDialog
- 用Python玩数据-笔记整理-第二章-练习与测试
- redux、react-redux、redux-thunk、redux-saga使用及dva对比