B - Dividing

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Submit Status

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 #include<cstdio>
 2 #include<string.h>
 3 #include<math.h>
 4 using namespace std;
 5 int dp[100000];
 6 int main()
 7 {
 8     int a[11];
 9     int t=1;
10     int cnt;
11     while(scanf("%d %d %d %d %d %d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])!=EOF)
12     {
13         if(a[1]+a[2]+a[3]+a[4]+a[5]+a[6]==0) break;
14         int sum,x;
15         sum=a[1]*1+a[2]*2+a[3]*3+a[4]*4+a[5]*5+a[6]*6;
16         printf("Collection #%d:\n",t);
17         t++;
18         if(sum%2)
19         {
20             printf("Can‘t be divided.\n\n");
21             continue;
22         }
23         x=sum/2;
24         memset(dp,0,sizeof(dp));
25         dp[0]=1;
26         for(int i=1;i<=6;i++)
27         {
28             if(!a[i])
29                 continue;
30             for(int j=1;j<=a[i];j*=2)
31             {
32                cnt=j*i;
33                for(int k=x;k>=cnt;k--)
34                {
35                    if(dp[k-cnt])
36                     dp[k]=1;
37                }
38                a[i]-=j;
39             }
40             cnt=a[i]*i;
41             if(cnt)
42             {
43                 for(int k=x;k>=cnt;k--)
44                 {
45                     if(dp[k-cnt])
46                         dp[k]=1;
47                 }
48             }
49         }
50         if(dp[x])
51             printf("Can be divided.\n");
52         else printf("Can‘t be divided.\n");
53         printf("\n");
54     }
55     return 0;
56 }

B - Dividing

时间: 2024-08-02 13:10:56

B - Dividing的相关文章

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

AC日记——Dividing poj 1014

Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 69575   Accepted: 18138 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 marbl

uva562 - Dividing coins(01背包)

题目:uva562 - Dividing coins(01背包) 题目大意:给出N个硬币,每个硬币有对应的面值.要求将这些硬币分成两部分,求这两部分最小的差值. 解题思路:先求这些硬币能够凑出的钱(0, 1背包),然后再从sum(这些硬币的总和)/2开始往下找这个值能否由这些硬币凑出.要注意的是,可以由前n个硬币组成那样也是可以组成的面值. 代码: #include <cstdio> #include <cstring> const int N = 105; const int m

poj 1004 Dividing

题目大意是,从输入六个数 ,第i个数代表价值为i的有几个,平均分给两个人 ,明摆着的背包问题,本来以为把他转化为01背包,但是TLe,后来发现是12万的平方还多,所以妥妥的TLE,后来发现这是一个完全背包问题,然后即纠结了 ,没学过啊 ,最后发现思想好i是蛮简单的,水水的A掉了,最后注意一下初始化问题和输入问题后就好了 #include <stdio.h> #include <string.h> int a[10]; int dp[120005]; int maxx(int a,i

uva 562 Dividing coins

Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nickel, which was made of copper. They were both so eager to get it and the fighting was so fierce, they stretched the coin to great lengt

hdu6060 RXD and dividing 贪心

/** 题目:hdu6060 RXD and dividing 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6060 题意:贪心 给定一颗树,n个节点,编号为1~n.将2~n编号的节点分成k份. 每一份分别和编号1的节点取并集.然后求每一份的节点连通的最小边权和: 然后k份获得的边权和加起来:问:求可以获得的k份边权和的总和的最大值. 思路:通过画树容易发现,假如k无穷大,如果节点x为根的子树有num个节点,那么x与x的父节点相连的那条边权最多加nu

POJ 1014 Dividing 【DP 之 多重背包 / 二进制优化】

Language: Default Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63647   Accepted: 16488 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal

HDU 1059 Dividing (多重背包)

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17638    Accepted Submission(s): 4949 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

POJ 1014 Dividing 背包

这道题使用多重背包,不过其实我也不太明白为什么叫这个名字. 因为感觉不是什么多重,而是物体的分解问题. 就是比如一个物体有数量限制,比如是13,那么就需要把这个物体分解为1, 2, 4, 6 如果这个物体有数量为25,那么就分解为1, 2, 4, 8, 10 看出规律吗,就是分成2的倍数加上位数,比如6 = 13 - 1 - 2 - 4, 10 = 25 - 1 - 2 - 4 - 8,呵呵,为什么这么分解? 因为这样分解之后就可以组合成所有1到13的数,为25的时候可以组合成所有1到25的数啦

hdu 1059 Dividing

题目: 链接:点击打开链接 题意: 判断是否能够平分弹珠. 算法: 多重背包. 思路: 模板...dp[i]中i表示花费.. 代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; int n[7]; int dp[120010]; int V; void bag_01(int c,int w)//01背包 { for(int i=V; i>=c; i--) dp