Poj 1014 dividing

Source: Internet
Author: User
Run ID User Problem Result Memory Time Language Code Length Submit Time
7537109 Kingpro 1014 Accepted 164 K 0 MS C ++ 702B 2010-08-26 18:12:16

 

#include <stdio.h>bool Check(int * val){int half=val[0]/2;for(int j1=0; j1<=val[1]; j1++) for(int j2=0; j2<=val[2]; j2++) for(int j3=0; j3<=val[3]; j3++) for(int j4=0; j4<=val[4]; j4++) for(int j5=0; j5<=val[5]; j5++) for(int j6=0; j6<=val[6]; j6++)if(j1*1+j2*2+j3*3+j4*4+j5*5+j6*6==half) return true;return false;}int main(){for(int n=1; ; n++){int val[7]={0}; bool isEnd=true;for(int i=1; i<=6 && (scanf("%d", &val[i]), isEnd && (isEnd=!val[i]), val[i]>6 && (val[i]=6-val[i]%2), val[0]+=i*val[i], true); i++);if(isEnd) break;printf("Collection #%d:\n%s\n\n", n, (val[0]%2!=0 || !Check(val)) ? "Can't be divided." : "Can be divided.");}return 0;}

 

Generally, a pile of stones indicates a value ranging from 1 to 6 to determine whether two piles can be divided equally. Obviously, if there are n stone k which can be divided, then k can be divided into n + 2. In turn, it is not necessarily true. However, it is true when n> 6. That is to say, stone k has n (n> 6) and stone k has (6-n % 2) results are the same.

The idea is as follows:

If it can be divided, the left and right sides take the same number of stones with the same value, then the remaining two piles of stones are still equal. If you try to remove the stones, the remaining stones are, assume that the number of stones with the left value k is 0, and the number of stones with the right value k is m. At this time, in a pile on the left, there must be a number of stones and a number of values and the same with the m k mentioned above, you can also delete. At this time, we can ensure the minimum m value of the equivalent condition. We can guess that it is 5 or 6 (m's parity judgment ).

In this case, m stones with values of k are equivalent to (6-m % 2) stones. If they can be divided, they can be divided. If they cannot be divided, they cannot be divided.

Let me look at the specific proof process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.