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. [Jobdu] 题目1506:求1+2+3+...+n

    题目描述: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C).  输入: 输入可能包含多个测试样例. 对于每 ...

  2. VirtualBox 中ubuntu访问window下共享目录

    1,vbox中设置window需要共享给vbox中ubuntu的文件夹,主要要注意共享文件夹名称windowShare. 2,在ubuntu中挂载windowShare. sudo mount -t ...

  3. Android Log工具类

    import java.text.SimpleDateFormat; import java.util.Date; import android.util.Log; public class LogU ...

  4. 一个关于css3背景透明的例子

    大家都知道使用opacity调节透明度不仅是背景透明了而且选择区域的文字也跟着透明了, 这是我们不想要的效果,于是强大的css3便有了只让背景透明的功能 那就是background:rgba(0,0, ...

  5. SPOJ 7258 Lexicographical Substring Search(后缀自动机)

    [题目链接] http://www.spoj.com/problems/SUBLEX/ [题目大意] 给出一个字符串,求其字典序排名第k的子串 [题解] 求出sam上每个节点被经过的次数,然后采用权值 ...

  6. 11136-Hoax or what

    Each Mal-Wart supermarket has prepared a promotion scheme run by the following rules: A client who w ...

  7. 怎样注册uber司机 如何注册uber司机 最新详细攻略

    怎样注册uber司机 如何注册加入uber司机 全国加入Uber 的要求 车辆要求:要求裸车价8万以上,车龄5年以内,第三者责任险保额30万以上,不支持20万以下的面包车/商务车,不支持4座以下车辆. ...

  8. 2015 11 26 java 配置环境变量

    使用java软件, 须进行改变配置环境变量.需要2步. 第一,在电脑中找到配置环境变量的位置: 我的电脑,属性,高级,环境变量. 第二,进行添加两个变量,更改一个变量: 1,变量名:JAVA_HOME ...

  9. nefu 462 fib组合

    nefu 462 fib组合 (斐波那契数列的通项公式以及推倒过程) 分类: 数学2014-05-21 10:27 190人阅读 评论(0) 收藏 举报 题目链接:http://acm.nefu.ed ...

  10. SQL存储过程动态查询数据区间

    以前经常看到人查询数据库采用left join及case方式,一条一条的枚举查询整个数据的数据区间方法可行,但是数据一但很大,枚举就死悄悄,在网上查看,几乎全是照抄case ,left join枚举无 ...