POJ2229 Sumsets 【递归】

Sumsets

Time Limit: 2000MS   Memory Limit: 200000K
Total Submissions: 13210   Accepted: 5300

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

Source

field=source&key=USACO+2005+January+Silver" style="text-decoration:none">USACO 2005 January Silver

做这题时有些被曾经的经验束缚了,看完题第一反应是母函数,然后上模板。然后输入1000000等结果,等啊等就是不出结果。。

。參考了大牛的解题报告:Click

#include <stdio.h>
#include <string.h>

#define maxn 1000002
int dp[maxn];

int main() {
    int i, n;
    scanf("%d", &n);
    dp[1] = 1;
    for(i = 2; i <= n; ++i) {
        if(i & 1) dp[i] = dp[i-1];
        else dp[i] = dp[i-1] + dp[i/2];
        dp[i] %= 1000000000;
    }
    printf("%d\n", dp[n]);
    return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

时间: 2024-10-10 09:02:19

POJ2229 Sumsets 【递归】的相关文章

POJ2229 Sumsets 基礎DP

Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 14344   Accepted: 5709 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

POJ2229 Sumsets 【递推】

Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 13210   Accepted: 5300 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

poj2229 Sumsets

思路: 简单dp. 实现: 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 int dp[1000005]; 5 int main() 6 { 7 long long n; 8 scanf("%lld",&n); 9 dp[0]=0; 10 dp[1]=1; 11 dp[2]=2; 12 for(int i=3;i<=n;i++) 13 if(i%2==0) 14 d

PHP_递归实现无限级分类

<?php /** * 递归方法实现无限级别分类 * @param array $list 要生成树形列表的数组[该数组中必须要有主键id 和 父级pid] * @param int $pid=0 父级id * @param int $level=0 缩进次数[用于指定分类名称要缩进的数量] */ function getTree($list,$pid=0,$level=0 ) { // static 表示声明一个静态变量, 静态变量在函数中会一直保存它的值 static $tree = arr

3.sql中的向上递归和向下递归

1.向下递归 select * from table_name where 条件 connect by prior bmbm(本级关联条件)=sjbmbm(上级关联条件) start with bmbm(本级关联条件)='610000000000'(本级编码)--包含本级 select * from table_name where 条件 connect by prior bmbm(本级关联条件)=sjbmbm(上级关联条件) start with sjbmbm(本级关联条件)='6100000

斐波那契数列的递归和非递归解法

//递归解法 function fib(n){ if(n < 1){ throw new Error('invalid arguments'); } if(n == 1 || n == 2){ return 1; } return fib(n - 1) + fib(n - 2); } //非递归解法 function fib(n){ if(n < 1){ throw new Error('invalid arguments'); } if(n == 1 || n == 2){ return 1

python3 递归

递归调用: 在调用一个函数的过程中,直接或者简介调用了该函数本身 必须有一个明确的结束条件 l = [1,[2,3,[4,5,[6,7,[8,9,[10,11,[12,13]]]]]]] def func(l): for i in l: if isinstance(i,list): func(i) else: print(i) func(l) 应用场景:不知道应该循环多少次,只知道什么时候应该结束

day05匿名函数,内置函数,二分法,递归,模块

yield作为表达式来使用的方式 #grep -rl 'python /root """ 查找root下文件中含有python的文件 """ import os def init(func): def wrapper(*args,**kwargs): g=func(*args,**kwargs) next(g) return g return wrapper @init def search(target): while True: search

浅谈递归调用的个人领悟

从大一开始学c,就不是挺理解递归的,最近突然有所体会: 递归调用中递归调用的函数可以把它想象成为一个树的结点,在函数中调用自身就是一个分支,直到出口条件时就是这棵树的叶子结点.叶子的值便是出口返回的值.最后从叶子结点按照你所调用的方法向上返回值,最终结束递归调用.