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 }

xtu read problem training A - Dividing

时间: 2024-11-04 13:37:38

xtu read problem training A - Dividing的相关文章

xtu read problem training 4 B - Multiplication Puzzle

Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 165164-bit integer IO format: %lld      Java class name: Main The multiplication puzzle is played with a row of cards, each containing a s

xtu read problem training 4 A - Moving Tables

Moving Tables Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 102964-bit integer IO format: %lld      Java class name: Main The famous ACM (Advanced Computer Maker) Company has rented a floor of a building wh

xtu read problem training 2 B - In 7-bit

In 7-bit Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 371364-bit integer IO format: %lld      Java class name: Main Very often, especially in programming contests, we treat a sequence of non-whitespace cha

xtu read problem training 3 A - The Child and Homework

The Child and Homework Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 437A64-bit integer IO format: %I64d      Java class name: (Any) Once upon a time a child got a test consisting of multiple-choice

xtu read problem training 3 B - Gears

Gears Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 378964-bit integer IO format: %lld      Java class name: Main Bob has N (1 ≤ N ≤ 2*105) gears (numbered from 1 to N). Each gear can rotate clockwise or co

xtu read problem training B - Tour

B - Tour Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must de

[LeetCode&amp;Python] Problem 728. Self Dividing Numbers

A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Also, a self-dividing number is not allowed to contain the digit zero. G

6D姿态估计从0单排——看论文的小鸡篇——Model Based Training, Detection and Pose Estimation of Texture-Less 3D Objects in Heavily Cluttered Scenes

这是linemod的第二篇,这一篇把训练从online learning 变成了 使用3D model, 并且对于检测结果用 3种方法: color.Pose.Depth来确保不会有false positive.感觉有种不忘初心的感觉(笑 基于linemod,是前一篇的改良 initial version of LINEMOD has some disadvantages. First, templates are learnede online, which is difficule to c

A Gentle Guide to Machine Learning

A Gentle Guide to Machine Learning Machine Learning is a subfield within Artificial Intelligence that builds algorithms that allow computers to learn to perform tasks from data instead of being explicitly programmed. Got it? We can make machines lear