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.
Sample Input 2 4 -2 3 3 4 5 8 Sample Output 2 Source
Romania OI 2002
|
[Submit] [Go Back] [Status] [Discuss]
给一个天平:给出n个挂钩,‘-’代表在天平左边‘+’代表在天平右边
然后给出m个砝码问能否使用砝码使得天平保持平衡
分析砝码放在左边视为重量为负右边为正,那么当重量为0的时候满足条件
又题目给出数据范围20(臂数)*15(臂值)*25(砝码重)=7500
所以最大重量为7500,把数据右移从-7500--7500 到0---15000
则7500为平衡状态 dp[i][j]表示第i个砝码重量为j的情况有多少种
初始化dp为0 dp[0][7500]=1
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制 #pragma comment(linker, "/STACK:102400000,102400000")//手工开栈 #include <map> #include <set> #include <queue> #include <cmath> #include <stack> #include <cctype> #include <cstdio> #include <cstring> #include <stdlib.h> #include <iostream> #include <algorithm> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define rds(x) scanf("%s",x) #define rdc(x) scanf("%c",&x) #define ll long long int #define maxn 15005 #define mod 1000000007 #define INF 0x3f3f3f3f //int 最大值 #define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i) #define MT(x,i) memset(x,i,sizeof(x)) #define PI acos(-1.0) #define E exp(1) using namespace std; int dp[22][maxn],a[22],b[22]; int n,m; int main(){ while(rd2(n,m)!=EOF){ FOR(i,1,n)rd(a[i]); FOR(i,1,m)rd(b[i]); MT(dp,0);dp[0][7500]=1; FOR(i,1,m)FOR(j,0,15000) if(dp[i-1][j])FOR(k,1,n) dp[i][j+a[k]*b[i]]+=dp[i-1][j]; printf("%d\n",dp[m][7500]); } } /* 2 4 -2 3 3 4 5 8 */