poj 2229 Sumsets

来源:互联网 发布:淘宝美工需要什么资质 编辑:程序博客网 时间:2024/05/20 05:03

Sumsets
Time Limit: 2000MS Memory Limit: 200000KTotal Submissions: 18601 Accepted: 7277

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

Source

数学题,找规律。

首先,当i为奇数的时候,不难发现,最后的总数总是与前一个偶数的结果相同,是因为,二的幂都是偶数,要想凑成奇数的话,必须使用1,所以,最后的结果总是等于前面的偶数的结果在每一行的首位添加一个一。a[i]=a[i-1];

当i为偶数的时候,仔细观察所有的式子不难发现,其中以一开头的行数等于a[i-1],剩下的以2,4,8,16......开头的行数将其全部除以2后发现,等于a[i/2]的行数,然后将其相加即可得到最后结果,即a[i-1]+a[i/2];

最后不要忘记输出最后九位,%mod;

当然,使用dp方法也可以得出答案


#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;const int mod=1000000000;int a[1000010];int main(){    a[1]=1; a[0]=1;    for(int i=2;i<=1000000;i++){        if(i&1){            a[i]=a[i-1]%mod;        }        else{            a[i]=(a[i-1]%mod+a[i/2]%mod)%mod;        }    }    int n;    while(cin>>n){            cout << a[n] <<endl;          }    return 0;}


0 0
原创粉丝点击