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数据库_索引_事务_优化 _锁_存储引擎_存储过程_CAP

    ##一.基础 ## *    插入                   INSERT INTO table_name ( field1, field2,...fieldN )              ...

  2. Pywinauto自动化操作PC微信提取好友微信号

    声明:https://zhuanlan.zhihu.com/p/29944988#! /usr/bin/env python #coding=utf-8 #pywinauto自动化操作微信号 #by ...

  3. No module named 'sklearn.impute',更新scikit-learn

    -------我错了,本篇作废,我把自己的包更新坏了,大家不要往下看了------------------最终我是把anaconda卸载重装的--------- 使用scikit-learn模块进行缺 ...

  4. TZOJ 2478 How many 0's?(数位DP)

    描述 A Benedict monk No.16 writes down the decimal representations of all natural numbers between and ...

  5. 【转载】传统以太网和时间敏感网络TSN的区别

    转载连接:http://www.proav-china.com/News/16800.html ——Biamp亚太区高级工程师   Kane Zhang [专业视听网报道]:[摘要]AVB-Audio ...

  6. 斐波那契字符串_KMP

    前言:通过这道题恶补了一下字符串匹配的知识 思路:首先就是求出菲波那切字符串,这个很简单,但是要注意递归超时的问题,可以考虑加上备忘录,或者用递推法,接下来就是匹配问题了,常规的BF会超时,所以要用K ...

  7. ACdream 1108(莫队)

    题目链接 The kth number Time Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) ...

  8. C# 多线程的代价~内存都被吃了!

    异步操作是.net4.5推出的新名词,事实上,这东西早就有了,它归根结底是通过线程池来实现的,即将一个大任务分成多个小任何块,每个线程并行处理其中的一个,完成后再把结果告诉主线程,在.net4.5推出 ...

  9. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---策略模式之MiniDuckSimulator[转]

     1  2{<HeadFirst设计模式>之策略模式 }  3{ 本单元中的类为策略类           }  4{ 编译工具: Delphi7.0           }  5{ E- ...

  10. WebBrowser修改默认白色背景

      背景:在使用Winform的WebBrowser显示网页的时候,在网页还未加载完成之前会显示白色,刚好网页内容呈现黑色,这样视觉效果上就十分差,想把这层白色的去掉. 试了很久之后发现根本去不掉,应 ...