POJ 1837 Balance (about Dynamic Planning)

Source: Internet
Author: User
Tags integer numbers

POJ 1837 Balance (about Dynamic Planning)
Balance

Time Limit:1000 MS   Memory Limit:30000 K
Total Submissions:11436   Accepted:7130

Description

Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance.
It orders two arms of negligible weight and each arm's length is 15. some hooks are attached to these arms and Gigel wants to hang up some weights from his collection of G weights (1 <= G <= 20) knowing that these weights have distinct values in the range 1 .. 25. gigel may droop any weight of any hook but he is forced to use all the weights.
Finally, Gigel managed to balance the device using the experience he gained at the National Olympus IAD in ICS. Now he wowould 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 possibilities to balance the device.
It is guaranteed that will 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 <= 20) and the number G (2 <= G <= 20 );
? The next line contains C integer numbers (these numbers are also distinct and sorted in ascending order) in the range-15 .. 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 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 numbers 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 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

C hooks exist on a balance. The position of the I hook is C [I], and C [I]. <0 indicates that the hook is on the left of the origin, C [I]> 0 indicates that the hook is located on the right side of the origin. Then, the weight of G hook codes is given and the number of hanging methods are used to balance the balance.

 

Analysis: When the balance is reached, every hook code is sent to the balance, and the status of the balance changes, which can be obtained from several previous states.

First, define the concept of a degree of balance j.

When the degree of balance is j = 0, it means that the degree of balance will reach the balance tomorrow, j> 0, it means that tomorrow's degree of balance tends to the right (the right half of the X axis), and j <0 indicates the opposite.

In this case, the degree of balance j can be regarded as a value to measure the running status of the current day.

Therefore, you can define a status array dp [I] [j], which indicates the number of hooks with a degree of balance of j when the first I hooks are full.

Because the distance from L [I] is-15 ~ 15. The hook weight ranges from w [I] to 1 ~ 25. The maximum number of hooks is 20.

Therefore, the most extreme degree of balance is that all objects are mounted at the far end. ThereforeThe maximum degree of balance is j = 15*20*25 = 7500.. In principle, dp [1 ~ 20] [-7500 ~ 7500].

Therefore, in order not to let negative numbers appear in the subscript, make a process to enable the array to be dp [1 ~ 20] [0 ~ 15000], so that 7500 corresponds to 0, when j = 7500, the day is in the balance state.

 

/* Dp [I] [j] indicates the number of solutions when the first I hook code is used and the difference (right-left) between the two ends of the balance is j. In extreme cases, all hook codes are mounted to the leftmost position on the left end. According to the question data, the maximum difference value is 0-15*25*20 =-7500. Therefore, an offset must be added, assume that the number of hooks is G, and the final answer is dp [G] [7500]. Transition equation: dp [I] [j + w [I] * l [k] = Sigma (dp [I-1] [j]). K is the position of the k hook */# include
 
  
# Include
  
   
# Include using namespace std; int dp [21] [15005]; int l [21], w [21]; int main () {int C, G; while (~ Scanf ("% d", & C, & G) {for (int I = 1; I <= C; I ++) scanf ("% d ", & l [I]); for (int I = 1; I <= G; I ++) scanf ("% d", & w [I]); memset (dp, 0, sizeof (dp); // The number of methods that reach each State is initialized to 0 dp [0] [7500] = 1; // 0 corresponds to 7500, after the first 0 hooks are mounted, one of the methods for reaching the balance state 7500 on a daily basis means that neither of the two ends is mounted for (int I = 1; I <= G; I ++) {for (int j = 0; j <= 15000; j ++) {if (dp [I-1] [j]) {// when a hook code is put into the I-1 status j has appeared and has been counted the number of methods, directly use the statistical results, otherwise ignore the current status j for (int k = 1; k <= C; k ++) {dp [I] [j + w [I] * l [k] + = dp [I-1] [j] ;}}} printf ("% d \ n", dp [G] [7500]);} return 0 ;}
  
 


 


 

Related Article

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.