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.
Collection #k:”, where k is the number of the test case, and then either
Output
For each colletcion, outputCan 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.
第一种:
转化为01背包来写。 但是因为一个一个背包分会导致n很大,所以我们考虑用二进制来优化。
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 32;
const int M= 2e6;
const int inf = 0x3f3f3f3f;
int cost[N],sz;
int dp[M];
void solve(int n,int v){
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++){
for(int j=v;j>=cost[i];j--)
dp[j]=max(dp[j],dp[j-cost[i]]+cost[i]);
}
}
int a[10];
int main(){
int cas=1;
while(1){
int sum=0;
for(int i=1;i<=6;i++){
scanf("%d",&a[i]);
sum+=i*a[i];
}
if(sum==0) break;
printf("Collection #%d:\n",cas++);
if(sum&1) puts("Can't be divided.");
else {
sz=0;
for(int i=1;i<=6;i++){
int k=1;
while(a[i]-k>0){
cost[sz++]=k*i;
a[i]-=k;
k*=2;
}
cost[sz++]=a[i]*i;
}
sum/=2;
solve(sz,sum);
if(dp[sum]==sum) puts("Can be divided.");
else puts("Can't be divided.");
}
puts("");
}
return 0;
}
第二种:
设dp[i][j] 表示为 前i种数加和得到j时候第i个数还剩下几个。
代码
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 32;
const int M= 1e6;
const int inf = 0x3f3f3f3f;
int dp[M];int a[10]; // 滚动数组优化
void solve(int n,int k){
memset(dp,-1,sizeof(dp));
dp[0]=0;
for(int i=0;i<n;i++){
for(int j=0;j<=k;j++){
if(dp[j]>=0) dp[j]=a[i];
else if(j-(i+1)<0 || dp[j-(i+1)]<=0) dp[j]=-1;
else dp[j]=dp[j-(i+1)]-1;
}
}
}
int main( ){
int cas=1;
while(1){
int flag=0; int sum=0;
for(int i=0;i<6;i++) {
scanf("%d",&a[i]);
if(a[i]==0) flag++;
sum+=a[i]*(i+1);
}
if(flag==6) break;
printf("Collection #%d:\n",cas++);
if(sum&1) puts("Can't be divided.");
else{
solve(6,sum/2);
if(dp[sum/2]>=0) puts("Can be divided.");
else puts("Can't be divided.");
}
puts("");
}
return 0;
}