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. UVa 116 Unidirectional TSP (DP)

    该题是<算法竞赛入门经典(第二版)>的一道例题,难度不算大.我先在没看题解的情况下自己做了一遍,虽然最终通过了,思路与书上的也一样.但比书上的代码复杂了很多,可见自己对问题的处理还是有所欠 ...

  2. python strip()函数介绍

    函数原型 声明:str为字符串,s为要删除的字符序列 str.strip(s)        删除str字符串中开头.结尾处,位于 s删除序列的字符 str.lstrip(s)       删除str ...

  3. 《火球——UML大战需求分析》(0.2)——目录

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

  4. 第七届河南省赛H.Rectangles(lis)

    10396: H.Rectangles Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 229  Solved: 33 [Submit][Status] ...

  5. Android 怎样在linux kernel 中读写文件

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  6. Android面试题06

    51. 一条最长的短信息约占多少byte? 中文70(包括标点),英文160,160个字节 这个说法不准确, 要跟手机制式运营商等信息有关. 做实验,看源码 ArrayList<String&g ...

  7. Android 标签控件

    版本号:1.0 日期:2014.7.24 版权:© 2014 kince 转载注明出处      在有的应用中可能须要设置一些标签来方便用去去查询某些信息,比方手机助手或者购物软件之类都会有一些标签. ...

  8. AT&T汇编试讲--获取CPU Vendor ID

    纯汇编代码如下: # a test program to get the processor vendor id # data segment .section .data output: .asci ...

  9. ceph启动脚本

    放在/etc/init.d/目录下,用法如下: root@u253:~# /etc/init.d/ceph === mon.a === usage: /etc/init.d/ceph [options ...

  10. MOSS程序中如何发Mail?

    我们使用.NET类库中的API发Mail的时候,我们要配置他的SMTP Server等,但是在Sharepoint里,已经提供了相关的封装的方法: SPUtility.SendEmail(SPWeb, ...