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;
}

[Usaco2005 Jan]Sumsets 求和的更多相关文章

  1. BZOJ1677: [Usaco2005 Jan]Sumsets 求和

    1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 570  Solved: 310[Submi ...

  2. BZOJ 1677: [Usaco2005 Jan]Sumsets 求和( dp )

    完全背包.. --------------------------------------------------------------------------------------- #incl ...

  3. BZOJ 1677: [Usaco2005 Jan]Sumsets 求和

    题目 1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 617  Solved: 344[Su ...

  4. 1677: [Usaco2005 Jan]Sumsets 求和

    1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 626  Solved: 348[Submi ...

  5. 【BZOJ1677】[Usaco2005 Jan]Sumsets 求和 递推

    ... #include <iostream> using namespace std; ]; int n,i; int main() { cin>>n; f[]=; ;i&l ...

  6. 【BZOJ】1677: [Usaco2005 Jan]Sumsets 求和(dp/规律)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1677 完全背包很容易想到,将1,2,4...等作为物品容量即可. 然后这题还有一个递推式 f[i]= ...

  7. BZOJ 1677 [Usaco2005 Jan]Sumsets 求和:dp 无限背包 / 递推【2的幂次方之和】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1677 题意: 给定n(n <= 10^6),将n分解为2的幂次方之和,问你有多少种方 ...

  8. bzoj 1677: [Usaco2005 Jan]Sumsets 求和【dp】

    设f[i]为i的方案数,f[1]=1,考虑转移,如果是奇数,那么就是f[i]=f[i-1]因为这1一定要加:否则f[i]=f[i-1]+f[i>>1],就是上一位+1或者i/2位所有因子乘 ...

  9. BZOJ1679: [Usaco2005 Jan]Moo Volume 牛的呼声

    1679: [Usaco2005 Jan]Moo Volume 牛的呼声 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 723  Solved: 346[ ...

随机推荐

  1. Import Items – Validation Multiple Languages Description

    ð  提交标准请求创建和更新物料,因语言环境与处理次序方式等因素,造成物料中英(更多语言)描述和长描述混乱刷新. 症状: >>> Submit Standard Open Inter ...

  2. pip命令自动补全功能;设置代理;使用国内源

    这是pip自带的功能 执行的脚本 把脚本写入.zshrc或者profile等里面,执行source立即生效 设置代理: pip --proxy=http://username:password@pro ...

  3. sklearn特征工程总结

    转自: http://www.cnblogs.com/jasonfreak/p/5448385.html https://www.zhihu.com/question/28641663/answer/ ...

  4. 【转】 一张图看懂开源许可协议,开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别

    原文:http://blog.csdn.net/testcs_dn/article/details/38496107 ----------------------------------------- ...

  5. Firefox下td用display控制页面导致页面变形

    Firefox下table的td元素假设使用了display:'block'会使得table变形.原因是block会将对象强制作为块对象呈递,为对象之后加入新行,所以并不适合td,改成display: ...

  6. [Rust] Setup Rust for WebAssembly

    In order to setup a project we need to install the nightly build of Rust and add the WebAssembly tar ...

  7. DacningLinks实现

    本文简单分析DancingLinks实现中的数据结构设计,给出了精确覆盖问题及其扩展问题的代码.并应用于数独问题. 先简单描写叙述一下精确覆盖问题: 给定一个N*M的01矩阵,从中选中若干行,这些行向 ...

  8. 《The Swift Programming Language》的笔记-第27页

    页 1 type safelanguage 本页的主要内容是说swift语言是"类型检查"的安全型编程语言.意思是赋值语句的左值和右值的类型要一致,左值声明是string型变量那么 ...

  9. C++语言笔记系列之二十——模版

    1.随意输入两个数x和y,输出最大值max. int max(int x, int y) {return x>y? x:y;} 2.函数模版 (1)用一种或者多种通用类型去表示函数--函数模版. ...

  10. Android 通过ADB Wireless无线调试应用

    使用数据线调试应用难免不方便,本篇博客介绍使用ADB Wireless工具.当手机和电脑处在同一网络下.实现无线调试应用. ADB Wireless可以让手机用无线来代替USB连接.而使用ADB工具的 ...