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
問題分析:
這個問題與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;
}