Sumsets(POJ 2229 DP)

Sumsets
Time Limit: 2000MS   Memory Limit: 200000K
Total Submissions: 15293   Accepted: 6073

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

分两种情况讨论:
当n为奇数时,划分中肯定存在1,那么dp[i]=dp[i-1];
当n为偶数时,可以把划分情况分为含有1和没有1两种情况
  含有1,那么i-1为奇数,dp[i-1];
  不含有1,那么划分中都是偶数,相当于dp[i>>1]的每种划分的两倍;

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 int  M=1000000000;
 6 unsigned long long dp[1000005];
 7 int main()
 8 {
 9     unsigned long long  n,i,j;
10     scanf("%lld",&n);
11     dp[1]=1;
12     for(i=2;i<=n;i++)
13         dp[i] = (i%2 ?dp[i-1]:(dp[i-1]+dp[i>>1]))%M;
14     printf("%lld\n",dp[n]);
15     return 0;
16 }

 

你可能感兴趣的:(Sumsets(POJ 2229 DP))