Sumsets
Time Limit: 2000MS   Memory Limit: 200000K
Total Submissions: 19024   Accepted: 7431

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

  1. 7

Sample Output

  1. 6

Source

USACO 2005 January Silver

——————————————————————————————————

题目的意思是给出一个数问把他变成若干个2^x的数累加,问有多少种不同情况

思路:方法一:dp,完全背包 +打表

方法二:分析可知对于第i项,i为奇数项就等于i-1项的值,i为偶数项就等于i-1项    加上i/2项的值(把i/2项每个数*2)

方法一:可能会超时,看判题机状态

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <vector>
  9. #include <set>
  10. #include <stack>
  11. #include <map>
  12. #include <climits>
  13.  
  14. using namespace std;
  15.  
  16. #define LL long long
  17. const int INF = 0x3f3f3f3f;
  18. const int mod=1000000000;
  19. int dp[1000005];
  20.  
  21. int main()
  22. {
  23. int n;
  24. memset(dp,0,sizeof dp);
  25. dp[0]=1;
  26. for(int i=0;i<=20;i++)
  27. {
  28. int k=pow(2,i);
  29. for(int j=k;j<1000005;j++)
  30. {
  31. dp[j]=(dp[j]+dp[j-k])%mod;
  32. }
  33. }
  34. while(~scanf("%d",&n)){
  35. printf("%d\n",dp[n]);
  36.  
  37. }
  38.  
  39. return 0;
  40. }

  

方法二:效率较高

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <vector>
  9. #include <set>
  10. #include <stack>
  11. #include <map>
  12. #include <climits>
  13.  
  14. using namespace std;
  15.  
  16. #define LL long long
  17. const int INF = 0x3f3f3f3f;
  18. const int mod=1000000000;
  19. int dp[1000005];
  20.  
  21. int main()
  22. {
  23. int n;
  24. memset(dp,0,sizeof dp);
  25. dp[1]=1;
  26. for(int j=2;j<1000005;j++)
  27. {
  28. if(j%2)
  29. dp[j]=dp[j-1]%mod;
  30. else
  31. dp[j]=(dp[j-1]+dp[j/2])%mod;
  32. }
  33. while(~scanf("%d",&n)){
  34. printf("%d\n",dp[n]);
  35.  
  36. }
  37.  
  38. return 0;
  39. }

  

POJ2229 Sumsets的更多相关文章

  1. POJ2229 Sumsets 【递归】

    Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 13210   Accepted: 5300 Descrip ...

  2. [USACO2005][poj2229]Sumsets(递推)

    http://poj.org/problem?id=2229 分析: 显然的递推 若n为奇数,那么肯定是在n-1的基础上前面每个数+1,即f[n]=f[n-1] 若n为偶数 当第一位数字是1的时候,等 ...

  3. POJ2229 - Sumsets(完全背包)

    题目大意 给定一个数N,问由不同的2的幂之和能组成N的方法有多少种 题解 看完题目立马想到完全背包...敲完代码上去超时了....后来发现是%的原因...改成减法就A了...%也太他妈耗时了吧!!!( ...

  4. poj2229 Sumsets (递推)

    http://poj.org/problem?id=2229 看到题目能感觉到多半是动态规划,但是没有清晰的思路. 打表找规律: #include<cstdio> #include< ...

  5. 《挑战程序设计竞赛》2.3 动态规划-基础 POJ3176 2229 2385 3616 3280

    POJ3176 Cow Bowling 题意 输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线. 规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个 ...

  6. 子集和问题(应用--换零钱)POJ2229:Sumsets

    我一直在纠结换零钱这一类型的题目,今天好好絮叨一下,可以说他是背包的应用,也可以说他是单纯的dp.暂且称他为dp吧. 先上一道模板题目. sdut2777: 小P的故事——神奇的换零钱 题目描述 已知 ...

  7. 【POJ - 2229】Sumsets(完全背包)

    Sumsets 直接翻译了 Descriptions Farmer John 让奶牛们找一些数加起来等于一个给出的数N.但是奶牛们只会用2的整数幂.下面是凑出7的方式 1) 1+1+1+1+1+1+1 ...

  8. POJ 2229 Sumsets

    Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 11892   Accepted: 4782 Descrip ...

  9. HDU 2709 Sumsets(递推)

    Sumsets http://acm.hdu.edu.cn/showproblem.php?pid=2709 Problem Description Farmer John commanded his ...

随机推荐

  1. linux操作系统-源码包安装jdk1.7

    1.下载安装文件 在oracle官方找不到bin二进制安装文件只能使用rpm包来安装 下载地址:http://www.oracle.com/technetwork/java/javase/downlo ...

  2. iOS.Crash.Case-[__NSArrayM objectForKeyedSubscript:]

    1. [__NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance - source code and s ...

  3. usr/include/c++/6.4.1/bits/stl_relops.:67: Parse error at "std"

    问题描述: 1.编译某qt工程的32位架构二进制包时,出现了上面错误,具体错误信息如下 qmake-qt5 -o ProductLicense/Makefile ProductLicense/Prod ...

  4. asp.net web 服务器端全局定时执行任务

    web网站里面,需要每隔1分钟,执行一个任务,并且一直保持这个定时执行状态,可以用如下一个方法:    1,Global.asax里面的 Application_Start ,发生在第一次请求网站的时 ...

  5. 英国BBC出的这套中国风海报,设计美哭了!

    “中国风”在国际上已经不是“小众”了 之前分享过好莱坞电影的中国风海报 没想到国外的电视剧也看上了中国市场 没错就是英国BBC的最长寿科幻剧—— <神秘博士Doctor Who> 前段时间 ...

  6. VMware Workstation 15 pro keys

    永久激活密钥UG5J2-0ME12-M89WY-NPWXX-WQH88 GA590-86Y05-4806Y-X4PEE-ZV8E0 YA18K-0WY8P-H85DY-L4NZG-X7RAD UA5D ...

  7. log4j日志整合输出(slf4j+commonslog+log4j+jdklogger)

    log4j日志整合输出(slf4j+commonslog+log4j+jdklogger) 博客分类: 日志   J2EE项目中,经常会用到很多第三方的开源组件和软件,这些组件都使用各自的日志组件,比 ...

  8. bootstrap 中 css 与 javascript 的使用

    1.css 1.1.选择器 1.2.子选择器: css 里的子元素用符号'>'表示.如下示例是表示拥有table样式的表盒,其thead元素内的tr元素如果有th的话,则应用该样式. .tabl ...

  9. 2016-2017-2 20155312 实验二《Java面向对象程序设计》实验报告

    知识总结 伪代码 产品代码 Java编程时,程序员对类实现的测试叫单元测试. 测试用例是为某个特殊目标而编制的一组测试输入.执行条件以及预期结果,以便测试某个程序路径或核实是否满足某个特定需求. 先写 ...

  10. How to turn on syntax highlighting in osx

    put follow code in ~/.vimrc set ai " auto indenting set history=100 " keep 100 lines of hi ...