POJ 1837.Balance

Source: Internet
Author: User
Tags integer numbers

Balance Time limit:1000MS Memory Limit:30000KB 64bit IO Format:%i64d &%i64 U Submit Status Practice POJ 1837

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-1 5..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 Weights ' 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

Dynamic planning Issues


State analysis

Using dynamic programming, it can be seen that one dimension is the use of the first I weight
The meaning of the last layer is: adding the first weight to the balance system to balance the balance
Then the meaning of each layer is: adding the first weight to the balance system so that the balance reaches a certain state
For balances, the equilibrium state can be expressed as a torque

I is the number of weights used J is the moment
The answer should be dp[g][0], i.e. using G weights (all used up) and a torque of 0 (maintain balance)

Initial state is dp[0][0]=1(balance is maintained when using the first 0 weights, i.e. no weight balance is placed)

The following examples are:

dp[4][0]=dp[3][16]+dp[3][-24]
DP[3][16]=DP[2][26]+DP[2][1]
DP[2][26]=DP[1][34]+DP[1][13]
DP[1][13]=DP[0][19]+DP[0][4]
DP[1][34]=DP[0][40]+DP[0][25]
DP[2][1]=DP[1][9]+DP[1][-11]
dp[1][9]=dp[0][15]+Dp[0][0]
DP[1][-11]=DP[0][-5]+DP[0][-20]
DP[3][-24]=DP[2][-14]+DP[2][-39]
DP[2][-14]=DP[1][-6]+DP[1][-28]
dp[1][-6]=dp[0][0]+dp[0][-15]
DP[1][-28]=DP[0][-22]+DP[0][-37]
DP[2][-39]=DP[1][-31]+DP[1][-51]
DP[1][-31]=DP[0][-25]+DP[0][-40]
DP[1][-51]=DP[0][-45]+DP[0][-60]

dp[i][j]=dp[i-1][j-weight[i]*hook[0]]+dp[i-1][j-weight[i]*hook[1]]+ ...
Even with an I weight and a torque of J equals the use of a i-1 weight and a torque of (J minus the possible torque of weight i)

Also known as Dp[i][j]+=dp[i-1][j-weight[i]*hook[k]] k=0,1......k-1

Because the subscript cannot be negative , the moment is changed to a positive number
Hook between -15~+15 weights up to 20, each final 25 namely torque Max is 15*20*25=7500

The original torque range is -7500~0~7500
Plus 7500, there's 0~7500~15000.

AC Code: Github

1 /*2 By:ohyee3 Github:ohyee4 Email:[email protected]5 Blog:http://www.cnblogs.com/ohyee/6 7 かしこいかわいい? 8 エリーチカ! 9 to write out the хорошо code OH ~Ten */ One  A#include <cstdio> -#include <algorithm> -#include <cstring> the#include <cmath> -#include <string> -#include <iostream> -#include <vector> +#include <list> -#include <queue> +#include <stack> A#include <map> at using namespacestd; -  - //DEBUG MODE - #defineDebug 0 -  - //Loops in #defineREP (n) for (int o=0;o<n;o++) -  to intC,g;//c Hook number g weight number + Const intMAXN = -; - intHOOK[MAXN];//Hook Position the intWEIGHT[MAXN];//Weight Weight *  $ intdp[maxn][15005];Panax Notoginseng  - BOOLDo () { the     if(SCANF ("%d%d", &c,&g) = =EOF) +         return false; A REP (C) thescanf"%d", &hook[o+1]); + REP (G) -scanf"%d", &weight[o+1]); $  $     //DP -     /* - State Analysis the  - Using dynamic programming, it can be seen that one dimension is the use of the first I weightWuyi the meaning of the last layer is: adding the first weight to the balance system to balance the balance the then the meaning of each layer is: adding the first weight to the balance system so that the balance reaches a certain state - for balances, the equilibrium state can be expressed as a torque Wu  - I is the number of weights used J is the moment About The answer should be dp[g][0], i.e. using G weights (all used up) and a torque of 0 (maintain balance) $ Initial state is dp[0][0]=1 (balance is maintained when using the first 0 weights, i.e. no weight balance is placed) -  - The following examples are: - dp[4][0]=dp[3][16]+dp[3][-24] A Dp[3][16]=dp[2][26]+dp[2][1] + Dp[2][26]=dp[1][34]+dp[1][13] the Dp[1][13]=dp[0][19]+dp[0][4] - Dp[1][34]=dp[0][40]+dp[0][25] $ Dp[2][1]=dp[1][9]+dp[1][-11] the Dp[1][9]=dp[0][15]+dp[0][0] the dp[1][-11]=dp[0][-5]+dp[0][-20] the dp[3][-24]=dp[2][-14]+dp[2][-39] the Dp[2][-14]=dp[1][-6]+dp[1][-28] - Dp[1][-6]=dp[0][0]+dp[0][-15] in dp[1][-28]=dp[0][-22]+dp[0][-37] the dp[2][-39]=dp[1][-31]+dp[1][-51] the dp[1][-31]=dp[0][-25]+dp[0][-40] About dp[1][-51]=dp[0][-45]+dp[0][-60] the  the dp[i][j]=dp[i-1][j-weight[i]*hook[0]]+dp[i-1][j-weight[i]*hook[1]]+ ... the The use of I weights with a torque of J equals the use of i-1 weights and torque is (J minus the possible torque of weight i) +  - also known as Dp[i][j]+=dp[i-1][j-weight[i]*hook[k]] k=0,1......k-1 the Bayi because the subscript cannot be negative, the moment is changed to a positive number the Hook between -15~+15 weights up to 20, each final 25 namely Torque Max is 15*20*25=7500 the  - the original torque range is -7500~0~7500 - Plus 7500, there's 0~7500~15000 . the  the     */ theMemset (DP,0,sizeof(DP)); the  -dp[0][0+7500] =1; the      for(inti =1; I <= g;i++) the          for(intj =1; J <=15000; j + +) the              for(intK =1; k <= c;k++)94                 if(J-weight[i] * Hook[k] >=0&& j-weight[i]*hook[k]<=15000) theDP[I][J] + = dp[i-1][j-weight[i] *Hook[k]]; the  theprintf"%d\n", dp[g][7500]);98  About     return true; - }101 102 intMain () {103      while(Do ());104     return 0; the}

POJ 1837.Balance

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.