Dividing
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 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. Source |
[Submit] [Go Back] [Status] [Discuss]
背包问题 6种背包给出种类问是否能平分
V等于总的重量,特判一下dp[V/2]是否等于V/2
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 122200
#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 a[10],dp[maxn],sum,V;
void ZeroOnePack(int c,int w){
for(int v=V;v>=c;--v)
dp[v]=max(dp[v],dp[v-c]+w);
}
void CompletePack(int c,int w){
for(int v=c;v<=V;++v)
dp[v]=max(dp[v],dp[v-c]+w);
}
void Mpack(int c,int w,int nl){
if(nl*w>=V)
CompletePack(c,w);
else{
int k=1;
while(k<nl){
ZeroOnePack(c*k,w*k);
nl-=k;k<<=1;
}
ZeroOnePack(nl*c,nl*w);
}
}
int main(){
int cnt=1;
while(rd(a[1])!=EOF){
bool flag=true;
sum=a[1];
FOR(i,2,6){
rd(a[i]);
sum+=a[i];
}
if(sum==0)break;
V=0;
FOR(i,1,6)V+=a[i]*i;
if(V%2!=0)flag=false;
if(flag){
MT(dp,0);
FOR(i,1,6)
Mpack(i,i,a[i]);
if(2*dp[V/2]!=V)flag=false;
}
printf("Collection #%d:\n%s\n\n",cnt++,flag?"Can be divided.":"Can't be divided.");
}
return 0;
}
/*
1 0 1 2 0 0
1 0 0 0 1 1
2 0 0 3 0 0
1 1 1 1 1 1
0 6999 0 6001 0 7000
0 0 0 0 4 5
3333 3333 3333 3333 3333 3333
0 0 0 1 0 2
1 2 3 4 5 6
0 0 0 0 4 5
0 0 2 3 0 0
0 0 0 2 2 1
0 0 0 1 2 2
0 1 2 3 4 5
6 5 4 3 2 1
0 0 5 0 3 0
0 3 0 0 0 1
0 2 0 1 0 0
1 1 1 0 0 0
3334 3334 3333 3333 3333 3333
0 1 1 1 1 1
0 0 0 0 6 5
0 0 0 3 0 2
0 0 1 3 3 4
0 0 0 1 4 6
0 0 0 1 4 5
0 0 0 0 0 0
*/