zoukankan      html  css  js  c++  java
  • POJ1014 Dividing

     
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 68550   Accepted: 17892

    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.
    大意:给你6个值,分别为数字“1”~“6”的数量,每个数字本身不可拆分,问可否把这些数字分成和相等的两份。

    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.

    Source

     
     
    discuss里面的玄学剪枝:
    事实上当某种石头个数太大的时候,截掉一截即可,
    我的处理是:
        if (x>60)
           if (x%2) x=61;
           else     x=60;
    这样数据范围就很小了,怎么做都能过~
    为什么是60?
    因为1~6的最小公倍数是60。
     
    用了以后就A了,害怕。
     
     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 int a[7];
     5 int sum;
     6 bool pd;
     7 void dfs(int now){
     8     if(!now){
     9         pd=1;return;
    10     }
    11     for(int i=6;i;i--){
    12         if(!a[i] || now<i)continue;
    13         a[i]--;
    14         dfs(now-i);
    15         if(pd)return;
    16 //        a[i]++;
    17     }
    18 }
    19 int main(){
    20     bool flag;
    21     int i,j;
    22     int cas=1;
    23     while(cas++){
    24         sum=0;
    25         for(i=1;i<=6;i++){
    26             scanf("%d",&a[i]);
    27             if(a[i]>60){
    28                 if(a[i]&1)a[i]=61;
    29                 else a[i]=60;
    30             }
    31         }
    32         for(i=1;i<=6;i++){
    33             sum+=a[i]*i;
    34         }
    35         if(!sum)break;
    36         printf("Collection #%d:
    ",cas-1);
    37         pd=0;
    38         if(sum&1){
    39             printf("Can't be divided.
    
    ");
    40             continue;
    41         }
    42         sum/=2;
    43         dfs(sum);
    44         if(pd)
    45             printf("Can be divided.
    
    ");
    46         else printf("Can't be divided.
    
    ");
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    dhtmlTree简单实例以及基本参数设置
    ewebeditor上传文件大小
    Java中集合类
    Ibatis入门基本语法
    afinal 上传文件服务端接受参数为空
    Android的快速开发框架 afinal
    学习asp.net比较完整的流程
    android中sharedPreferences的用法
    mysql怎样配置ODBC数据源
    细谈虚拟主机常见问题
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5818858.html
Copyright © 2011-2022 走看看