HDU 1059 多重背包 二进制

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20070    Accepted Submission(s): 5627


Problem Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. 
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
 

Input
Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0''. The maximum total number of marbles will be 20000. 

The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
 

Output
For each colletcion, output ``Collection #k:'', where k is the number of the test case, and then either ``Can be divided.'' or ``Can't be divided.''. 

Output a blank line after each test case.
 

Sample Input
   
   
   
   
1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0
 

Sample Output
   
   
   
   
Collection #1: Can't be divided. Collection #2: Can be divided.
 

Source
Mid-Central European Regional Contest 1999
#include<stdio.h>
#include<string.h> 
#include<math.h>
int lg(int x) 
{
	int t=0;
	while(x>=2)
	{
		x=x/2;
		t++;
	}
	return t;
}//求logn(2为底)
int sc(int a[])
{
	int t=0,i;
	for(i=1;i<=6;i++)
	{
		scanf("%d",&a[i]);
		if(a[i]==0)
		{
			t++;
		}
			
	}	if(t==6)
		return 0;
		else return 1;
	
	
}//输入函数,如果全输入0 返回0,否则则返回1;
int main()
{
	int a[7],d[120008],i,j,count=0,b[20008];
	//freopen("input.txt","r",stdin);
	while(sc(a)!=0)
	{
		int l=0;
		memset(d,0,sizeof(d));
		count ++;
		int sum=0,t=1,f;
		for(i=1;i<7;i++)
		{
			if(a[i]!=0) 
			{
				for(j=0;j<lg(a[i]-1);j++)
				{
					b[l++]=pow(2,j)*i;
				}
				 b[l++]=a[i]*i-pow(2,j)*i+i;
			}
		}//转换为01背包
		for(i=1;i<7;i++)
		{
			if(a[i]!=0) 
			{
				sum+=a[i]*i;
			}
		}求总共的价值(背包容量)
		if(sum%2!=0) t=0;//总量为奇数数肯定不能分成相等的两份;
		else {
			for(i=0;i<l;i++)
			{
				for(j=sum/2;j>=b[i];j--)
				{
					if(d[j]<=d[j-b[i]]+b[i])
					{
						d[j]=d[j-b[i]]+b[i];
					}
				}
			}
				
		}
		if(d[sum/2]!=sum/2) t=0;
		printf("Collection #%d:\n",count);
		if(t) printf("Can be divided.\n");
		else printf("Can't be divided.\n");
		printf("\n");
	}
}


你可能感兴趣的:(HDU 1059 多重背包 二进制)