POJ 1837 Balance (DP)

Source: Internet
Author: User
Tags integer numbers

Balance
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 11806 Accepted: 7368

Description

Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any and ordinary balance.
It orders arms of negligible weight and each arm's length is 15. Some Hooks is attached to these arms and Gigel wants to hang up Some weights from his collection of G weights (1 <= g <=) Knowing that these weights has 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 he gained at the National Olympiad in Informatics. Now he would like to know in how 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 for each test case at the evaluation.

Input

The input has the following structure:
? The first line contains the number C (2 <= C <=) and the number G (2 <= G <= 20);
? The next line contains C integer numbers (these numbers is 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 in the X axis (when no weights is attached 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 center and the sign of the Nu Mbers determines the arm of the the balance to which the hook was attached: '-' for the left arm and ' + ' for the right arm);
? On the next line there is 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

Given the position of the C hook and the mass of the G weight, there are several ways to balance the balance.

This problem at first glance to use a search, in fact, DP can. DP[I][J] means the number of cases in which the moment is J when the first weight is hung, and the balance is j=0.
So the state transfer equation isDp[i][k+loc[j]*wei[i]]+=dp[i-1][k], said before (i-1) a heavy load after the torque is K, then at the J Hook to hang the weight I, then the two to add, that is, Dp[i][k+loc[j]*wei[i]] will add dp[i-1][k].

It is important to note that the torque may be negative, so the subscript of the DP array may be out of bounds, so the moment must be added to a number of moments, to prevent the subscript out of bounds.

#include <stack> #include <queue> #include <cmath> #include <cstdio> #include <cstring># include<iostream> #include <algorithm> #pragma commment (linker, "/stack:102400000 102400000") #define Mset0 (t) memset (t,0,sizeof (t)) #define Lson A,b,l,mid,cur<<1#define Rson a,b,mid+1,r,cur<<1|1using namespace Std;const double Eps=1e-6;const int maxn=20+5;int loc[maxn],wei[maxn],n,m,dp[maxn][31000];int Main () {#        Ifndef Online_judge freopen ("In.txt", "R", stdin), #endif//Online_judge while (scanf ("%d%d", &n,&m)!=eof) {        Mset0 (DP);        for (int i=1;i<=n;i++) scanf ("%d", &loc[i]);        for (int i=1;i<=m;i++) scanf ("%d", &wei[i]);        Dp[0][10000]=1;                for (int i=1;i<=m;i++) for (int j=1;j<=n;j++)//for (int k=0;k<=21000;k++)//actually this can be too ... for (int k=loc[j]*wei[i]<0?-1*loc[j]*wei[i]:0;k<=21000;k++) Dp[i][k+loc[j]*wei[i]]+=d P[I-1][K];        printf ("%d\n", dp[m][10000]); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1837 Balance (DP)

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.