POJ2229 HDU2709 Sumsets【数列】

Sumsets

Time Limit: 2000MS   Memory Limit: 200000K
Total Submissions: 23695   Accepted: 9112

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

USACO 2005 January Silver

问题链接POJ2229 HDU2709 Sumsets

问题分析

  这个问题与OEIS中的一个数列相关联,数列的序号是A018819。该数列的名字就是Binary partition function: number of partitions of n into powers of 2。数列问题的关键是该数列的通项公式,该数列的通项公式是条件递推式。有关该数列的通项公式,参见程序。

  这个问题打表是必要的,用函数init()来实现。

程序说明:(无)

题记:(略)

参考链接:(无)

AC的C++语言程序如下:

/* POJ2229 HDU2709 Sumsets */

#include <iostream>
#include <stdio.h>

using namespace std;

const int N = 1e6;
const int MOD = 1e9;
int f[N + 1];

void init()
{
    f[0] = 1;
    f[1] = 1;
    for(int i = 2; i <= N; i++)
        if(i % 2)
            f[i] = f[i - 1];
        else
            f[i] = (f[i - 1] + f[i >> 1]) % MOD;
}

int main()
{
    init();

    int n;
    while(~scanf("%d", &n))
        printf("%d\n", f[n]);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/82154331