Balance
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 11436 |
|
Accepted: 7130 |
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
Test Instructions: There is a C hook on a balance, the position of the first hook is c[i],c[i] < 0 means that the hook is on the left side of the Origin . C[i] > 0 indicates that the hook is on the right side of the origin point; then give the weight of G hook code, ask how many kinds of hanging method to keep balance.
Analysis: When the balance is balanced, a hook code is attached to each balance, the state of the balance changes, and this state can be obtained by a number of previous States .
First, define the concept of a balance J.
When the balance is j=0, the balance of the Libra is reached, j>0, indicating the right side of Libra (right half of the x-axis), j<0 the opposite
At this point, the balance J can be regarded as a value to measure the current Libra state.
It is therefore possible to define a state array dp[i][j], which means the number of hooks with a balance of J when the front I hook code is full.
Because the range of distance l[i] is -15~15, the range of the hook code weight is w[i] is 1~25, the maximum number of hooks is 20
So the most extreme balance is that all objects hang at the far end, so the maximum balance is j=15*20*25=7500. In principle there should be dp[1~20][-7500 ~ 7500].
Therefore, in order not to let the subscript appear negative numbers, do a processing, so that the array is opened to dp[1~20][0~15000], so that 7500 corresponds to 0, then when the j=7500 balance state.
/* Dp[i][j] indicates the number of scenarios where the difference between the ends of the balance (right end-left) is used for the first I hook code. Extreme situation for all hooks on the left side of the leftmost position, according to the topic data that the time difference value is 0-15 * 25 * 20 =-7500, so to add an offset, 7500 corresponds to 0, assuming the number of hooks is G, the final answer is dp[g][7500]. Transfer equation: Dp[i][j + w[i] * L[k]] = Sigma (Dp[i-1][j]). K for k hook position */#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int dp[21][ 15005];int l[21], W[21];int main () {int C, G; while (~SCANF ("%d%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 corresponding to 7500, hang the first 0 hook code, the Libra to reach the equilibrium state 7500 method has 1, that is, both ends are not hanging for (int i = 1; I <= G; i++) {for (int j = 0; J <= 1 5000; J + +) {if (Dp[i-1][j]) {/////When a i-1 hook code is placed, the status J has been present and the number of methods is counted, the statistic result is directly used, otherwise the current state J for (int k = 1) is ignored; K <= C; k++) {dp[i][j + w[i] * L[k]] + = dp[i-1][j]; }}}} printf ("%d\n", dp[g][7500]); } return 0;}
POJ 1837 Balance (knapsack Problem of dynamic planning)