http://www.lydsy.com/JudgeOnline/problem.php?id=1677

完全背包很容易想到,将1,2,4...等作为物品容量即可。

然后这题还有一个递推式

f[i]==f[i-1], 当i%2==1

f[i]==f[i-1]+f[i/2], 当i%2==0

当i为奇数时,我们可以看为i-1加上一个1的情况,那么只有f[i-1]中情况(因为每种情况只是多了一个1)

当i为偶数时,分为2种情况,含有1和不含有1,当含有1时,那么情况就是f[i-1],当不含有1时,情况就是f[i/2],因为不含有1他们都可以被2整除

背包:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1000005, MD=1000000000;
int f[N], n; int main() {
read(n);
f[0]=1;
for1(i, 0, 30) {
int s=1<<i;
if(s>n) break;
for(int j=s; j<=n; ++j)
{ f[j]+=f[j-s]; f[j]%=MD; }
}
print(f[n]);
return 0;
}

递推:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1000005;
int f[N], n; int main() {
read(n);
f[1]=1;
for1(i, 2, n) {
f[i]=f[i-1];
if(!(i&1)) f[i]+=f[i>>1];
f[i]%=1000000000;
}
print(f[n]);
return 0;
}

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

有以下六种方式
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

HINT

Source

最新文章

  1. 篇一:MySQL中case when then
  2. Android工作学习第5天之Activity的完全退出程序
  3. 安卓下如何使用JUnit进行软件测试
  4. MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过 ButtonBase 触发命令
  5. charles 常用设置
  6. dataTables表格分页排序等交互
  7. iOS,OC,图片相似度比较,图片指纹
  8. USB系列之六:基于DOSUSB的简单U盘驱动程序
  9. BZOJ 3626: [LNOI2014]LCA( 树链剖分 + 离线 )
  10. Visual Studio2017 远程调试 Remote Debugger
  11. bzoj 4501 旅行
  12. walle2.0 nginx.conf配置文件参数
  13. MySQL5.7.25(解压版)Windows下详细的安装过程
  14. C#网络请求与JSON解析
  15. 使用Semaphore控制对资源的多个副本的并发访问
  16. 求一个数组中重复数字的个数,要求复杂度为O(n)
  17. es6 中的 symbol
  18. Windows中几个内存相当的指标
  19. Python:Windows8下安装BeautifulSoup
  20. sse float 转int 截断和不截断

热门文章

  1. codechef The Ball And Cups题解
  2. execve 系列函数
  3. jedis使用线程池封装redis基本操作
  4. .net core json序列化首字符小写和日期格式处理
  5. FFmpeg音视频同步示例
  6. Ajax异步打开新页面弹框被拦截,无法将参数值传递到后台
  7. scrapy添加 请求头
  8. 细说WebSocket -- Node.js篇
  9. unity, editorWindow lose data when enter play mode
  10. Genral log(普通日志)与 Slow log(慢速日式)