Description
Farmer John commanded his cows to search forDifferent sets of numbers that sum to a given number. The cows use is numbers that is an integer power of2. Here is the possible sets of numbers that sum to7: 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+4Help FJ count all possible representations forA given integer N (1<= N <=1, the, the).
Input
A single line with a single integer, N.
Output
as This 9 digits (inbase representation).
Sample Input
7
Sample Output
6
Source
Usaco 2005 January Silver
If I is an odd number, there must be a 1, the f[i-1] in each case plus a 1 to get fi, so f[i]=f[i-1]
If I is an even number, if there is 1, at least two, then f[i-2] each case plus two 1, get I, if not 1, the decomposition of each item in addition to 2, then get F[I/2]
So F[I]=F[I-2]+F[I/2]
Since just output the last 9 digits, don't forget modulo 1000000000
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <stdlib.h>6#include <cmath>7 using namespacestd;8 #defineN 10100069 #defineMOD 1000000000Ten intN; One intDp[n]; A voidinit () { - -dp[1]=1; thedp[2]=2; - for(intI=3; i<n;i++){ - if(i&1){ -dp[i]=dp[i-1]; + } - Else{ +dp[i]=dp[i-1]+dp[i/2]; Adp[i]%=MOD; at } - } - } - intMain () - { - init (); in while(SCANF ("%d", &n) = =1){ - toprintf"%d\n", Dp[n]); + - } the return 0; *}
View Code
POJ 2229 Sumsets (DP or math)