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).

给出一个N(1≤N≤10^6),使用一些2的若干次幂的数相加来求之.问有多少种方法

Input

一个整数N.

Output

方法数.这个数可能很大,请输出其在十进制下的最后9位.

Sample Input

7

Sample Output

6

HINT

  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

考虑到n不大,所以我们可以直接用完全背包,复杂度应该是\(O(n\ln n)\)级别

其实发现奇数只能通过偶数+1得到,而偶数可以通过奇数+1,也可以通过其折半的偶数翻倍得来,因此得到方程

\[f[i]=\begin{cases} f[i-1]&,i\%2=1\\ f[i-1]+f[i/2]&,i\%2=0\end{cases}
\]

复杂度\(O(n)\)

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1;char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=1e6,p=1e9;
int f[N+10];
int main(){
int n=frd(); f[0]=1;
for (register int i=1;i<=n;i+=2) f[i]=f[i-1],f[i+1]=(f[i]+f[(i+1)>>1])%p;
printf("%d\n",f[n]);
return 0;
}

最新文章

  1. Files 的值“&lt;&lt;&lt;&lt;&lt;&lt;&lt; .mine”无效。路径中具有非法字符
  2. 如何用TDR来测试PCB板的线路阻抗
  3. Java基础知识笔记(六:网络程序设计)
  4. C# ListView得到选中项及子项
  5. 在使用dot。js中的值中有空格出现后,进行去除
  6. php环境搭建工具包推荐
  7. autohotkey-【GUI】Switch between Windows of the Same Application
  8. nginx多域名的配置方法
  9. Global &amp; Local Variable in Python
  10. Linux下PHP安装配置MongoDB数据库连接扩展
  11. MySQL学习笔记:MySQL: ERROR 1064(42000)
  12. jdk阅读xml文件
  13. mysql下优化表和修复表命令使用说明(REPAIR TABLE和OPTIMIZE TABLE)
  14. H5新增元素
  15. 文笔很差系列1 - 也谈谈AlphaGo
  16. Java方法的静态绑定与动态绑定讲解(向上转型的运行机制详解)
  17. Linux学习之路-2017/12/22
  18. 如何把OpenWrt安装到PC?
  19. 查看GCC的内置宏定义
  20. Lucene4.x创建索引与3.x的一些不同

热门文章

  1. MD5加密Java工具类
  2. 【scrapy】创建第一个项目
  3. cocos2d-x 2.0下怎样让BOX2D DEBUG DRAW的方法笔记
  4. 【求建议】毕业之声——信院IT类毕业学子经验分享交流会
  5. 十分简洁的手机浏览器 lydiabox
  6. java读取Excel表格中的数据
  7. 【iOS系列】-UITableView的使用
  8. Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExce
  9. HTML5中meta属性
  10. Lightoj 1012 - Guilty Prince