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. StyleCop学习笔记——初识StyleCop

    一.定义 StyleCop是微软的一个开源的静态代码分析工具,检查c#代码一致性和编码风格. 二.支持的环境. JetBrains R# 5.1.3 ( 5.1.3000.12) JetBrains ...

  2. 反射 reflect 初始学习

    类也是对象,本身是java.lang.Class 类的对象. Class的三种表示方式: 1.Class a = A.class: 类名.class的方式.经常在日志记录里面,获取当前类的日志记录生成 ...

  3. Oracle ClusterwarePRCT-1011 : Failed to run "oifcfg".&nb

    OS: Oracle Linux Server release 6.3 DB: Oracle 11.2.0.3 在oracle-linux6.3安装11g RAC,在安装软件时候提示: An inte ...

  4. hdu 1872 稳定排序

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1872 稳定排序 Description 大家都知道,快速排序是不稳定的排序方法.如果对于数组中出现的任 ...

  5. Go append方法

    append用来将元素添加到切片末尾并返回结果.看代码: package main import "fmt" func main() { x := [],,} y := [],,} ...

  6. bootstrap bootstrapTable 分页 传值问题

    bootstrapTable 分页传值 配置项:将原始的 limit: params.limit, //页面大小  page: params.offset, //页码 改成 limit: params ...

  7. 位bit——字节Byte???

    1.换算 每8个位(bit)组成一个字节(byte) 位bit简写为小写字母“b”,字节Byte简写为大写字母“B” 8*b=1*B 1024*B=1*KB 1024*K=1MB 2.举例 一个英文字 ...

  8. verilog运算符及表达式

    1.运输符 算术运算符(+,-,X,/,%) 赋值运算符(=,<=) 关系运算符(>,<,>=,<=) 逻辑运算符(&&,||,!)//与或非 条件运算符 ...

  9. button swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  10. [rsync+inotify]——监控客户端文件变化,rsync同步到服务器

    关于rsync的配置请参考博文:http://www.cnblogs.com/snsdzjlz320/p/5630695.html 实验环境 (1) Rsync服务器:10.0.10.158 (2) ...