Sumsets
Time Limit : 6000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 45 Accepted Submission(s) : 20
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem 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).
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,a[n]=a[n-1]
当n为偶数的时候:
如果加数里含有1,则一定至少有2个1,就是对n-2后面+1+1,就是a[n-2]
如果加数里面没有1,即对n/2的每一个加数乘以2,总类数为a[n/2]
所以n为偶数时的总类数为a[n]=a[n-2]+a[n/2]
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 int a[1000000+5]; 13 int main() 14 { 15 int n; 16 a[1]=1,a[2]=2; 17 for(int i=3;i<=1000000;i++) 18 { 19 if(i%2) 20 { 21 a[i]=a[i-1]; 22 } 23 else 24 { 25 a[i]=a[i-2]+a[i/2]; 26 a[i]%=1000000000; 27 } 28 } 29 while(~scanf("%d",&n)) 30 { 31 printf("%d ",a[n]); 32 } 33 return 0; 34 }