题目链接:http://poj.org/problem?id=2229

Sumsets

Time Limit: 2000MS Memory Limit: 200000K

Total Submissions: 21845 Accepted: 8454

Description

Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 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

Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000).

Input

A single line with a single integer, N.

Output

The number of ways to represent N as the indicated sum. Due to the potential huge size of this number, print only last 9 digits (in base 10 representation).

Sample Input

7

Sample Output

6


解题心得:

  1. 问给你一系列2的N次方的数,让你用这些数相加起来等于m,问一共有多少种方法。
  2. 刚开始看到这个题的时候第一个反应就是青蛙跳台阶的问题(链接),按照这个思路状态转移就出来了。dp[n][m] += dp[n-1][m-k*c[i]],在空间上优化可以使用滚动数组来进行优化。这样还是会TLE,因为没有优化过的完全背包是三重循环,这个时候就需要用到完全背包的优化,完全背包的优化其实很简单,思想就是既然背包有无穷多个,那么直接从小到大开始叠加就行了,会自然叠加到最大,这样就可以省去k个背包的循环,利用的就是k无穷大不用一一进行枚举。可以很简单的看懂优化代码。

没有优化过的完全背包(大概写法):

for(int i=0;i<n;i++) {
for(int k=1;k*c[i] <= n;k++) {
for(int j=m;j>=k*c[i];k--) {
dp[j] += dp[j-k*c[i]];
}
}
}

完全背包的时间优化(大概写法):

for(int i=0;i<n;i++) {
for(int j=c[i];j<=n;j++) {//注意这里是从小到大开始叠加
dp[j] += dp[j-c[i]];
}
}

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn = (int) 1e6 + 10;
int n, dp[maxn];
int Mod = (int) 1e9; int main() {
int T = 1;
while (~scanf("%d", &n)) {
memset(dp, 0, sizeof(dp));
dp[0] = 1;
while (T <= n) {
for (int j = T; j <= n; j++) {
dp[j] += dp[j - T];
dp[j] %= Mod;
}
T <<= 1;
}
printf("%d\n", dp[n]);
return 0;
}
}

POJ:2229-Sumsets(完全背包的优化)的更多相关文章

  1. poj 2229 Sumsets 完全背包求方案总数

    Sumsets Description Farmer John commanded his cows to search for different sets of numbers that sum ...

  2. poj 2229 【完全背包dp】【递推dp】

    poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 828 ...

  3. POJ 2229 Sumsets(技巧题, 背包变形)

    discuss 看到有人讲完全背包可以过, 假如我自己做的话, 也只能想到完全背包了 思路: 1. 当 n 为奇数时, f[n] = f[n-1], 因为只需在所有的序列前添加一个 1 即可, 所有的 ...

  4. poj -2229 Sumsets (dp)

    http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...

  5. POJ 1014 Dividing(多重背包, 倍增优化)

    Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...

  6. POJ 2229 Sumsets

    Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 11892   Accepted: 4782 Descrip ...

  7. poj 2229 Sumsets(dp)

    Sumsets Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 400000/200000K (Java/Other) Total Sub ...

  8. POJ 2229 sumset ( 完全背包 || 规律递推DP )

    题意 : 给出一个数 n ,问如果使用 2 的幂的和来组成这个数 n 有多少种不同的方案? 分析 :  完全背包解法 将问题抽象==>有重量分别为 2^0.2^1.2^2…2^k 的物品且每种物 ...

  9. poj 2229 Sumsets DP

    题意:给定一个整数N (1<= N <= 1000000),求出以 N为和 的式子有多少个,式子中的加数只能有2的幂次方组成 如5 : 1+1+1+1+1.1+1+1+2.1+2+2.1+ ...

  10. poj 2229 Sumsets(dp 或 数学)

    Description Farmer John commanded his cows to search . Here are the possible sets of numbers that su ...

随机推荐

  1. centos yum 安装jdk后配置JAVA_HOME

    centos6.4 yum 安装jdk和JAVA_HOME的设置 2014-06-17 16:13 1176人阅读 评论(0) 收藏 举报  分类: linux(3)  版权声明:本文为博主原创文章, ...

  2. JSON语法格式

    一.JSON数据格式 名称/值对 二.JSON值对数据类型 数字    字符串   逻辑值    数组(在方括号中)     对象 (在花括号中)     null eg: { "staff ...

  3. html+css杂记

    overflow可设置超出后隐藏 子元素撑开父级元素:①父级元素宽度不固定,②父级元素设置为inline-block或者添加float: html中的中文在浏览器打开为乱码(已经写了<meta ...

  4. MarkDown 编辑器学习

    MarkDown 编辑器学习 是一种简单快键的文字排版工具,可以用于编写说明文档,鉴于其语法简洁明了,且其渲染生成的样式简单美观,很多开发者也用它来写博客,已被国内外很多流行博客平台所支持.生成的文件 ...

  5. Jquery插件之ajaxForm简介

    我们平常在使用jQuery异步提交表单的时候,一般都是加载在submit事件中,如下所示: $(document).ready(function(){ $('#myForm').submit(func ...

  6. linux文件按照行数切割

    公司小站的nginx服务器日志一直没有管理 到发现的时候已经有50G+的logs文件 查看起来十分不便 只能将其切割 接下来分享 具体方法 split -l 5000(行数) -a 5(增加文件名上限 ...

  7. Vim中根据正则对选中文本对齐(比如ini文件的=号对齐)

    vimrc增加如下内容即可: vnoremap <M-=> :call Duiqi('\v(^\s*\S+)\s+(.*)')<CR> "reg匹配的第2段文字对齐 ...

  8. IOS 4个容易混淆的属性(textAligment contentVerticalAlignment contentHorizontalAlignment contentMode)

    四个容易混淆的属性:1. textAligment : 文字的水平方向的对齐方式1> 取值NSTextAlignmentLeft      = 0,    // 左对齐NSTextAlignme ...

  9. (转载)Fiddler模拟post四种请求数据

    https://www.cnblogs.com/xiaoxi-3-/p/7612254.html https://blog.csdn.net/qq_15283475/article/details/5 ...

  10. 5.2 Array类型

    ◆  创建数组的基本方式有两种. ①第一种是使用Array构造函数,new关键字可省略 var colors = new Array(); var colors = new Array(20); // ...