Question 1677: [usaco2005 Jan] sumsets sum time limit: 5 sec memory limit: 64 MB
Submit: 617 solved: 344
[Submit] [Status] 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 ).
A n (1 ≤ n ≤ 10 ^ 6) is given, which is obtained by adding several power numbers of 2. How many methods are there?
Input
An integer n.
Output
Number of methods. This number may be very large. Please output the last 9 digits in decimal format.
Sample input7
Sample output6
There are six methods
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
Question
This question is obviously recursive. Orz f [I] = f [I-1] + (I & 1 = 0? F [I/2]: 0)
Code
1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 using namespace std; 4 5 const int N=1000005; 6 int f[N], n; 7 8 int main() { 9 scanf("%d",&n);10 f[1]=1;11 for(int i=2;i<=n;i++) {12 f[i]=f[i-1];13 if(!(i&1)) f[i]+=f[i>>1];14 f[i]%=1000000000;15 }16 printf("%d\n",f[n]);17 return 0;18 }View code
Bzoj 1677: [usaco2005 Jan] sumsets summation