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

    https://github.com/jfeinstein10/JazzyViewPager https://github.com/chiemy/JellyViewPager JellyViewPag ...

  2. minify合并js和css文件

    压缩 JavaScript 和 CSS,是为减少文件大小,节省流量开销:合并 JavaScript 和 CSS,是为了减少请求数量,减轻服务器压力.而这些枯燥又没有技术含量的工作,我们以前通常会手动处 ...

  3. MySQL · 特性分析 · MDL 实现分析

    http://mysql.taobao.org/monthly/2015/11/04/ 前言 在MySQL中,DDL是不属于事务范畴的,如果事务和DDL并行执行,操作相关联的表的话,会出现各种意想不到 ...

  4. Panopticon跨平台的逆向工程反汇编工具

    http://www.freebuf.com/sectool/104045.html Panopticon 使用GPLv3授权许可,其免费. 项目文档:https://panopticon.re. 问 ...

  5. 《Linux内核设计与实现》读书笔记

    http://www.cnblogs.com/wang_yb/tag/linux-kernel/

  6. SQL Server 2012 Express LocalDB

    微软最新推出的 SQL Server 2012 Express LocalDB 是一种 SQL Server Express 的运行模式,特别适合用在开发环境使用,也内置在 Visual Studio ...

  7. 用count(*)还是count(列名) || Mysql中的count()与sum()区别

    Mysql中的count()与sum()区别   首先创建个表说明问题 CREATE TABLE `result` (   `name` varchar(20) default NULL,   `su ...

  8. MySQL大批量插入数据

    MySQL大批量插入数据 1. 对于Myisam类型的表,可以通过以下方式快速的导入大量的数据. ALTER  TABLE  tblname  DISABLE  KEYS; loading  the  ...

  9. 笔记——js 数组

    JS阅读笔记--数组[Array] 最近在看zepto源码,里面用到了很多基础知识,借此机会又把基础知识复习和整理了一遍,算是温故而知新吧.先从引用类型Array写起吧 1. length属性 代码: ...

  10. SQL in与exists相关性能问题总结

    SQL  in与exists相关性能问题总结 in 和 exists in 和 exists的是DBA或开发人员日常工作学习中常用的基本运算符,今天我就这两个所带来的性能问题进行分析总结,方便自己与他 ...