Sumsets
Time Limit: 2000MS   Memory Limit: 200000K
Total Submissions: 16876   Accepted: 6678

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

7

Sample Output

6
初学动态规划,我用了一种非常愚蠢的解法耗内存又超时了...
AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
int dp[+];
/*const int N_MAX = 1000000;
int dp[21][N_MAX + 1];
int main() {
int N;
while (cin >> N) {
int k = 0;
while ((1 << k) <= N) {//求使得2^k大于N的最小k
k++;
} for (int i = 0;i < k;i++)
dp[i][0] = 0;
for (int i = 1;i <= N;i++)
dp[0][i] = 1;
for (int i = 1;i < k;i++) {
for (int j = 1;j <= N;j++) {
dp[i][j] = dp[i - 1][j];
for (int k1 = 1;(j - k1*(1 << i))>=0;k1++) {
dp[i][j] += dp[i - 1][j - k1*(1 << i)];
}
}
}
cout << dp[k-1][N] << endl;
}
return 0;
}*/
//若i为奇数,(i-1)为偶数,i的组合数就是(i-1)的组合数,因为(i-1)只能加1得到i。若i为偶数,(i-1)为奇数,则通过(i-1)+1的方式得到i的组合必定带有1,接下来考虑
//全是偶数的组合数,考虑到全是偶数的组合数和(i/2)的组合数一样,因为只要(i/2)的组合数里每一个数*2就可以得到i
int main() {
int N;
while (cin >> N) {
dp[] = ;
for (int i = ;i <= N;i++) {
if ((i & )==) {//若为偶数
dp[i] = dp[i / ];
}
dp[i] += dp[i - ];
dp[i] %= ;
}
cout << dp[N] << endl;
}
return ;
}

poj 2220 Sumsets的更多相关文章

  1. POJ 2229 Sumsets

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

  2. poj -2229 Sumsets (dp)

    http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...

  3. POJ 2549 Sumsets

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10593   Accepted: 2890 Descript ...

  4. poj 2459 Sumsets

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11612   Accepted: 3189 Descript ...

  5. poj 2229 Sumsets(dp)

    Sumsets Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 400000/200000K (Java/Other) Total Sub ...

  6. poj 2229 Sumsets 完全背包求方案总数

    Sumsets Description Farmer John commanded his cows to search for different sets of numbers that sum ...

  7. POJ 2549 Sumsets(折半枚举+二分)

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11946   Accepted: 3299 Descript ...

  8. POJ 2549 Sumsets hash值及下标

    题目大意:找到几何中的4个数字使他们能够组成 a+b+c=d , 得到最大的d值 我们很容易想到a+b = d-c 那么将所有a+b的值存入hash表中,然后查找能否在表中找到这样的d-c的值即可 因 ...

  9. poj 2229 Sumsets DP

    题意:给定一个整数N (1<= N <= 1000000),求出以 N为和 的式子有多少个,式子中的加数只能有2的幂次方组成 如5 : 1+1+1+1+1.1+1+1+2.1+2+2.1+ ...

随机推荐

  1. java web servlet

    一.什么是Servlet Servlet是一种小型的Java程序,它扩展了Web服务器的功能.作为一种服务器端的应用,他是运行在Servlet容器当中,例如Tomcat就是一种流行的Servlet容器 ...

  2. xampp

    Fatal error: Class 'kernel' not found in C:\xampp\htdocs\shopex\install\install.core.php on line 10 ...

  3. 关于AS3的垃圾回收

    FlashPlayer运行GC(Gabage Collection)的时间并不固定,它会根据你的内存的占用情况来决定运行GC的时间.它会根据用户机器的内存值来设定一个阀值,然后将程序的占用内存量保存在 ...

  4. MySQL内存----使用说明全局缓存+线程缓存) 转

    MySQL内存使用说明(全局缓存+线程缓存) 首先我们来看一个公式,MySQL中内存分为全局内存和线程内存两大部分(其实并不全部,只是影响比较大的 部分): per_thread_buffers=(r ...

  5. C# TextBox 换行 滚动到最后一行

    .要让一个Windows Form的TextBox显示多行文本就得把它的Multiline属性设置为true. 这个大家都知道,可是当你要在代码中为Text属性设置多行文本的时候可能会遇到点麻烦:) ...

  6. 图形化管理debian服务

    bootupmanager这个软件 ,用着勉强吧, 功能不多. 安装 sudo apt-get install bum 卸载sudo apt-get remove --purge bum 多了不说 , ...

  7. 一种快速求fibonacci第n个数的算法

    利用动态规则的思路,摒弃传统的递归做法,可以得到一种快速的求fibonacci第n个数的算法: ''' 求第n(从1开始)位fibonacci数 fibonacci数列前两位为0, 1. 后面每一位数 ...

  8. cocos2d-x中使用sqlite

    在单机游戏中有几十个场景道具,每一个都有各自的状态(获得.未获得.获得个数)等等,如果在游戏中平凡涉及到这些道具的实时存储,那么使用文本就会稍慢.可以使用sqlite数据库来完成. 下载地址:http ...

  9. Visual studio 2013 添加 GitHub

  10. linux- svn服务器

    环境:centos6.5 安装 centos 6.5默认安装了svn server, 这里直接使用.如果没有的话,使用yum -y install subversion安装 创建版本库 3.1 创建s ...