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. MySQL数据库基本使用

    一 .数据库概述 数据库就是以一定格式进行组织的数据的集合.通俗来看数据库就是用户计算机上 一些具有特殊格式的数据文件的集合. 数据库也可以理解为表格,大家都知道表格都是由表名.表头.数据等几部分组成 ...

  2. hdu3853之概率dp入门

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xingyeyongheng/article/details/25205693 LOOPS Time ...

  3. 2018-8-14-Resharper-如何把类里的类移动到其他文件

    title author date CreateTime categories Resharper 如何把类里的类移动到其他文件 lindexi 2018-08-14 17:34:39 +0800 2 ...

  4. 微信网页授权demo2

    1.在微信公众号请求用户网页授权之前,开发者需要先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名.请注意,这里填写的是 ...

  5. NPOI 1.1

    1 .NPOI 版本2.1 2. NPOI 读取execl 3.导入数据(SqlBulkCopy) 示例代码: public class ImportServerData { DataTable dt ...

  6. Hosts 广告

    # 百度 127.0.0.1 cpro.baidustatic.com 127.0.0.1 dup.baidustatic.com 127.0.0.1 hm.baidu.com 127.0.0.1 i ...

  7. SpringBoot+Shiro+mybatis整合实战

    SpringBoot+Shiro+mybatis整合 1. 使用Springboot版本2.0.4 与shiro的版本 引入springboot和shiro依赖 <?xml version=&q ...

  8. Spring事务传播行为详解

    前言 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为.事务传播行为是Spring框架独有的事务增强特性,他不属于的事务实际提供方数据库行为.这是Spring ...

  9. 从Java到C++——union的使用方法

    版权声明:本文为博主原创文章,未经博主同意不得用于不论什么商业用途,转载请注明出处. https://blog.csdn.net/luoweifu/article/details/33342965 你 ...

  10. POJ1635Subway tree systems

    Subway tree systems Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8049   Accepted: 33 ...