Balance
原题链接
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 15653 Accepted: 9863
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
平衡
原题链接
时间限制:1000MS内存限制:30000K
提交总数:15653接受:9863
描述
吉格尔有一个奇怪的“天平”,他想要平衡它。实际上,该设备不同于任何其他普通天平。
它命令两只重量可以忽略不计的手臂,每只手臂的长度为15.一些钩子连接在这些手臂上,并且Gigel想要从他的G权重集合中(1 <= G <= 20)挂上一些权重,因为知道这些权重是不同的值在1..25范围内。吉格尔可能下垂任何钩子的重量,但他被迫使用所有的重量。
最后,Gigel利用他在信息学国家奥林匹克运动会上获得的经验设法平衡了设备。现在他想知道该设备可以平衡多少种方式。
知道钩子和权重组的重新分区编写一个程序,计算平衡设备的可能性数量。
保证在评估中每个测试用例至少存在一个解决方案。
输入
输入具有以下结构:
第一行包含数字C(2 <= C <= 20)和数字G(2 <= G <= 20);
下一行包含C个整数(这些数字也是不同的,并按升序排序),范围为-15..15,代表钩子的重新分区;每个数字表示相对于X轴上摆轮中心的位置(当没有重物时,装置平衡并排成X轴;距离的绝对值表示吊钩和平衡中心之间的距离并且数字的符号决定钩子所连接的平衡臂:’ - ‘表示左臂,’+’表示右臂。
在下一行有G自然的,不同的,并按照1..25范围内的升序数字排序,表示权值。
输出
输出包含数字M,表示平衡平衡的可能性数量。
示例输入
2 4
-2 3
3 4 5 8
示例输出
2
资源
罗马尼亚OI 2002
题解
题目的意思是给你N个物品,给每个物品选择权值,使得每个物品的质量和权值的乘积 的和等于0。很典型的01背包。
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxG=25,maxn=25,maxv=7505<<1;
int n,m,nn,f[maxG][maxv],a[maxG],v[maxG];
int read(){int x;scanf("%d",&x);return x;}
int main()
{
n=read();m=read();
memset(f,0,sizeof f);
for (int i=1;i<=n;i++) a[i]=read();
for (int i=1;i<=m;i++) v[i]=read();
for (int i=1;i<=m;i++)
{
for (int j=0;j<=maxv;j++)
if (f[i][j])
for (int k=1;k<=n;k++)
if (0<=j+a[k]*v[i]&&j+a[k]*v[i]<=maxv)
f[i][j+a[k]*v[i]]+=f[i-1][j];
}
printf("%d",f[m][maxv]);
return 0;
}