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

当 i 为奇数时可以将 i-1 的展开项加 1 得到 i 的展开项
当 i 为偶数是单纯的将 i-1 的展开项加 1 无法得到所有的 i 的展开项,因为 i 是偶数,所以 i 的展开项中有全为偶数的情况
将全为偶数的展开像除以 2 得到的是 i/2 的展开项(可以倒着想,将 i/2 的展开项乘 2 得到 i 的全偶数展开项) 转移式为 dp[i] = (i&1)?(dp[i-1]):(dp[i-1]+dp[i/2])
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 1e6+;
int dp[maxn]; int main(){
int n;
scanf("%d",&n);
memset(dp,,sizeof(dp));
dp[] = ; for(int i=;i<=n;++i){
if(i&)
dp[i] = dp[i-];
else{
dp[i] = dp[i-] + dp[i>>];
if(dp[i]>)
dp[i] -= ;
}
} printf("%d\n",dp[n]);
return ;
}

POJ 2229 递推的更多相关文章

  1. 放苹果 POJ - 1664 递推

    把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第一行是测试数据的数目t(0 <= t < ...

  2. Number Sequence POJ - 1019 递推 数学

    题意 1 12 123 1234 12345 ....这样的序列 问第n位数字是几   是数字! 1-9! 思路:递推关系 主要是位数的计算   用a[i]=a[i-1]+(int)log10((do ...

  3. poj 2229 【完全背包dp】【递推dp】

    poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 828 ...

  4. 【POJ】2229 Sumsets(递推)

    Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 20315   Accepted: 7930 Descrip ...

  5. POJ 2229 Sumsets(递推,找规律)

    构造,递推,因为划分是合并的逆过程,考虑怎么合并. 先把N展开成全部为N个1然后合并,因为和顺序无关,所以只和出现次数有关情况有点多并且为了避免重复,分类,C[i]表示序列中最大的数为2^i时的方案数 ...

  6. POJ 2229 sumset ( 完全背包 || 规律递推DP )

    题意 : 给出一个数 n ,问如果使用 2 的幂的和来组成这个数 n 有多少种不同的方案? 分析 :  完全背包解法 将问题抽象==>有重量分别为 2^0.2^1.2^2…2^k 的物品且每种物 ...

  7. POJ 1664 放苹果 (递推)

    题目链接:http://poj.org/problem?id=1664 dp[i][j]表示i个盘放j个苹果的方案数,dp[i][j] 可以由 dp[i - 1][j] 和 dp[i][j - i] ...

  8. HOJ 2148&POJ 2680(DP递推,加大数运算)

    Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...

  9. POJ 2506 Tiling(递推+大整数加法)

    http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. #includ ...

随机推荐

  1. data-ng-show指令

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. Struts2 第一讲 -- Struts2开发前奏

    我们在学习Struts之前,先来复习一下Servlet,众所周知Servlet是JavaWeb的三大组件.我们发送一个请求,这个请求交给Servlet处理,Servlet将处理的结果返还给浏览器.每个 ...

  3. 【JAVA】学习笔记

    对程序员来说,学习一门新的语言,最开始的代码当然是hello world!下面我们从这段代码入手,一步一步来学习Java基础知识. class Hello{ public static void ma ...

  4. orcal 数据库 maven架构 ssh框架 的全注解环境模版 maven中央仓库批量删除lastupdated文件后依然是lastupdated解决方法 mirror aliyun中央仓库

    批量删除文件,得用批处理文件,文件名随便,路径改成你的Repository,代码如下 rem 这里写你的仓库路径 set REPOSITORY_PATH=d:\repo rem 正在搜索... for ...

  5. 核心动画(UIView封装动画)-转

    一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画支持. 执行动画所需要的工作由UIView类自动完成 ...

  6. mysql if...else 的使用

    select case when tca.id = '3' then 'vw' else epc_code end as epccode,tfp.product_id, tfp.vender, tfp ...

  7. SQLSERVER SQL性能优化

      1.选择最有效率的表名顺序(只在基于规则的优化器中有效)      SQLSERVER的解析器按照从右到左的顺序处理FROM子句中的表名,因此FROM子句中写在最后的表(基础表driving ta ...

  8. 【TOJ 1545】Hurdles of 110m(动态规划)

    描述 In the year 2008, the 29th Olympic Games will be held in Beijing. This will signify the prosperit ...

  9. ABAP术语-Function Builder

    Function Builder 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/03/1063196.html Tool for creat ...

  10. vuex重置所有state(可定制)

    在正式场景中我们经常遇到一个问题,就是登出页面或其他操作的时候,我们需要重置所有的vuex,让其变为初始状态,那么,就涉及到了多种方法:1.页面刷新: window.location.reload() ...