描述


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. sql server 的JDBC 配置

    1.java中使用数据库需要用JDBC(java database connection)来连接和操作 下载地址:http://www.microsoft.com/en-us/download/det ...

  2. 【失败】制作CentOS镜像

    1.在vmware上安装centos虚拟机,安装过程中设置CPU时,点选 虚拟化Intel VT-x~ 和 虚拟化CPU性能计数器,网络使用NAT模式 2.#mkdir /opt/iso,上传镜像(用 ...

  3. Inline functions

    Problems: (Page 372) There are two problems with the use of proprocessor macros in C++. The first is ...

  4. IOS 学习笔记 2015-03-20 O之 nil,Nil,NULL,NSNull

    1.oc最好 用nil   [ nil  任意方法],不会崩溃 nil 是一个对象值.NULL是一个通用指针(泛型指针). 2. NSNULL,NULL和nil在本质上应该是一样的,NULL和nil其 ...

  5. Linux中的堡垒--iptables

    iptables的构成(四表五链)     表         filter:过滤数据包         nat :转换数据包的源或目标地址         mangle:用来mangle包,改变包的 ...

  6. Nginx常用伪静态规则(rewrite)-Discuz X3.2-WordPress-PHPCMS-ECSHOP-SHOPE(转)

    当我们从apache服务器转向Nginx服务器的时候,它们的伪静态规则就不一样了,所以你熟悉Nginx服务器的伪静态规则,自己写当然也好.不了解Nginx服务器的伪静态规则的,为方便各位站长,收集了这 ...

  7. ES6笔记-字符串方法

    字符串检索方法,indexOf(searchValue,fromIndex)//参数1必需,检索查询的字符串或者值,参数2选题,规定检索的起始位置,不设置默认从0开始 indexOf()方法返回检索字 ...

  8. onbeforeunload、beforeunload

    <script type="text/javascript"> function addOnBeforeUnload(e) {     var ev = e || ev ...

  9. Ubuntu15.10 编译VLC Android(安卓)过程记录

    持续更新中... 最后一次修改于 2016-03-20 15:33:45 1.必要库的安装 除基本编译环境(gcc.g++等外),需要额外安装如下的库(用于下载必要的依赖文件) (1)JDK 推荐安装 ...

  10. 实现目标文件与源码分开的makefile测试实验

    uboot提供了两种编译策略,即可以将生成的目标文件与源码混在一起,也可以将生成的目标文件与源码分开.通过对uboot Makefile的分析,笔者编写了一个简单的实现这种功能的Makfile. 顶层 ...