Sumsets
Time Limit: 2000MS   Memory Limit: 200000K
Total Submissions: 15045   Accepted: 5997

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
题解:让用2^k的数相加组成n,刚看见就说是母函数,没打出来,打出来也肯定不对因为数据量太大肯定超时了;
其实可以用完全背包写;
思路很好想,把2^k的物品往背包里面放;dp[j]+=dp[j-i]为方案数;
递推也很好想,如果是奇数,直接dp[i]=dp[i-1]
如果是偶数,dp[i]可以由dp[i/2]得到;也可以由dp[i-1]得到;
完全背包:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
typedef long long LL;
const int MAXN=1000010;
const int MOD=1e9;
int bag[MAXN];
int main(){
int N;
while(~scanf("%d",&N)){
mem(bag,0);
bag[0]=1;
for(int i=1;i<=N;i*=2){
for(int j=i;j<=N;j++){
bag[j]+=bag[j-i];
bag[j]%=MOD;
}
}
printf("%d\n",bag[N]);
}
return 0;
}

  母函数超时:

#include<stdio.h>
const int MAXN= 1000010;
int main(){
int a[MAXN],b[MAXN],N;
while(~scanf("%d",&N)){
int i,j,k;
for(i=0;i<=N;i++){
a[i]=1;b[i]=0;
}
for(i=2;i<=N;i*=2){
for(j=0;j<=N;j++)
for(k=0;k+j<=N;k+=i)
b[j+k]+=a[j];
for(j=0;j<=N;j++)
a[j]=b[j],b[j]=0;
}
printf("%d\n",a[N]);
}
return 0;
}

  递推;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
typedef long long LL;
const int MAXN=1000010;
const int MOD=1e9;
int dp[MAXN];
int main(){
int N;
dp[0]=1;
for(int i=1;i<MAXN;i++){
if(i&1)dp[i]=dp[i-1];
else dp[i]=(dp[i-1]+dp[i/2])%MOD;
}
while(~scanf("%d",&N))printf("%d\n",dp[N]);
return 0;
}

  

Sumsets(完全背包)的更多相关文章

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

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

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

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

  3. POJ2229 - Sumsets(完全背包)

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

  4. POJ 2229 Sumsets(技巧题, 背包变形)

    discuss 看到有人讲完全背包可以过, 假如我自己做的话, 也只能想到完全背包了 思路: 1. 当 n 为奇数时, f[n] = f[n-1], 因为只需在所有的序列前添加一个 1 即可, 所有的 ...

  5. BZOJ 1677 [Usaco2005 Jan]Sumsets 求和:dp 无限背包 / 递推【2的幂次方之和】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1677 题意: 给定n(n <= 10^6),将n分解为2的幂次方之和,问你有多少种方 ...

  6. BZOJ1677: [Usaco2005 Jan]Sumsets 求和

    1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 570  Solved: 310[Submi ...

  7. BZOJ 1677: [Usaco2005 Jan]Sumsets 求和( dp )

    完全背包.. --------------------------------------------------------------------------------------- #incl ...

  8. POJ2229 Sumsets

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

  9. 【POJ】2229 Sumsets(递推)

    Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 20315   Accepted: 7930 Descrip ...

随机推荐

  1. Csharp多态的实现(接口)

    1.什么是接口 接口可以看做是一个标准, 所有继承的子类需要按照接口中声明的方法来 接口用关键字 interface 修饰,接口的名字一般是I.........able ,表示我有什么能力 接口一般是 ...

  2. Spring MVC返回的json如何去除根节点名称

    spring xml中配置视图如果是如下 <property name="defaultViews"> <list> <bean class=&quo ...

  3. 在View中使用CGridCtrl时出现系统异常

    一.简介 我的程序是单文档程序,我的View视图需要使用CGridCtrl,于是我把CGridCtrl作为子窗口嵌入到View中覆盖住整个View.由于不能像gridctrl_demo227那样直接在 ...

  4. 一道面试题细说C++类型转换

    开篇先说这道面试题: class ClassA { public: virtual ~ ClassA() { } virtual void FunctionA() { } }; class Class ...

  5. nginx install lua module

    #install luajit #http://luajit.org/download.html .tar.gz cd LuaJIT- make install PREFIX=/home/allen. ...

  6. HDU 4498 Function Curve (分段, simpson)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 最近太逗了...感觉成都要打铁了...只能给队友端 ...

  7. 框架开发(三)---smarty整合

    一 smarty 是什么 Smarty是一个PHP的模板引擎.更明确来说,它可以帮助开发者更好地 分离程序逻辑和页面显示.最好的例子,是当程序员和模板设计师是不同的两个角色的情况,而且 大部分时候都不 ...

  8. 手机端 UI一些插件

    手机弹出框 http://yun.baidu.com/share/link?shareid=3523128425&uk=2685891615

  9. 转:CI伪静态化

    去掉php框架CI默认url中的index.php 2010-03-17 17:33:07|  分类: php框架ci |字号 订阅   CI默认的rewrite url中是类似这样的,例如你的CI根 ...

  10. 《火球——UML大战需求分析》(0.1)——开篇废话

    说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...