POJ 2229 sumsets Full Backpack or recursive _ backpack

Source: Internet
Author: User
Tags pow cmath

Sumsets

Time Limit: 2000MS Memory Limit: 200000K
Total submissions: 14968 accepted: 5978

Description farmer John commanded he cows to search for different sets of the numbers of that sum to a given number. The cows use is numbers that are, the integer power of 2. Here are the possible sets of numbers this 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 and 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 representation).

Sample Input

7

Sample Output

6


Q: Given an n, only 2 power times are allowed, and how many different schemes are composed of N.



First of all this problem has a very good thinking: bare full backpack


Up to 20 items, the value is 2^0, 2^1, ..., 2^19.

It's a good way to run a full backpack and make a table. g++ can be passed, C + + has not been ...

#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm > #include <queue> #include <stack> #include <map> #include <set> #include <vector> #def  INE INF 0x3f3f3f #define EPS 1e-8 #define MAXN (1000000+1) #define MAXM (100000) #define RI (a) scanf ("%d", &a) #define Rl (a) scanf ("%lld", &a) #define RF (a) scanf ("%lf", &a) #define RS (a) scanf ("%s", a) #define PI (a) printf ("%d\n", (a) #define PF (a) printf ("%.2lf\n", (a)) #define PL (a) printf ("%lld\n", (a)) #define PS (a) printf ("%s\n", (a)) #define W ( A while (a--) #define CLR (A, B) memset (A, (b), sizeof (a)) #define MOD 1000000007 #define LL Long long #define Lson O&LT;&L
T;1, L, Mid #define Rson o<<1|1, mid+1, R #define LL o<<1 #define RR o<<1|1 using namespace std;
LL DP[MAXN];
int fac[21];
    int Pow (int a, int n) {int ans = 1;
        while (n) {ans *= A;
    n--;
return ans;
 } void Getdp () {   for (int i = 0; i < i++) Fac[i] = Pow (2, I);
    Dp[0] = 1; for (int i = 0; i < i++) for (int j = Fac[i]; J < MAXN J + +) Dp[j] = (Dp[j] + dp[j-fac[i])%10
00000000;
    int main () {GETDP (); int n; Ri (n);
    Pl (Dp[n]);
return 0;
 }


Another kind of thinking:

Two kinds of DP state derivation dp[].

One, dp[i] = dp[i-1];

II, dp[i] = Dp[i-1] + dp[i>>1].

#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm > #include <queue> #include <stack> #include <map> #include <set> #include <vector> #def  INE INF 0x3f3f3f #define EPS 1e-8 #define MAXN (1000000+1) #define MAXM (100000) #define RI (a) scanf ("%d", &a) #define Rl (a) scanf ("%lld", &a) #define RF (a) scanf ("%lf", &a) #define RS (a) scanf ("%s", a) #define PI (a) printf ("%d\n", (a) #define PF (a) printf ("%.2lf\n", (a)) #define PL (a) printf ("%lld\n", (a)) #define PS (a) printf ("%s\n", (a)) #define W ( A while (a--) #define CLR (A, B) memset (A, (b), sizeof (a)) #define MOD 1000000007 #define LL Long long #define Lson O&LT;&L
T;1, L, Mid #define Rson o<<1|1, mid+1, R #define LL o<<1 #define RR o<<1|1 using namespace std;
LL DP[MAXN];
    void Getdp () {dp[0] = 1;
        for (int i = 1; i < MAXN i++) {if (I & 1) dp[i] = dp[i-1];
       Else     Dp[i] = (Dp[i-1] + dp[i >> 1])% 1000000000;
    int main () {GETDP ()}; int n; Ri (n);
    Pl (Dp[n]);
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.