Sumsets
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
思路:用完全背包的方法做,状态转移方程:dp[j]+=dp[j-v](j>=v)
代码:
1 #include "cstdio" 2 #include "algorithm" 3 #include "cstring" 4 #include "queue" 5 #include "cmath" 6 #include "vector" 7 #include "map" 8 #include "stdlib.h" 9 #include "set" 10 typedef long long ll; 11 using namespace std; 12 const int N=1e6+5; 13 const int Mod=1e9; 14 #define db double 15 int n; 16 int dp[N]; 17 void solve(){ 18 memset(dp,0,sizeof(dp)); 19 dp[0]=dp[1]=1; 20 for(int i=0;i<22;i++){ 21 int v=1<<i; 22 for(int j = 2;j<=N;j++){ 23 if(j>=v) dp[j]+=dp[j-v]; 24 while (dp[j]>=1000000000) dp[j]-=1000000000; 25 } 26 } 27 } 28 int main(){ 29 solve(); 30 while(scanf("%d",&n)==1){ 31 printf("%d ",dp[n]); 32 } 33 return 0; 34 }