hdu 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).

InputA single line with a single integer, N.OutputThe 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
题目大意就是将一个整数拆分为只有 1 或者 有 1 和偶数(2的次方数)组分的几个数的和

当于在f[n-1]的拆分结果上增加一个 1 即可;

               当整数n为偶数时,分为两种情况,含有1 ,和不含有1;

                           含有1时,就是f[n-1],不含1时,就是f[n/2](拆分结果都除以2)的结果              

                         综上结果,得出打表公式:

                                    f[1]=1;   f[2]=2;

                                    f[n]=f[n-1]              //n是奇数

                                    f[n]=f[n-1]+f[n/2]   //n是偶数

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1000010];
int main()
{
    int n;
    a[1] = 1;
    a[2] = a[3] = 2;
    a[4] = a[5] = 4;
    for(int i=6;i<=1000000;i++)
    {
        if(i%2==0)
            a[i] = (a[i-1] + a[i/2])%1000000000;//这里取i/2的值
        else a[i] = a[i-1]%1000000000;
    }
    while(cin >> n)
    {
        cout << a[n] << endl;
    }
    return 0;
}

 

posted on 2017-06-05 19:45  九月旧约  阅读(142)  评论(0编辑  收藏  举报

导航