【BZOJ2022】Pku1837 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. 输入一个天平若干(<=20)挂钩的位置,将若干(<=20)砝码挂到天平上,问有多少种使天平挂平衡的方法。
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
-2 3
3 4 5 8
Sample Output
2
HINT
第一种放法把(4,8)放左边,(3,5)放右边
第二种放法把((3,4,5)放左边,8单独放右边
Solution
直接背包。
Code
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 6 #ifdef WIN32 7 #define LL "%I64d" 8 #else 9 #define LL "%lld" 10 #endif 11 12 #ifdef CT 13 #define debug(...) printf(__VA_ARGS__) 14 #define setfile() 15 #else 16 #define debug(...) 17 #define filename "" 18 #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout) 19 #endif 20 21 #define R register 22 #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++) 23 #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b)) 24 #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b)) 25 #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0) 26 #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0) 27 #define cabs(_x) ((_x) < 0 ? (- (_x)) : (_x)) 28 char B[1 << 15], *S = B, *T = B; 29 inline int F() 30 { 31 R char ch; R int cnt = 0; R bool minus = 0; 32 while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ; 33 ch == '-' ? minus = 1 : cnt = ch - '0'; 34 while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0'; 35 return minus ? -cnt : cnt; 36 } 37 #include <map> 38 #define maxn 30 39 int a[maxn], b[maxn]; 40 long long f[maxn][20010]; 41 int main() 42 { 43 // setfile(); 44 R int n = F(), m = F(), sum = 0; 45 for (R int i = 1; i <= n; ++i) a[i] = F(); 46 for (R int i = 1; i <= m; ++i) b[i] = F(), sum += b[i]; 47 std::sort(a + 1, a + n + 1); 48 R int minn = a[1] * sum, maxx = a[n] * sum; 49 f[0][10000] = 1; 50 for (R int k = 1; k <= m; ++k) 51 for (R int i = minn; i <= maxx; ++i) 52 for (R int j = 1; j <= n; ++j) 53 f[k][i + 10000] += f[k - 1][i - a[j] * b[k] + 10000]; 54 printf("%lld\n", f[m][10000] ); 55 return 0; 56 }