Poj 1837 Balance

Balance
Time Limit: 1000MS
 
Memory Limit: 30000K
Total Submissions: 7628
 
Accepted: 4609
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
몇 가지 방안을 구하는 그룹 배낭 문제는 01 배낭 문제를 구하는 방법과 같은 방법으로 구할 수 있다. 수조로 존재하는 경로 줄을 저장하고 DP
단지 그중에 조별 배낭을 구하는 사상이 많아졌다
f[i][j]는 앞의 i분동에서 j까지의 경로 갯수를 나타내고 j는 모든 분동과 좌표의 곱셈의 합이다
구하는 방안의 수는 f[G][7500]이다.(정상적으로 f[G][0]로 이해해야 하지만 j에 음수가 존재하기 때문에 구간을 비추었고 [-75007500]에서 [01500]로 비추었다)
그룹 배낭 아래의 같은 그룹의 물품을 동시에 처리해야 하기 때문에 분동과 좌표의 곱셈은 양일 수도 있고 음일 수도 있기 때문에 롤러 수조로 실현할 수 없다.
#include<stdio.h>
#include<string.h>

int c[21],v[21];

int f[21][15001];

int main(void)
{
	int C,G;
//	freopen("d:\\in.txt","r",stdin);
	while(~scanf("%d%d",&C,&G))
	{
		int i,j,k;
		for(i=1;i<=C;i++)
			scanf("%d",&c[i]);
		for(i=1;i<=G;i++)
			scanf("%d",&v[i]);
		memset(f,-1,sizeof(f));
		f[0][7500]=1;              // 1, 
		for(i=1;i<=G;i++)
		{
			for(j=15000;j>=0;j--)
			{
				for(k=1;k<=C;k++)
				{
					if(j-v[i]*c[k]>=0 && j-v[i]*c[k]<=15000 &&f[i-1][j-v[i]*c[k]]!=-1)
					{
						f[i][j]=f[i][j]==-1?f[i-1][j-v[i]*c[k]]:f[i-1][j-v[i]*c[k]]+f[i][j];
					}
				}
			}
		}
		printf("%d
",f[G][7500]); } return 0; }

좋은 웹페이지 즐겨찾기