xtu read problem training A - Dividing

A - Dividing

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

 

 

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.

解题:多重背包的二进制优化,第一次搞二进制优化啊。。。。。。无尽的WA


 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <climits>

 7 #include <vector>

 8 #include <queue>

 9 #include <cstdlib>

10 #include <string>

11 #include <set>

12 #define LL long long

13 #define INF 0x3f3f3f3f

14 using namespace std;

15 int dp[200000],w[8] = {0,1,2,3,4,5,6};

16 int num[7],sum,val;

17 int main() {

18     int i,j,k,ks = 1,temp,u;

19     int a[1000];

20     while(true) {

21         for(val = sum = 0,i = 1; i <= 6; i++) {

22             scanf("%d",num+i);

23             sum += num[i];

24             val += i*num[i];

25         }

26         if(!sum) break;

27         printf("Collection #%d:\n",ks++);

28         if(val&1) puts("Can't be divided.");

29         else {

30             temp = val>>1;

31             memset(dp,0,sizeof(dp));

32             for(i = 1; i <= 6; i++) {

33                 int u = log2(num[i]);

34                 for(k = 0; k <= u; k++){

35                     if(k == u){a[k] = num[i]-(1<<u)+1;}

36                     else a[k] = 1<<k;

37                 }

38                 for(k = 0; k <= u; k++){

39                     for(j = temp; j >= a[k]*i; j--){

40                         dp[j] = max(dp[j],dp[j-a[k]*i]+a[k]*i);

41                     }

42                 }

43             }

44             if(dp[temp] == temp) puts("Can be divided.");

45             else puts("Can't be divided.");

46         }

47         printf("\n");

48     }

49     return 0;

50 }
View Code

 

你可能感兴趣的:(read)