Sumsets

Time Limit: 6000/2000 MS (Java/Others)   

 Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2159    

Accepted Submission(s): 875

Problem 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次方(k为大于等于0的正数).输出分的方法种数.(由于当输出整数过大时,种数很大只输出最后9位)

思路一:

a[n]为和为 n 的种类数;
根据题目可知,加数为2的N次方,即 n 为奇数时等于它前一个数 n-1 的种类数 a[n-1] ,若 n 为偶数时分加数中有无 1 讨论,即关键是对 n 为偶数时进行讨论:
1.n为奇数,a[n]=a[n-1]
2.n为偶数:
(1)如果加数里含1,则一定至少有两个1,即对n-2的每一个加数式后面 +1+1,总类数为a[n-2]
(2)如果加数里没有1,即对n/2的每一个加数式乘以2,总类数为a[n/2]
所以总的种类数为:a[n]=a[n-2]+a[n/2];

 #include <iostream>
using namespace std;
long i,a[];
int main()
{
a[] = ;
a[] = ;
for(i = ; i < ; i++)
{
if((i&) == )
{
a[i] = a[i-]; //i为奇数与它前一个数量相同
}
else
{
a[i] = (a[i-] + a[i>>]) % ; //含有1: a[i-1]每种情况填11、不含1: a[i/2]每种情况*2
}
}
while(cin >> i){
cout << a[i] << endl;
}
return ; }

思路二:DP思想

假如只能用1构成那么每个数的分的方法种数就是1.

如果这个时候能用 2 构成,那么对于大于等于 2 的数 n 就可以由 n - 2 2 构成 就转化为 求 n - 2 的种数那么就是 d [ n ] = d [ n-2 ] + d [ n ] (前面 d [ n-2 ] 表示数n可以由2构成的种数,后面加的 d [ n ] 表示数n只能由 1 构成的种数.)

那么状态转移方程式子就出来了(c [ n ] = 2^n)

d [ n ] [ k ] = d [ n ] [ k - 1 ] + d [ n - c [ k ] ] [ k ] ;

循环降维:

d [ n ] = d [ n ] + d [ n - c [ k ] ] ;

 #include<iostream>
#include<cstring>
using namespace std;
long d[],c[],n,i,j;
int main()
{
while(cin >> n)
{
memset(d,,sizeof(d));
c[]=d[]=;
for(i=;i<=;i++)
c[i]=c[i-]<<;
for(i=;i<=&&c[i]<=n;i++)
for(j=c[i];j<=n;j++)
d[j]=(d[j]+d[j-c[i]])%;
cout << d[n] << endl;
}
return ;
}

hdu 2709 Sumsets的更多相关文章

  1. HDU 2709 Sumsets(递推)

    Sumsets http://acm.hdu.edu.cn/showproblem.php?pid=2709 Problem Description Farmer John commanded his ...

  2. HDU - 2709 Sumsets 【递推】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2709 题意 给出一个数N 要求有多少种方式 求和 能够等于N 加的数 必须是 2的幂次 思路 首先可以 ...

  3. 题解报告:hdu 2709 Sumsets

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2709 Problem Description Farmer John commanded his co ...

  4. HDU 2709 Sumsets 经典简单线性dp

    Sumsets Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  5. hdu 2709 递推

    题意:给出一个数,把他拆成2^n和的形式,问有多少种拆法 链接:点我 对6进行分析 1 1 1 1 1 1 1 1 1 1 2 1 1 2 2 1 1 4 2 2 4 2 4 对最上面4个,显然是由4 ...

  6. hdu Sumsets

    Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T ...

  7. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  8. HDU——PKU题目分类

    HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...

  9. [转] HDU 题目分类

    转载来自:http://www.cppblog.com/acronix/archive/2010/09/24/127536.aspx 分类一: 基础题:1000.1001.1004.1005.1008 ...

随机推荐

  1. (转)UML序列图总结

    序列图主要用于展示对象之间交互的顺序. 序列图将交互关系表示为一个二维图.纵向是时间轴,时间沿竖线向下延伸.横向轴代表了在协作中各独立对象的类元角色.类元角色用生命线表示.当对象存在时,角色用一条虚线 ...

  2. Spring入门(6)-使用注解装配

    Spring入门(6)-使用注解装配 本文介绍如何使用注解装配. 0. 目录 使用Autowired 可选的自动装配 使用Qualifier选择 1. 使用Autowired package com. ...

  3. codeforce 630N Forecast

    N. Forecast time limit per test 0.5 seconds memory limit per test 64 megabytes input standard input ...

  4. AutoCAD.NET二次开发:创建自定义菜单(AcCui)

    从CAD2007之后,Autodesk提供了一个新的程序集AcCui.dll,使用这个程序集,我们可以方便地做一些界面方面的操作,比如创建自定义菜单. 下面介绍一下菜单的创建过程: 1.在项目中添加引 ...

  5. 不能发现 class "com.uustudio.unote.android.BaseApplication"

    12-13 15:45:46.289: E/AndroidRuntime(3474): java.lang.RuntimeException: Unable to instantiate applic ...

  6. radio select的 option使用

    1  radio的使用 <td id="sex">性别:              <input type="radio" name=&quo ...

  7. llnq SqlMethods like

    http://www.cnblogs.com/freeliver54/archive/2009/09/05/1560815.html http://www.cnblogs.com/chen1388/a ...

  8. 简单的玩玩etimer <contiki学习笔记之九 补充>

    这幅图片是对前面  <<contiki学习笔记之九>>  的一个补充说明. 简单的玩玩etimer <contiki学习笔记之九> 或许,自己正在掀开contiki ...

  9. Codeforces Gym 100425H H - Football Bets 构造

    H - Football BetsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  10. LVS 之 DR 2

    http://www.cnblogs.com/kgdxpr/archive/2013/09/09/3309776.html http://lyp0909.blog.51cto.com/508999/5 ...