poj 2229

Sumsets
Time Limit: 2000MS   Memory Limit: 200000K
Total Submissions: 21281   Accepted: 8281

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

Source

题意:用2的幂次(1,2,4,8...)表示N,有多少种表示方法。
题解:
【完全背包】:
  定义:dp[i][j]表示前i个2的幂表示j有多少种方法,则dp[i][j]=Σ{dp[i-1][j-k*c[i]]|0<k*c[i]<=N}
  

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e6;
const int mod=1e9; int f[maxn]; int main()
{
int N,M; while(scanf("%d",&N)==)
{
memset(f,,sizeof(f)),f[]=;
for(int i=;; i++)
{
int t=(<<(i-));
if(t>N) break;
for(int j=t; j<=N; j++)
{
f[j]+=f[j-t];
while(f[j]>mod) f[j]-=mod;
}
}
printf("%d\n",f[N]);
}
return ;
}

【递推dp】:

  定义:dp[N]表示答案。当N为奇数时,dp[N]=dp[N-1];当N为偶数时,若组成N的数中没有1,则把这些数除以2,就是dp[N/2];若有1(显然有1就至少有两个1),则去掉两个1,相当于是dp[N-2].所以dp[N]=dp[N/2]+dp[N-2]

  

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e6;
const int mod = 1e9; int dp[maxn]; int main()
{
int n;
cin >> n;
memset(dp, , sizeof(dp));
dp[] = , dp[] = , dp[] = , dp[] = ;
for (int i = ; i <= n; i++) {
if (i % == ) dp[i] = dp[i - ]%mod;
else dp[i] = (dp[i - ] + dp[i / ]) % mod;
}
cout << dp[n] << endl;
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e6;
const int mod=1e9; int dp[maxn+]; int dfs(int n)
{
if(dp[n]>) return dp[n];
if(n%==)
return dp[n]=dfs(n-)%mod;
else
return dp[n]=(dfs(n-)+dfs(n/))%mod;
} int main()
{
memset(dp,,sizeof(dp));
int n;
dp[]=,dp[]=,dp[]=,dp[]=;
while(scanf("%d",&n)==)
{
printf("%d\n",dfs(n));
}
return ;
}

poj 2229 【完全背包dp】【递推dp】的更多相关文章

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

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

  2. D. Caesar's Legions 背包Dp 递推DP

    http://codeforces.com/problemset/problem/118/D 设dp[i][j][k1][k2] 表示,放了i个1,放了j个2,而且1的连续个数是k1,2的连续个数是k ...

  3. 递推DP URAL 1167 Bicolored Horses

    题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...

  4. 递推DP URAL 1017 Staircases

    题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...

  5. 递推DP URAL 1260 Nudnik Photographer

    题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...

  6. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

  7. 递推DP URAL 1119 Metro

    题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...

  8. 递推DP 赛码 1005 Game

    题目传送门 /* 递推DP:官方题解 令Fi,j代表剩下i个人时,若BrotherK的位置是1,那么位置为j的人是否可能获胜 转移的时候可以枚举当前轮指定的数是什么,那么就可以计算出当前位置j的人在剩 ...

  9. 递推DP HDOJ 5328 Problem Killer

    题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...

随机推荐

  1. 原生JS与JQ获取元素的区别

    刚学JQ不久,有时候可能会把JS和JQ获取元素的方式搞错,接下来获取属性方法什么的就一发不可收拾了,现在把两者获取获取元素的代码整理下. 一.原生JS获取元素. 1.常用的三种方式获取元素对象(将指定 ...

  2. 016-WebDriver API(2)

    1. 多表单切换 WebDriver只能在一个页面上对元素进行识别和定位,无法直接定位frame/iframe表单内嵌页面上的元素,这是就需要通过switch_to.frame()方法将当前定位的主体 ...

  3. HBase入门实例: Table中Family和Qualifier的关系与区别

    Table中Family和Qualifier的关系与区别 就像用MySQL一样,我们要做的是表设计,MySQL中的表,行,列的在HBase已经有所区别了,在HBase中主要是Table和Family和 ...

  4. 巧用tar命令

    tar命令可以对文件进行归档.它最初设计是用来将数据存储在磁带上.tar可以将多个文件和文件夹保存为单个文件,同时还能保留所有的文件属性,如所有者.权限等.由tar创建的文件通常称为Tarball.下 ...

  5. 生成中国地区随机IP

    #随机生成IP 中国区    public function randip($member){        if($member['user_ip']){            if($member ...

  6. 实体类No default constructor found 找不到默认构造函数;

    root cause org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [c ...

  7. 使用Httpclient 完美解决服务端跨域问题

    项目需求: jsonp是从前台js的角度考虑,通过Ajax调用springMVC的接口.同一个ip.同一个网络协议.同一个端口,三者都满足就是同一个域,否则就是跨域问题了.首页广告需要一个轮播的效果, ...

  8. 2019.9.27 csp-s模拟测试53 反思总结

    这个起名方式居然还有后续?! 为什么起名不是连续的?! T1想了半天,搞出来了,结果数组开小[其实是没注意范围].T2概率期望直接跳,后来翻回来写发现自己整个理解错了期望的含义[何].T3错误想到赛道 ...

  9. python 内置操作函数

  10. bzoj2049: [Sdoi2008]Cave 洞穴探测

    bzoj2049: [Sdoi2008]Cave 洞穴探测 给n个点,每次连接两个点或切断一条边,保证是树结构,多次询问两个点是否联通 Lct裸题 //Achen #include<algori ...