描述


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. webapp构建工具库

    Meteor:JavaScript App Platform braintree:在线支付 jquery datetimepicker:日期控件 Hotjar Tracking Code:网站追踪 Z ...

  2. PHP学习笔记(六)

    <Wordpress 50个过滤钩子> 1-10 过滤钩子是一类函数,wordpress执行传递和处理数据的过程中,在针对这些数据做出某些动作之前的特定点执行.本质上,就是在wordpre ...

  3. java新手笔记2 数据类型

    1.注释 /** doc注释 * 类说明信息 */ //声明类 文件名与类名一致 public class World {//类定界符 //声明方法 main方法 public static void ...

  4. ECMA中关于if与else的关系的一句英文,感觉比较经典

    Each else for which the choice of assocated if is ambiguous shall be associated with the nearest pos ...

  5. 检查mysql数据库是否存在坏表脚本

    #!/bin/bash #此脚本的主要用途是检测mysql服务器上所有的db或者单独db中的坏表 #变量说明 pass mysql账户口令 name mysql账号名称 data_path mysql ...

  6. PHP curl 采集内容之规则 及图片下载方法2

    <?phpheader("Content-type:text/html; charset=utf-8");/*$pattern = '/xxx(.*)yyyy/isU'; / ...

  7. 浏览器中输入URL到返回页面的全过程

    第一步,解析域名,找到主机IP (1)浏览器会缓存DNS一段时间,一般2-30分钟不等.如果有缓存,直接返回IP,否则下一步. (2)缓存中无法找到IP,浏览器会进行一个系统调用,查询hosts文件. ...

  8. STM32库函数实现方法

    一.概述 1.调用STM32库函数配置与直接配置寄存器 ① 直接配置寄存器 使用过51单片机的朋友都知道为了将IO口配置成某种特殊功能或者配置中断控制,我们先将需要如下步骤: 根据需要配置功能计算值- ...

  9. linux系统装windows时需要注意的问题

    (1)    利用windows安装光盘安装XP.WIN7系统时,sata接口的硬盘要将其设置为兼容模式或者IDE模式才能安装.我认为这些盗版光盘安装系统的软件并没有支持sata接口硬盘的驱动程序才导 ...

  10. 配置mybatis错误总结

    ### The error may exist in SQL Mapper Configuration ### Cause: org.apache.ibatis.builder.BuilderExce ...