Sumsets

http://acm.hdu.edu.cn/showproblem.php?pid=2709

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
 
解题代码:
 
/*
设way[n]为和为 n 的种类数; 根据题目可知,加数为2的N次方,即 n 为奇数时等于它前一个数 n-1 的种类数 way[n-1] ,若 n 为偶数时分加数中有无 1 讨论,即关键是对 n 为偶数时进行讨论: 1.如果所求的n为奇数,那么所求的分解结果中必含有1,因此,直接将n-1的分拆结果中添加一个1即可 为way[n-1] 2.如果所求的n为偶数,那么n的分解结果分两种情况 (1).如果加数里含1,则一定至少有两个1,即对n-2的每一个加数式后面 +1+1,总类数为way[n-2];
(2).不含有1 那么,分解因子的都是偶数,将每个分解的因子都除以2,刚好是n/2的分解结果,并且可以与之一一对应,这种情况有 s[n/2] 所以,状态转移方程为: 如果i为奇数 way[n] = way[n-1]
如果i为偶数 way[n] = way[n-2]+way[n/2];
*/
#include <stdio.h>
#include <math.h>
#include <string.h> #ifdef WINDOWS
#define LL __int64
#define LLd "%I64d"
#else
#define LL long long
#define LLD "%lld"
#endif const int max_n = 1e6+;
const LL MOD = 1e9;
LL way[max_n]; void deal()
{
way[] = way[] = ;
for (int i = ; i < max_n; i ++)
{
if (!(i&))
{
way[i] = way[i-] + way[i/];
way[i] %= MOD;
}
else
{
way[i] = way[i-];
way[i] %= MOD;
}
}
}
int main ()
{
int n;
deal();
while (~scanf ("%d", &n))
{
printf (LLD"\n", way[n]);
}
return ;
}

HDU 2709 Sumsets(递推)的更多相关文章

  1. hdu2709 Sumsets 递推

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2709 感觉很经典的一道递推题 自己想了有半天的时间了....比较弱.... 思路: 设f[n]表示和为 ...

  2. HDU 4747 Mex 递推/线段树

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=4747 Mex Time Limit: 15000/5000 MS (Java/Others)Memory Limi ...

  3. 致初学者(四):HDU 2044~2050 递推专项习题解

    所谓递推,是指从已知的初始条件出发,依据某种递推关系,逐次推出所要求的各中间结果及最后结果.其中初始条件或是问题本身已经给定,或是通过对问题的分析与化简后确定.关于递推的知识可以参阅本博客中随笔“递推 ...

  4. poj2229 Sumsets (递推)

    http://poj.org/problem?id=2229 看到题目能感觉到多半是动态规划,但是没有清晰的思路. 打表找规律: #include<cstdio> #include< ...

  5. HDU 2604 Queuing(递推+矩阵)

    Queuing [题目链接]Queuing [题目类型]递推+矩阵 &题解: 这题想是早就想出来了,就坑在初始化那块,只把要用的初始化了没有把其他的赋值为0,调了3,4个小时 = = 本题是可 ...

  6. HDU - 2604 Queuing(递推式+矩阵快速幂)

    Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. hdu 1723 DP/递推

    题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacc ...

  8. hdu 1249 三角形 (递推)

    三角形 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  9. [hdu 2604] Queuing 递推 矩阵快速幂

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  10. HDU 5366 dp 递推

    The mook jong Accepts: 506 Submissions: 1281 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...

随机推荐

  1. EMVTag系列2《磁条等效数据》

    Ø 57  磁条2等效数据 L: var. up to 19 -M(必备):此数据必须存在并提供给终端,终端在读应用数据过程中,如果没有读到必备数据,终端中止交易 按GB/T 17552,磁条2的数据 ...

  2. bat隐藏文件夹

    在windows中隐藏文件是可以隐藏,但是下面红色区域一旦选中,那也就显示出来了 现在有如下代码,可以实现个性化隐藏 ========================================= ...

  3. Zygote(app_process)相关分析1

    首先我们从Init.c中来看,当Init中解析完init.rc文件时会得到一系列的action,通过action去调用一些函数. Zygote是在init.rc中service section中 se ...

  4. Python: 迭代器与生成器小结

    迭代器与生成器的区别: 1. 迭代器由Class对象创建. 生成器由包含yield表达的Function对象或者Generator Expression创建. 2. 迭代器的原理: (1)由Itera ...

  5. 60.ISE PhysDesignRules ERROR

    PhysDesignRules:2100 - Issue with pin connections and/or configuration on block:<U_ila_pro_0/U0/I ...

  6. eclipse插件开发中全局对象的获取

    转自:http://blog.csdn.net/liaomin416100569/article/details/7165425 1.获取系统默认的ShellShell shell = Platfor ...

  7. 使用C语言在Win控制台中实现指定位置输出

    在古老的Turbo C中有个GotoXY可以让你在指定坐标中输出文字,可恨的是我看过的C语言书籍,有一半都是关于它的.我现在用着Windows系统,不可能还让我去写着DOS程序啊,起码也得从Win控制 ...

  8. COS中访问文件的三种方式

    1.通过FID来访问文件(比如EF,DF) 2.通过SFI来访问文件(有些COS命令可以通过SFI来快速访问文件,而不需要事先选中文件) 3.通过文件名来访问文件(只能是DF文件)

  9. c++ _beginthread

    c++多线程编程 #include <windows.h> #include <process.h> /* _beginthread, _endthread */ #inclu ...

  10. 面向对象版js分页

    基于前一个js分页,我将代码改成一个面向对象版的js分页,代码如下 http://peng666.github.io/blogs/pageobj <!DOCTYPE html> <h ...