Codeforce round#49 Untitled (Hdu 5339)

Source: Internet
Author: User

Untitled

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 947 Accepted Submission (s): 538


Problem Descriptionthere is an integeraandNIntegersb1,... ,bn . After selecting some numbers fromb1,... ,bn In any order, sayC1,... ,cr , we want to make sure thatAMODC1MOd c2 mod... mod cr=0 (i.e.,aWould become the remainder divided bycI Each time, and at the end, we wantaTo become0). Please determine the minimum value of R. If The goal cannot be achieved, print −1 instead.

Inputthe first line contains one integerT≤5 , which represents the number of testcases.

For each testcase, there is and both lines:

1. The first line contains and integersNanda(1≤n≤,1≤a≤6 ).

2. The second line containsNIntegersb1,... ,bn (∀1≤i≤n,1≤bi≤6 ).

Outputprint t Answers in t lines.

Sample Input22 92 72 96 7

Sample output2-1

Sourcebestcoder Round #49 ($) This topic is a purely violent search of a topic, only one by one points to note is that the sequence should first descending order, why? (Because if the ascending order is i<j,ci less than Cj,ci first to take the remainder operation, then CJ will not make sense) in addition to the simple deep search, attached to my Code
1#include <stdio.h>2#include <stdlib.h>3 #defineMAXN 304 intmin, sum,n;5 intA[MAXN], B[MAXN],C[MAXN];6 intMin (intAintb)7 {8     returnA> b?b:a;9 }Ten intDfsintm) One { A     if(M = = n +1) -     { -         inttemp =sum; the         intLength =0; -          for(intj =1; J <=n; J + +) -         { -             if(temp = =0) +                  Break; -             if(C[j] = =0) +             { ATemp%=A[j]; atlength++; -             } -         } -         if(temp==0) -Min =min (min, length); -         return 0; in     }
Here the use of the choice of 0, not selected as 1, convenient code debugging in accordance with the binary operation, more intuitive -C[M] =0;//used to mark which is selected toDFS (M +1); +C[M] =1;//Mark not selected -DFS (M +1); the } * intcmpConst void*a,Const void*b) $ {Panax Notoginseng return*(int*) B-* (int*) A; - } the intMain () + { A intnum; thescanf"%d", &num); + while(num--) - { $min=1000000; $scanf"%d%d", &n, &sum); - for(inti =1; I <= N; i++) -scanf"%d", &a[i]); theQsort (&a[1], N,sizeof(a[1]), CMP); -Dfs1);Wuyi if(min!=1000000) theprintf"%d\n", min); - Elseprintf"-1\n"); Wu } -}

Codeforce round#49 Untitled (Hdu 5339)

Related Article

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.