首页 > 其他 > 详细

Sumsets(POJ 2229 DP)

时间:2016-01-31 13:26:23      阅读:217      评论:0      收藏:0      [点我收藏+]
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)

原文:http://www.cnblogs.com/a1225234/p/5173033.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!