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. Android应用程序框架之无边界设计意图

    Android的应用框架的外特性空间的描述在SDK文档有十分清楚的描述,Android应用的基本概念,组件生命周期等等有详细的描述.在外特性空间中,Android提供了Activity,Service ...

  2. [MODX] 0. Mangement System Overview

    In Modex, there are three tabs: Resoources, Elements & Files First: 'Files' is the place where t ...

  3. Windows 10正式版官方原版ISO镜像下载

    [微软官方]下载地址1:官方下载工具(32-位系统版本)官方下载工具(64-位系统版本) [MSDN]下载地址2:cn_windows_10_multiple_editions_x64_dvd_684 ...

  4. JDK1.6官方下载_JDK6官方下载_JDK1.6API(chm)下载_JDK6APICHM中文参

    JDK1.6官方下载_JDK6官方下载地址:http://www.java.net/download/jdk6/6u10/promoted/b32/binaries/jdk-6u10-rc2-bin- ...

  5. Linux下的lds链接脚本简介

    转载:http://hubingforever.blog.163.com/blog/static/171040579201192472552886/   一. 概论 每一个链接过程都由链接脚本(lin ...

  6. JAVA基础之StringBuilder基础方法

    StringBuilder sb = new StringBulder("kikikiki"); sb.apend("123")    //追加 输出kikik ...

  7. Collections.synchronizedMap 详解

    众所周知,HashMap 本身非线程安全的,但是当使用 Collections.synchronizedMap(new HashMap()) 进行包装后就返回一个线程安全的Map. 怎么实现的呢?今天 ...

  8. Java动态加载jar及class文件

    经常碰到需要动态加载jar及class文件的场景.Java类由于需要加载和编译字节码,动态加载class文件较为麻烦,但JDK仍提供了一整套方法来动态加载jar文件和class文件. 一.动态加载ja ...

  9. SDKInitializer.initialize报错求助

    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);     ...

  10. 简单遗传算法求解n皇后问题

    版权声明:本文为博主原创文章,转载请注明出处. 先解释下什么是8皇后问题:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法.在不 ...