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

    https://github.com/navasmdc/PhoneTutorial PhoneTutorial-master.zip

  2. Android多媒体数据库之MediaStore研究

    应网友要求,今天给大家讲android的多媒体数据库.MediaStore这个类是android系统提供的一个多媒体数据库,android 中多媒体信息都可以从这里提取.这个MediaStore包括了 ...

  3. JAMA:Java矩阵包

    原文链接:JAMA:Java矩阵包 API文档链接:线性代数Java包 JAMA jama是一个非常好用的java的线性代数软件包.适用于日常编程可能碰到的各种矩阵运算问题,提供了一个优雅的简便的解决 ...

  4. 一个开源音乐播放器,低仿QQ音乐!

    有暇,弄了个音乐播放器,页面效果整体上参考了QQ音乐,相关API使用了易源数据提供的相关接口(https://www.showapi.com/api/lookPoint/213),在此表示感谢.先来看 ...

  5. SSIS 学习(6):包配置(上)【转】

    Integrartion Services 包实际上就是一个对象属性的集合,在前面我们开发的所有 Integration Services包,其中的变量.属性,比如:数据库链接.同步文件目录等,我们都 ...

  6. 配置hibernate出现的错误一

    问题:2011-04-18 11:35:46,734 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] - could not complete sche ...

  7. SQL Server 2012 performance dashboard 安装

    微软提供了一个很好用的工具performance dashboard: 下载地址: http://www.microsoft.com/en-us/download/details.aspx?id=29 ...

  8. oracle 自增列设置

    序列 create sequence sq_1 minvalue maxvalue start increment cache ; 触发器 create or replace trigger 触发器名 ...

  9. 关于Windows下如何查看端口占用和杀掉进程

    更详细博客参见: http://www.cnblogs.com/chenwenbiao/archive/2012/06/24/2559954.html 或可参考:http://www.cnblogs. ...

  10. [设计模式]<<设计模式之禅>>抽象工厂模式

    1 女娲的失误 上一篇讲了女娲造人的故事.人是造出来了,世界也热闹了,可是低头一看,都是清一色的类型,缺少关爱.仇恨.喜怒哀乐等情绪,人类的生命太平淡了,女娲一想,猛然一拍 脑袋,忘记给人类定义性别了 ...