POJ-2229

Sumsets
Time Limit: 2000MS   Memory Limit: 200000K
Total Submissions: 19599   Accepted: 7651

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,求n有多少种由2的幂次之和组成的方案. 

 

当n为奇数的时候,那么所求的和式中必有1,则dp[n]==dp[n-1];

当n为偶数的时候,可以分两种情况:

1.含有1,个数==dp[n-1];

2.不含有1,这时每个分解因子都是偶数,将所有分解因子都除以二,所得的结果刚好是n/2的分解结果,并且一一对应,则个数为dp[n/2];

 

 

AC代码:

 1 //#include<bits/stdc++.h>
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std;
 5 
 6 const long long MOD=1000000000;
 7 
 8 int dp[1000010];
 9 
10 int main(){
11     ios::sync_with_stdio(false);
12     int n;
13     while(cin>>n&&n){
14         memset(dp,0,sizeof(dp));
15         dp[1]=1;
16         for(int i=2;i<=n;i++){
17             if(i&1){
18                 dp[i]=dp[i-1];
19             }
20             else{
21                 dp[i]=(dp[i-1]+dp[i>>1])%MOD;
22             }
23         }
24         cout<<dp[n]<<endl;
25     }
26     return 0;
27 }

 

posted @ 2017-08-11 20:45  Kiven#5197  阅读(222)  评论(0编辑  收藏  举报