Sumsets (Full backpack)

Source: Internet
Author: User
Tags cmath

Sumsets
Time Limit: 2000MS Memory Limit: 200000K
Total Submissions: 15045 Accepted: 5997

Description

Farmer John commanded his cows to search for different sets of numbers the sum to a given number. The cows use is numbers that is an integer power of 2. Here is 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).  

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 representation).

Sample Input

7

Sample Output

6
The puzzle: Let the number of 2^k with the sum of N, just see that is the mother function, did not fight out, and certainly not because the amount of data is too large to definitely time out;
Can actually write with a complete backpack;
Thinking very well, the 2^k of the goods to the backpack inside put; dp[j]+=dp[j-i] for the number of programs;
Recursion is also very good to think, if it is odd, direct dp[i]=dp[i-1]
If it is an even number, dp[i] can be obtained by DP[I/2], or it can be obtained by dp[i-1];
Full backpack:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < Algorithm>using namespace std, #define MEM (x, y) memset (x,y,sizeof (×)) #define T_T while (t--) #define SI (x) scanf ("%d", &X) #define SL (x) scanf ("%lld", &x) typedef long LONG ll;const int maxn=1000010;const int Mod=1e9;int bag[maxn];int Main () {int N; ~SCANF ("%d", &n) {mem (bag,0); bag[0]=1; for (int. i=1;i<=n;i*=2) {for (int j=i;j<=n;j++) { Bag[j]+=bag[j-i]; Bag[j]%=mod; }} printf ("%d\n", Bag[n]); }return 0;}

The parent function timed out:

#include <stdio.h>const int maxn= 1000010;int main () {    int a[maxn],b[maxn],n;    while (~SCANF ("%d", &n)) {        int i,j,k;        for (i=0;i<=n;i++) {            a[i]=1;b[i]=0;        }        for (i=2;i<=n;i*=2) {for            (j=0;j<=n;j++) for (                k=0;k+j<=n;k+=i)                    b[j+k]+=a[j];                for (j=0;j<=n;j++)                    a[j]=b[j],b[j]=0;            }        printf ("%d\n", A[n]);    }    return 0;}

recursion;

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < Algorithm>using namespace std, #define MEM (x, y) memset (x,y,sizeof (×)) #define T_T while (t--) #define SI (x) scanf ("%d", &X) #define SL (x) scanf ("%lld", &x) typedef long LONG ll;const int maxn=1000010;const int Mod=1e9;int dp[maxn];int m Ain () {int n;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/2])%MOD;} while (~SCANF ("%d", &n)) printf ("%d\n", Dp[n]); return 0;}

  

Sumsets (Full backpack)

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.