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. Twitter Storm中Topology的状态

    Twitter Storm中Topology的状态 状态转换如下,Topology 的持久化状态包括: active, inactive, killed, rebalancing 四个状态. 代码上看 ...

  2. UVA10253 Series-Parallel Networks

    Series-Parallel Networks https://vjudge.net/problem/UVA-10253 如果用一个节点表示串联/并联操作,用一棵树表示每一个串并联网络,要求一个节点 ...

  3. Ionic Cordova 环境配置window

    1.安装java jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2.安 ...

  4. centos apache安装oracle扩展

    参考网址: http://blog.csdn.net/a82168506/article/details/11763989 步骤如下: 下载安装包,下载地址.(我下载的11.1版本) http://w ...

  5. 使用setTimeout函数解决栈溢出问题

    下面的代码,如果队列太长会导致栈溢出,怎样解决这个问题并且依然保持循环部分: var list = readHugeList(); var nextListItem = function() { va ...

  6. HDU1709

    /*  * 好奇怪的母函数  */ #include<cstdio> #include<cstring> #include<cmath> #include<a ...

  7. fill memset, for小测试

    /*很无聊写着玩玩,后来发现memset效率会比fill高出这么多,可惜一般只用来赋值0,-1......以后可以用fill来偷偷懒了...*/ #include<iostream> #i ...

  8. WebGis二次开发包实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs& ...

  9. 加载selenium2Library失败---robotframework环境搭建(site-packages下无selenium2library文件夹)

    加载Selenium2library失败,检查D:\Python27\Lib\site-packages 目录下是否有Selenium2Library 目录,没有该目录,事情就尴尬了. 自己安装的版本 ...

  10. RQNOJ PID141 / 寻找代表元 [2017年6月计划 二分图01]

    PID141 / 寻找代表元 ☆ 提交你的代码 查看讨论和题解 1分前 我的状态 已通过 2017-06-28 21:03:46 运行耗时:31 ms 运行内存:28048 KB 查看最后一次评测记录 ...