Project Euler 106:special subset sums:meta-testing special subset and: meta-test

Source: Internet
Author: User

Special subset Sums:meta-testing

Let S (A) represent the sum of elements in set A of size n. We shall call it a special sum set if for any of the non-empty disjoint subsets, B and C, the following properties is true:

    1. S (B) ≠s (C); That's, sums of subsets cannot be equal.
    2. If B contains more elements than C then S (B) > S (c).

For this problem we shall assume a given set contains n strictly increasing elements and it already satisfies the SEC Ond rule.

Surprisingly, out of the "possible subset pairs that can is obtained from a set for which n = 4, only 1 of these pairs n Eed to is tested for equality (first rule). Similarly, when n = 7, only over the of the 966 subset pairs need to be tested.

For n = A, how many of the 261625 subset pairs that can is obtained need to being tested for equality?

Note:this problem is related to problem 103 and problem 105.

Special subsets and: meta-tests

Memory S (a) is the and of all elements in a set of size n. If any of the two non-empty and disjoint subsets B and C of a are satisfied with the following conditions, we call a a special and set:

    1. S (B) ≠s (C); that is, any subset is different from the other.
    2. If the element in B is more than C, then S (b) > S (c).

In this question we assume that the collection contains n strictly monotonically incrementing elements, and that it satisfies the second condition.

Surprisingly, when n = 4 o'clock, only 1 groups in all possible subset pairs of 25 need to verify the subset and be equal (the first condition). Similarly, when n = 7 o'clock, only 70 groups of all possible subset pairs of 966 need to be tested.

When n = 12 o'clock, how many groups need to be tested in all possible subset pairs of 261625 groups?

Note: This question is related to question 103th and 105th.

Solving

First of all to say is the language is poor, the topic did not understand, engaged for a long time.

Note the points:

1. The collection here and not necessarily a special subset

2. This set element must be strictly incremental.

3. The set is already satisfied with the second condition, the problem is not to be judged

4. Ask for a subset of the number of possible equal

4.1 subset pairs, two subsets must also be disjoint

4.2 "need to test" means that the subset that does not need to be tested is necessarily unequal, the subset pairs that "need to be tested" may be equal, note the possibility of this , it may not be equal, it can be understood as: The maximum number required

When 1 2 3 are correctly understood: the set of n is as long as the increment sequence of any n number, such as: 1, 2, 3, 4,、、、、、、 N

4th: The number of possible equality of subsets

Under what circumstances are the two subsets of B and C elements equal to each other?

Note: Set A is strictly incremented, then subsets B and C must also be strictly incremented.

Subsets B, C elements, and certain unequal conditions:

1. The number of elements in set B and C is not equal

2. Minimum value of Set B > Maximum value of Set C

Turn

Subset B, c elements, and possibly equal cases:

1. The number of elements in the set B, C is equal and the elements in B are greater than the elements in C, and the elements in C are greater than the elements in B.

According to the above can be solved

Java

 PackageLevel4;Importjava.util.ArrayList;Importjava.util.Arrays; Public classpe0106{ Public Static voidrun () {intA[] = {1,2,3,4,5,6,7,8,9,10,11,12};//int a[] = {1219, 1183, 1182, 1115, 1035, 1186, 591, 1197, 1167, 887, 1184, 1175};Arrays.sort (A);            Meta_testing (A); }     Public Static voidMeta_testing (int[] a) {        //all subsetsArraylist<arraylist<integer>> sets =Makesubsets (a); intSize =sets.size (); intcount_equal = 0; intCount_sets = 0; System.out.println (Total number of subsets: "+size);  for(inti=0;i<size;i++) {ArrayList<Integer> Set1 =Sets.get (i);  for(intj=i+1;j<size;j++) {ArrayList<Integer> Set2 =Sets.get (j); //do not intersect                if(!Isdisjoint (Set1,set2)) {Count_sets++; if(set1.size () = =set2.size ()) {                        ints = 0; intt = 0;  for(intK = 0;k<set1.size (); k++){                            if(Set1.get (k) >Set2.get (k)) s= 1; if(Set1.get (k) <Set2.get (k)) T= 1; }                        if(s = = 1 && t = = 1) count_equal++; } }}} System.out.println ("Subset Pair Quantity:" +count_sets); System.out.println ("Possible equal number of subsets:" +count_equal); }    //two subsets of elements intersect true intersect false does not intersect     Public Static BooleanIsdisjoint (arraylist<integer> set1,arraylist<integer>Set2) {        intSize1 =set1.size (); intSize2 =set2.size (); ArrayList<Integer> set =NewArraylist<integer>();  for(inti=0;i<size1;i++){            intelement =Set1.get (i); if(Set.contains (Element))return true; ElseSet.add (Element); }         for(inti=0;i<size2;i++){            intelement =Set2.get (i); if(Set.contains (Element))return true; ElseSet.add (Element);        } set.clear (); return false; }            //find out all the subsets     Public StaticArraylist<arraylist<integer>> Makesubsets (inta[]) {ArrayList<ArrayList<Integer>> sets =NewArraylist<arraylist<integer>>();  for(inti=1;i< (int) Math.pow (2,a.length); i++) {ArrayList<Integer> set =Makesubset (a,i);        Sets.add (set); }        returnsets; }    //finding a subset//use and 1 to perform and operate and shift//001001 corresponds to the position of the 2nd 5th according to the position of the 1.//&000001//----------        //1 Remove the number corresponding to the position//move right below one post//000100//here's the same.     Public StaticArraylist<integer> Makesubset (int[] A,intm) {ArrayList<Integer> set =NewArraylist<integer>();  for(intI=0;i<a.length; i++){            if(M>0 && (m&1) ==1) {Set.add (a[i]); } m=m>>1; }        returnset; }     Public Static voidMain (string[] args) {LongT0 =System.currenttimemillis ();        Run (); LongT1 =System.currenttimemillis (); Longt = T1-t0; System.out.println ("Running Time=" +t/1000+ "s" +t%1000+ "MS"); }}
Java Code
Total number of subsets: 4095 subset Pair Quantity:261625 Possible equal number of subsets:21384runningtime =3s496ms

Python

#CODING=GBKImportItertoolsse=set (Range (1,13)) C=0 forIinchXrange (2, Len (SE)): forMinchitertools.combinations (se,i): forNinchItertools.combinations (se-set (m), i): t=0 forKinchRange (len (m)):ifM[k]>N[k]: t=1s=0 forKinchRange (len (m)):ifm[k]<N[k]: s=1ifS==1 andT==1: C+=1PrintC/2.

Mathblog directly to find the answer, all subsets of the number is better, as for the later use of Cattleya number, the problem is not too much to explain, I do not know why.

Answer =

Answers you see in the solution:

#CODING=GBKImportItertoolsdefC (n,k): Result= 1 forIinchRange (k): Result*= N-I result/= i + 1returnresultdefCatalan (n):returnC (2 * n, N)/(n + 1)defE106meta (n): Result=0 forK1inchRange (1, N): forK2inchRange (1,min (K1,N-K1) +1): x= C (N,K1) *c (N-k1,k2)ifK1 = =k2:x/= 2result+=xreturnresultdefe106 (n): Result=0 forKinchRange (2,N/2 + 1): Result+ = C (n,2*k) * (c (2*k,k)/2-Catalan (k))returnresultif __name__=='__main__':    asserte106 (7) = = 70PrintE106 (12)

Project Euler 106:special subset sums:meta-testing special subset and: meta-test

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.