Lightoj 1235 Coin Change (IV) (binary enumeration)

Source: Internet
Author: User

  1. Say this is our school summer camp finished a problem, just see thought is to water problem, later found the complexity is too big, can not stand, the game finished also did not get out, then said this is binary enumeration. Then the touch of the study, and then wrote the word,
  2. The so-called binary enumeration is to calculate the first half, then half, and then use the two-point search to see if we can get this state, if it is possible,
  3. Test instructions: Give you two number n,k, the following gives you the number of N, indicating that you now have the face value of the coin, each coin face value of two, see if you can pay K
  4. Solution ideas: First thought that only three states, the direct Dfs is good, and later found that the complexity is too big, and then the dead is the above, the details of the test code
  5. Source code:
    #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int a[28];long long a1[20        005],a2[20005];int s,e;int t;int t1,t2;int n,k;void dfs (int x,long long v) {if (x==e) {if (e==n) a1[t1++]=v;        else if (E==N/2) a2[t2++]=v;    return; } for (int i=0; i<=2; i++) dfs (x+1,v+i*a[x]);}        int main () {while (scanf ("%d", &t) ==1) {int cases=1;            while (t--) {memset (a1,0,sizeof (A1));            Memset (a2,0,sizeof (A2));            scanf ("%d%d", &n,&k);            for (int i=0; i<n; i++) scanf ("%d", &a[i]);            t1=t2=0;            E=N/2;            DFS (0,0);            E=n;            DFS (n/2,0);            Sort (A2,A2+T2);            int j,l,mid,r;                for (j=0; j<t1; J + +) {long long h=k-a1[j];                l=0;                r=t2-1;                    while (l<=r) {mid = (l+r) >>1; if (a2[mid]==h)Break                    if (a2[mid]>h) r=mid-1;                else l=mid+1;            } if (l<=r) break;            } if (J&LT;T1) printf ("Case%d:yes\n", cases++);        else printf ("Case%d:no\n", cases++); }    }}

Lightoj 1235 Coin Change (IV) (binary enumeration)

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.