DP_Sumsets

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

 


 

 

题意:整数N用2^n之和的形式表示的方案数

思路:当N>1时,若N为奇数,则每个分解方案中至少含有一个1项。此时若每种分解方案中去掉一个1项,方案数不发生改变。

   即     N分解的方案数=(N-1)分解的方案数

   若该N为偶数,则分为两类情况:

   1、含有1项,同上,每个方案中去掉1项,方案数不变。

   2、不含有1项,此时每个方案中最小项应为2,若将每一项除2,方案数不变。

   即     N分解的方案数=(N-1)分解的方案数+(N/2)分解的方案数。

边界条件:当N=1时只有一种分解方案。

注意:可能溢出,需要取模

 

 


 

 

 1 #include<cstdio>
 2 int s[1000005];
 3 int main()
 4 {
 5     int n;
 6 
 7     s[1]=1;
 8     for( int i=2; i<=1000000; i++){
 9         if( i%2==0 )
10             s[i]=(s[i-1]+s[i/2])%1000000000;
11         else
12             s[i]=s[i-1];
13     }
14     while(~scanf("%d",&n)){
15         printf("%d\n",s[n]);
16     }
17 
18     return 0;
19 }
View Code

 

 

 

posted @ 2019-08-01 17:53  Konoba  阅读(189)  评论(0编辑  收藏  举报