Acdream 1726 A Math game (binary enumeration +hash)

Source: Internet
Author: User

Topic Links:

http://acdream.info/problem?pid=1726

Test instructions

Given the number of N, and a number, judge whether K can be composed of any number of them.

Analysis:

Since n Max is 40, the brute force enumerates all the case complexity for 2^40 sure tle, and then comes to think of the binary enumeration

Divided into two halves, first to deal with the combination of the first N/2 number of cases, the results hash, and then enumerate the second half

And then find it in the hash table. Time complexity of O (2^ (N/2));

The code is as follows:

#include <stdio.h> #include <iostream> #include <cstdio> #include <cstring> #include < algorithm> #define PB push_back#define MP make_pair#define REP (i,n) for (int i=0;i< (n), ++i) #define for (I,L,H) for ( int i= (l); i<= (h); ++i) #define DWN (i,h,l) for (int i= (h); i>= (l);-i) #define CLR (Vis) memset (vis,0,sizeof (VIS)) # Define MST (Vis,pos) memset (vis,pos,sizeof (VIS)) #define MAX3 (A,B,C) max (A,max (b,c)) #define MAX4 (A,B,C,D) max (max (b) , Max (c,d)) #define MIN3 (a,b,c) min (a,min (b,c)) #define MIN4 (a,b,c,d) min (min (A, b), Min (c,d)) #define PI ACOs (-1.0) # Define INF 1000000000#define linf 1000000000000000000ll#define eps 1e-8#define LL long longusing namespace Std;const int M    axn=1<<20;const int hash = 1000007;struct hashmap//build hash Table {LL A[MAXN];    int head[hash],next[maxn],size;        void init () {//Initialize MST (head,-1);    size=0;        } bool Find (LL val) {//Find whether an element is in the hash table int tmp = (Val%hash + hash)%hash;           for (int i = head[tmp];i!=-1;i=next[i]) if (Val==a[i]) return true;    return false;        } void Add (LL val) {//Add element to hash table in int tmp = (val%hash+hash)%hash;        if (Find (Val)) return;        A[size]=val;        NEXT[SIZE]=HEAD[TMP];    head[tmp]=size++;        }}h1;int N,m,num[55];int Main () {while (~scanf ("%d%d", &n,&m)) {h1.init ();        REP (i,n) scanf ("%d", num+i);        int T=N/2;            REP (i,1<<t) {LL sum=0;            REP (j,t) {if (i& (1&LT;&LT;J)) sum+=num[j];            } if (sum>m) continue;        H1.add (sum);        } int tt=n-t;        int flag=0;            REP (I,1&LT;&LT;TT) {LL sum=0;            REP (J,TT) {if (i& (1&LT;&LT;J)) sum+=num[t+j];            } if (sum>m) continue;                if (H1.find (m-sum)) {flag=1;            Break        }} if (flag) printf ("yes\n"); else printf ("no\n"); } return 0;}


Acdream 1726 A Math game (binary enumeration +hash)

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.