POJ-1014 Dividing(背包+二进制优化)

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 file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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 collection, 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.

翻译

输入

输入文件中的每一行描述一组要分割的大理石。这些行包含六个非负整数n1……n6,其中ni是i值的大理石数量。
如果有一个大理石的值为1,一个大理石的值为3,两个大理石的值为4,则不能将它们拆分为相等的值集。上面的示例将通过输入行“1 0 1 2 0 0”来描述。弹珠的最大总数为20000个。求是否可以把它们拆分为相等的值集

输入文件的最后一行是“0”;不要处理这一行。

输出

对于每个集合,输出“collection#k:”,其中k是测试用例的编号,然后是“可以分割”或“不能分割”。

在每个测试用例后输出一个空行

理解

要用到二进制优化,如数字 31,我们可以把数字31分为:1 2 4 8 16
而 1 2 4 8 16 可以组成 1……31中的所有数字,所以我们就把for(i=1;i<=n;i++) 改为for(i=1;i<=n;i*=2),
再看如2的次方: 1 2 4 8 16 32……中比32小的2的次方(1,2,4,8,16)这5个数字可以组成1……31中任意一个数字:所以我们就利用这一特性,大大的减少了循环次数,节约了时间
代码如下

代码

#include "stdio.h"
#include "algorithm"
#include "string.h"
using namespace std;
int a[9],dp[120009];
int main()
{
    
    
	int i,n,m=0,j;
	while(~scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]))
	{
    
    
		n=0;
		if(a[1]+a[2]+a[3]+a[4]+a[5]+a[6]==0)
			break;
		for(i=1;i<=6;i++)
			n+=a[i]*i;
		if(n%2)
		{
    
    
			printf("Collection #%d:\nCan't be divided.\n\n",++m);
			continue;
		}
		memset(dp,0,sizeof(dp));
		dp[0]=1;
		int mid=n/2;
		for(i=1;i<=6;i++)
		{
    
    
			if(a[i]!=0)
			{
    
    
				for(j=1;j<=a[i];j*=2)//二进制优化 
				{
    
    
					a[i]-=j;
					int s=i*j;
					for(int ii=mid;ii>=s;ii--)
						if(dp[ii-s])
							dp[ii]=1; 
				}
				int s=a[i]*i;//不能组成2的更大的次方了,单独在计算一下
				for(int ii=mid;ii>=s;ii--)
						if(dp[ii-s])
							dp[ii]=1; 
			}
		}
		if(dp[mid])
		printf("Collection #%d:\nCan be divided.\n\n",++m);
		else
		printf("Collection #%d:\nCan't be divided.\n\n",++m);
	}
} 

猜你喜欢

转载自blog.csdn.net/weixin_53623850/article/details/120618574