POJ 1837 Balance (01 backpack) __ Knapsack problem

Source: Internet
Author: User
Tags integer numbers
Description

Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from no other ordinary balance.

It orders two arms of negligible weight and each arm ' s length is 15. Some hooks are attached to this arms and Gigel wants to hang up Some weights in his collection of G weights (1 <= g <= knowing that this weights have distinct values in the range 1..25. Gigel may droop any weight of any hooks but he's forced to use all the weights.

Finally, Gigel managed to balance the device using the experience him gained at the National Olympiad in Informatics. Now it would like to know in many ways the device can be balanced.

Knowing the repartition of the hooks and the set of the weights write a program that calculates the number of Possibilitie s to balance the device.

It is guaranteed that would exist at least one solution to the evaluation.


Input

The input has the following structure:the the "contains" number C (2 <= C <=) and the number G (2 <= G <= 20); The next line contains C integer numbers (this numbers are also distinct and sorted in ascending order) in the range-15. Representing the repartition of the hooks; Each number represents the ' position relative to the ' center of the ' balance on the ' X axis (when no weights are the Device is balanced and lined up to the X axis; The absolute value of the distances represents the distance between the hook and the balance of the sign Mbers determines the "arm of the balance to which" the hook is attached: ' for the "left arm and ' +" for the right arm); On the next line there are G natural, distinct and sorted in ascending order numbers in the range 1..25 representing the W Eights ' values.


Output

The output contains the number M representing the number of possibilities to poise the balance.


Sample Input

2 4 
-2 3 3 4 5 8 


Sample Output

2


the

There is a balance, 15 of the arm of the left arm, then gives the c,g.

C represents a few hooks, the hook gives a negative number representing the distance from the left arm, and a positive right arm;

G on behalf of a few weights, you want to make this balance to maintain a number of methods, require all weights used up.


train of Thought

DP[I][J] dp[i][j], I I for the number of weights placed, J J for the equilibrium state, when the j=0 j=0 balance.

But this definition of J J is likely to be negative and is not allowed when used as an array subscript, so we calculate that the maximum of the left or right of the Libra is 7500.

Then dp[i][7500] dp[i][7500] represents the number of methods of the balance of I-I-weights.

conversion for a 01 knapsack problem

State transition equation: Dp[i][j+w[i]∗c[k]]+=dp[i−1][j] dp[i][j+w[i]*c[k]]+=dp[i-1][j]

For the first I weights, it can be placed anywhere, so it contributes to every hook j+w[i]∗c[k] j+w[i]*c[k].

Final answer: dp[g][7500] dp[g][7500]


AC Code

 #include <iostream> #include <algorithm> #include <stdio.h> #include <
string.h> #include <math.h> #include <iostream> using namespace std;
#include <vector> #include <queue> int dp[25][16000];
int a[25],b[25];
    int main () {int c,g;
        while (~SCANF ("%d%d", &c,&g)) {memset (dp,0,sizeof (DP));
        for (int i=1; i<=c; i++) scanf ("%d", a+i);
        for (int i=1; i<=g; i++) scanf ("%d", b+i);
        Dp[0][7500]=1;
                    for (int i=1; i<=g; i++) for (int j=0; j<15000; J + +) if (dp[i-1][j))//A small optimization
        for (int k=1; k<=c; k++) dp[i][j+b[i]*a[k]]+=dp[i-1][j];
    printf ("%d\n", dp[g][7500]);
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.