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.
 
  
1:DP
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1| 1
using namespace std;
typedef long long LL;
int dp[60010],use[60010],sum;
int num[10];
//优化的多重背包
void solve()
{
    sum /=2;
    memset(dp,0,sizeof(dp));
    dp[0] = 1;

    for(int i = 1; i <= 6; i++)
    {
        if(num[i] > 0)
        {
            memset(use,0,sizeof(use));
            for(int j = i; j <= sum; j++)
            {
                if(dp[j-i] && !dp[j] && use[j-i] < num[i])
                {
                    dp[j] = 1;
                    use[j] = use[j-i]+1;
                }
            }
        }
    }

    if(dp[sum]) printf("Can be divided.\n\n");
    else printf("Can't be divided.\n\n");
}

int main()
{
    int item = 1;
    while(~scanf("%d %d %d %d %d %d",&num[1],&num[2],&num[3],&num[4],&num[5],&num[6]))
    {
        if(num[1]==0&&num[2]==0&&num[3]==0&&num[4]==0&&num[5]==0&&num[6]==0) break;
        sum = 0;
        for(int i = 1; i <= 6; i++)
            sum += num[i]*i;
        printf("Collection #%d:\n",item++);
        if(sum%2 != 0)
        {
            printf("Can't be divided.\n\n");
            continue;
        }
        else
            solve();
    }
    return 0;
}


==========华丽的分割线============
 
  
2:二进制优化为01背包。
 
  
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1| 1
using namespace std;
typedef long long LL;
const int N = 6;  
int V, total, sum;  
int bag[120005];  
int w[15], n[15], v[1005];  
  
void _01_bag()  
{  
    int i, j;  
    memset(bag, 0, sizeof(bag));  
    for(i = 0; i < total; i++)  
    {  
        for(j = sum; j >= v[i]; j--)  
        {  
            bag[j] = max(bag[j], bag[j-v[i]]+v[i]);  
        }  
    }  
}  
  
int main()  
{  
    int i, temp, zz = 1;  
    while(scanf("%d %d %d %d %d %d", &n[0], &n[1], &n[2], &n[3], &n[4], &n[5]))  
    {  
        if(n[0] + n[1] + n[2] + n[3] + n[4] + n[5] == 0) break;  
        printf("Collection #%d:\n", zz++);  
        V = 0;  
        for(i = 0; i < N; i++)  
        {  
            w[i] = i + 1;  
            V += w[i]*n[i]; //求总和  
        }  
        if(V%2 == 1)    //总和是奇数则不能平分  
        {  
            printf("Can't be divided.\n\n");  
            continue;  
        }  
        sum = V/2; total = 0;  
        for(i = 0; i < N; i++)  //二进制压缩为——01背包  
        {  
            if(n[i] == 0) continue;  
            temp = 1;  
            while(n[i] > temp)  
            {  
                v[total++] = temp*w[i]; //将新的值赋给v[]  
                n[i] -= temp;  
                temp *= 2;  
            }  
            v[total++] = n[i]*w[i];  
        }  
        _01_bag();  //用新的v[]数组直接拿来01背包  
        if(bag[sum] != sum)  
            printf("Can't be divided.\n\n");  
        else  
            printf("Can be divided.\n\n");  
    }  
  
    return 0;  
} 

==========更加华丽的分割线============

 
  
3:DFS
 
  
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1| 1
#include<iostream>  
using namespace std;  
typedef long long LL;
int n[7];  //价值为i的物品的个数  
int SumValue;  //物品总价值  
int HalfValue;  //物品平分价值  
bool flag;    //标记是否能平分SumValue  
  
void DFS(int value,int pre)  
{  
    if(flag)  
        return;  
  
    if(value==HalfValue)  
    {  
        flag=true;  
        return;  
    }  
  
    for(int i=pre;i>=1;i--)  
    {  
        if(n[i])  
        {  
            if(value+i<=HalfValue)  
            {  
                n[i]--;  
                DFS(value+i,i);  
  
                if(flag)  
                    break;  
            }  
        }  
    }  
    return;  
}  
  
int main(int i)  
{  
    int test=1;  
    while(cin>>n[1]>>n[2]>>n[3]>>n[4]>>n[5]>>n[6])  
    {  
        SumValue=0;  //物品总价值  
  
        for(i=1;i<=6;i++)  
            SumValue+=i*n[i];  
  
        if(SumValue==0)  
            break;  
  
        if(SumValue%2)    //sum为奇数,无法平分  
        {  
            cout<<"Collection #"<<test++<<':'<<endl;  
            cout<<"Can't be divided."<<endl<<endl;    //注意有空行  
            continue;  
        }  
  
        HalfValue=SumValue/2;  
        flag=false;  
  
        DFS(0,6);  
  
        if(flag)  
        {  
            cout<<"Collection #"<<test++<<':'<<endl;  
            cout<<"Can be divided."<<endl<<endl;  
            continue;  
        }  
        else  
        {  
            cout<<"Collection #"<<test++<<':'<<endl;  
            cout<<"Can't be divided."<<endl<<endl;  
            continue;  
        }  
    }  
    return 0;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值