[VJ][简单背包]Balance

Balance

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 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 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.

Examples

Input

2 4	
-2 3 
3 4 5 8

Output

2

正确解法:

不会写啊,这是难的背包,对没错qwq

首先是杠杆原理,重量*杆长。我们把7500当成一个平衡点,左边的都是小于7500,右边的都是大于7500.

f[i][j] 表示选前i个钩子所构成的 j 的平衡值。

第一个for表示钩子,第二个for表示平衡值 j ,第三个for表示选n个位置。

如果f[i-1][j]有值的话(毕竟前i个钩子在n个位置的所有情况 j 肯定0-15000不是全都能取到的)选第i个钩子,在第n个位置的情况。f[i][j+a[k]*b[i]]+=f[i-1][j];

初始化要 f[0][7500]=1.当一个钩子都不选的话,这个杠杆平衡 达到7500 的情况是1种。

最后的答案就是f[g][7500] (g是钩子的个数)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<string>
 6 #include<cstring>
 7 #include<map>
 8 using namespace std;
 9 int n, g;
10 int a[30],b[30], f[30][15100];
11 int main()
12 {
13     cin >> n >> g;
14     for (int i = 1; i <= n; i++)
15         cin >> a[i];
16     for (int j = 1; j <= g; j++)
17         cin >> b[j];
18     f[0][7500] = 1;
19     for(int i=1;i<=g;i++)
20         for (int j = 0; j <= 15000; j++)
21             if (f[i - 1][j])
22             {
23                 for (int k = 1; k <= n; k++)
24                     f[i][j + a[k] * b[i]] += f[i - 1][j];
25             }
26     cout << f[g][7500] << endl;
27     return 0;
28 }
View Code
No matter how you feel, get up , dress up , show up ,and never give up.
原文地址:https://www.cnblogs.com/Kaike/p/9985563.html