Test instructions: Give a number A and n number B1,b2,..., bn.
Select some number from N to rearrange into c1,c2,..., cm makes a%c1%c2%...%cm=0.
If you can select the output requires a minimum of several numbers, otherwise output-1.
Analysis:
First of all, it's important to "be sure to divide the big number and then the small number."
Because if you divide the small number x first, the number of modulo must be in the 0 to X-1, and then the number greater than X is meaningless.
The selected number of columns is a non-incrementing sequence.
With a maximum of 20 N, a simple 2^n search can be done.
Found a bug in Hdu OJ through this question
Is the sort of time can not be handwritten integer CMP make it in reverse order, otherwise g++ sentenced Re,c++ WA.
There are two ways of writing, in fact, the same way, the processing method has a little difference:
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cstdlib>6#include <cmath>7#include <vector>8#include <queue>9#include <map>Ten#include <Set> One #defineEPS 10e-13 A - using namespacestd; - thetypedefLong Longll; - - intb[ -],v[ -],n,a,c,ans; - BOOLvis[ -]; + - voidDfsintcur) + { A if(cur==-1) at { - intt =A; - for(intI=0; i<c;i++) -t%=V[i]; - if(t==0) ans =min (ans,c); - return; in } -Vis[cur] =true; toV[c++] =B[cur]; +DFS (cur-1); -Vis[cur] =false; thec--; *DFS (cur-1); $ }Panax Notoginseng - intMain () the { + intT; Ascanf"%d",&T); the while(t--) + { -scanf"%d%d",&n,&a); $ for(intI=0; i<n;i++) $scanf"%d",&b[i]); -Sort (b,b+n); -c =0; thememset (Vis,0,sizeof(Vis)); -Ans = -;WuyiDFS (N-1); theprintf"%d\n", ans== -?-1: ans); - } Wu return 0; -}
View Code
More Concise:
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cstdlib>6#include <cmath>7#include <vector>8#include <queue>9#include <map>Ten#include <Set> One #defineEPS 10e-13 A - using namespacestd; - thetypedefLong Longll; - - intb[ -],v[ -],n,a,c,ans; - + voidDfsintCurintXintlen) - { + if(x==0) ans =min (ans,len); A if(cur==-1)return; atDFS (cur-1, X,len); -DFS (cur-1, x%b[cur],len+1); - } - - intMain () - { in intT; -scanf"%d",&T); to while(t--) + { -scanf"%d%d",&n,&a); the for(intI=0; i<n;i++) *scanf"%d",&b[i]); $Sort (b,b+n);Panax Notoginsengc =0; -Ans = -; theDFS (N-1A0); + if(ans< -) printf ("%d\n", ans); A Elseprintf"-1\n"); the } + return 0; -}
View Code
Hdu 5339 Untitled (mathematical thinking)