HDU 5339 Untitled

Source: Internet
Author: User

This question is obviously a check-in question, but I did not make the game, naked zero, really tragic ...

After looking at the main idea of the direct search on the line, just need to put them from the big to small sort after search, I did not think ... I don't want to say more.

At first I enumerate all the cases directly:

1#include <cstdio>2#include <cstring>3#include <vector>4#include <algorithm>5 using namespacestd;6 7 intb[ at],a;8 9InlineBOOLOkConstvector<int> &v,inta) {Ten     intLen =v.size (); One      for(inti = len-1; I >=0; --i) A         if(a% v[i] = =0)return 1; -         ElseA%=V[i]; -     return 0; the } -  - intMain () { -     intT,n; +scanf"%d",&t); -      while(t--) { +scanf"%d%d",&n,&a); A          for(inti =0; I < n; ++i) atscanf"%d", B +i); -Sort (b, B +n); -         intLimit = (1<< N)-1; -         BOOLFlag =0; -          for(inti =1; I <= limit; ++i) { -vector<int>v; in              for(intj =0; (1<< j) <= I; ++j) -                 if(I & (1<<j)) V.push_back (B[j]); to             if(ok (V, a)) { +printf"%d\n", V.size ()); -Flag =1; the                  Break; *             } $         }Panax Notoginseng         if(Flag = =0) puts ("-1"); -     } the     return 0; +}
View Code

But found 700+ms, surprisingly slow, so I try to direct deep search, unexpectedly 78ms, sure enough fast!

1#include <cstdio>2#include <cstring>3#include <vector>4#include <algorithm>5 using namespacestd;6 Const intINF =0x3fffffff;7 8 intb[ at],ans,n;9 Ten voidDfsintIdintNuminta) { One     if(A = =0) { AAns =min (ans, num); -         return ; -     } the     if(ID > N)return ; -  -DFS (ID +1, Num +1AB[id]); -DFS (ID +1, Num, a); + } -  +InlineBOOLcmpintXinty) { A     returnX >y; at } -  - intMain () { -     intT, A; -scanf"%d",&t); -      while(t--) { inscanf"%d%d",&n,&a); -          for(inti =1; I <= N; ++i) toscanf"%d", B +i); +Sort (b +1, B + n +1, CMP); -Ans =inf; theDfs1,0, a); *printf"%d\n", ans = = inf? -1: ans); $     }Panax Notoginseng     return 0; -}
View Code

On the internet to see a single pressure than my first version faster than:

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 int ConstMAX =1<< A;6 int ConstINF =0x3fffffff;7 intb[ -], Sta[max];8 9 intLowbit (intx)Ten { One     returnX & (-x); A } -  - BOOLcmpintAintb) the { -     returnA >b; - } -  + intMain () - { +     intT; Ascanf"%d", &T); at      while(T--) -     { -memset (STA,0,sizeof(STA)); -         intN, A; -scanf"%d%d", &n, &a); -          for(inti =1; I <= N; i++) inscanf"%d", &b[i]); -Sort (b +1, B + n +1, CMP); to          for(inti =1; I <= N; i++) +sta[1<< (I-1)] =B[i]; -         intCNT, ans =INF; the          for(inti =1; I < (1<< N); i++) *         { $             intTMP =A;Panax NotoginsengCNT =0; -              for(intj = i; J >0; J-=Lowbit (j)) the             { +TMP%=Sta[lowbit (j)]; ACNT + +; the             } +             if(TMP = =0) -Ans =min (ans, cnt); $         } $         if(ans = =INF) -printf"-1\n"); -         Else theprintf"%d\n", ans); -     }Wuyi}
View Code

Although it blew zero, the game still had to be played.

HDU 5339 Untitled

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.