蓝桥杯第二次选拔c.Sumsets

/*
c.Sumsets
Time Limit: 2000MS	
Memory Limit: 200000K
Total Submissions: 13291	
Accepted: 5324
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
USACO 2005 January Silver


题目分析:找规律,规律还是很好发现的,或者直接分析题目,奇数的数目肯定和其前面的偶数相同,比如6有6种分解,7只比6大1,不
存在别的分解,只是将6的所有分解加1,故数目与6相同,偶数间的关系为num[i] = num[i - 2] + num[i / 2] 因为多出来的无非两种东西,
一是num[i - 2]每行末直接加2,
二是num[i / 2]每项乘2,举个例子,
比如8,它的分解数目可以理解为6的分解数加上4的分解数
8:
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 2 + 2
2 + 2 + 2 + 2
4 + 4
1 + 1 + 1 + 1 + 1 + 1 + 2
1 + 1 + 1 + 1 + 4
1 + 1 + 2 + 2 + 2
1 + 1 + 2 + 4
2 + 2 + 4
8

6:
1+ 1 + 1 + 1 + 1 + 1
1 +1 + 1 + 1 + 2
1 +1 + 2 + 2
1 +1 + 4
2 +2 + 2
2 +4

4:
1 + 1 + 1 + 1
1 + 1 + 2
2 + 2
4

这样看的很清楚了,8的分解种包含了4的分解每项乘2和6的分解每行末加2两种情况
*/ 

#include   
const int MAX = 1000000 + 1;  
const int MOD = 1000000000;  
int num[MAX];  
int main()  
{  
    int n;  
    num[1] = 1;  
    num[2] = 2;  
    for(int i = 3; i < MAX; i++)  
    {  
        if(i % 2 == 1)  
            num[i] = num[i - 1];  
        else  
        {  
            num[i] = num[i - 2] + num[i / 2];  
            num[i] %= MOD;  		//稍微动下脑筋就知道对 每一项都保留末尾9尾,产生的后一项末尾9位任是正确的。 
        }  
    }  
    while(scanf("%d",&n) != EOF)  
        printf("%d\n",num[n]);  
}  
//当时一点思路都没有,至今也是模糊的想法。 

你可能感兴趣的:(蓝桥杯第二次选拔c.Sumsets)