描述


http://poj.org/problem?id=2229

将一个数n分解为2的幂之和共有几种分法?

Sumsets
Time Limit: 2000MS   Memory Limit: 200000K
Total Submissions: 16207   Accepted: 6405

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

分析


对i讨论:

1.i是奇数:

  分成的序列中必有1,所以可将i分为1+(i-1),所以f[i]=f[i-1];

2.i是偶数:
  (1).分成的序列中有1:

    同奇数,f[i]=f[i-1];

  (2).分成的序列中没有1:

    序列中的所有数都是2的倍数,那么任一种序列中的各个数/2,就得到了i/2的序列,那这种情况下,i的序列数就和i/2的序列数相同即f[i]=f[i/2];

  综上:f[i]=f[i-1]+f[i/2];

 #include<cstdio>

 const int maxn=,mod=1e9;
int n,f[maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
freopen("sum.out","w",stdout);
#endif
scanf("%d",&n);
f[]=;
for(int i=;i<=n;i++)
{
if(i&) f[i]=f[i-];
else f[i]=(f[i-]+f[i/])%mod;
}
printf("%d\n",f[n]);
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}

POJ_2229_Sumsets_(动态规划)的更多相关文章

  1. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  2. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  3. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  4. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  5. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  6. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. ORACLE no1 存储过程插入更新表数据

    CREATE OR REPLACE PROCEDURE sp_cust_main_data_yx(InStrDate  IN VARCHAR2,                             ...

  2. java内存

    java内存分为四部分: 1).栈区(stacksegment),由编译器自动分配释放,存放函数的参数值和局部变量的值等,具体方法执行结束之后,系统自动释放JVM内存资源: 2).堆区(heapseg ...

  3. 注册一个比较大小Handlebars

    1.显示的数据 var datas = { id:"number" } 2.temp模板 <script id="template" type=" ...

  4. Linux下的另一个词典GoldenDict

    以前一直在用statdict,突然发现了一个好用的东西Goldendict. 转载丁香园上一篇文章:http://www.dxy.cn/bbs/topic/20455142 Goldendict 话说 ...

  5. stack around the variable “XX” was corrupted

    晚上花了几个小时fix了这个恼人的BUG!“在变量XX周围的堆栈已损坏” 在网上找到的解释是: 把“project->配置属性->c/c++->代码生成->基本运行时检查 设置 ...

  6. php 常用五种模式

    /* 设计模式之单例模式 $_instance 必须声明为静态的私有变量 构造函数必须声明为私有,防止外部程序 new 类从而失去单例模式的意义 getInstance() 方法必须设置为公有的,必须 ...

  7. php重载

    重载 PHP所提供的"重载"(overloading)是指动态地"创建"类属性和方法.我们是通过 魔术方法(magic methods)来实现的. 当调用当前环 ...

  8. $_REQUEST变量数组header()函数

    $_SERVER 包含http信息头,路径和服务器端的一些信息,没发送一次HTTP请求,就会创建一个$_SERVER数组Array ( [HTTP_HOST] => localhost [HTT ...

  9. 基于ARM-LINUX的温度传感器驱动(DS18B20) .

    DS18B20数字温度传感器接线方便,封装成后可应用于多种场合,如管道式,螺纹式,磁铁吸附式,不锈钢封装式,型号多种多样,有LTM8877,LTM8874等等.主要根据应用场合的不同而改变其外观.封装 ...

  10. C++ 11 笔记 (一) : lambda

    时至今日都是我咎由自取,错就是错,与任何人无关.掉进C++98的各种坑里无法自拔的抖M感,让我选择了华丽丽的无视C++11,导致今日面对开源的代码到各种看不懂的地步,一入C++深似海,我今天愿意承担一 ...