hdoj 2709 Sumsets 【母函数 递归】

Sumsets

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1791    Accepted Submission(s): 701


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
 

因为题目数据较大,所以不可能是单纯的母函数题目。

先写出母函数程序,记录数据,找出规律即可。

 

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 1000000+1
#define mod 1000000000
int c1[MAX],c2[MAX];
int dp[MAX];
int a[24];
int main()
{
	int n,m,k,i,j;
	dp[1]=1;dp[2]=dp[3]=2;dp[4]=dp[5]=4;
	for(i=6;i<MAX;i+=2)
	{
		dp[i+1]=dp[i]=(dp[i-1]%mod+dp[i/2]%mod)%mod;
	}
	while(scanf("%d",&n)!=EOF)
	{
         printf("%d\n",dp[n]);
	}
	return 0;
}



 

你可能感兴趣的:(hdoj 2709 Sumsets 【母函数 递归】)