Acdream 1726 A Math game (binary find)

Source: Internet
Author: User

A Math GameTime limit:2000/1000ms (java/others) Memory limit:256000/128000kb (java/others) Submit statistic Next Problemproblem descriptionrecently, Losanto find an interesting Math game. The rule is Simple:tell you a number H, and you can choose some numbers from a set {a[1],a[2],......, a[n]}. If the sum of the number you choose is H, then you win. Losanto just want to know whether he can win the game. Inputthere is several cases.
In each case, there is numbers in the first line N(The size of the set) and H. The second line has n numbers {a[1],a[2],......, a[n]}. 0<n<=40, 0<=h<10^9, 0<=a[i]<10^9, all the numbers is integers. Outputif Losanto could win the game, output "Yes" in a line. Else output "No" in a line. Sample Input
10 872 3 4 5 7 9 10 11 12 1310 382 3 4 5 7 9 10 11 12 13
Sample Output
NoYes
MANAGERRIHKDDD Test Instructions: given n numbers, Q can you add these numbers to H? Analysis: Binary find, first half of the and calculated the existence of the array, then the second half of the number of ANS to calculate, and then in the first half of the search H-ans the problem with a new way of writing, bit operation. Binary is used to indicate which numbers are taken and which are not. For example, the first 5 numbers, the 2^5 binary is 100000 then you can put the previous five number of combinations of all of them to represent the number of each bits, such as taking the first and third numbers, then the binary is 100101.
#include <cstdio>#include<iostream>#include<cstring>#include<cmath>#include<stdlib.h>#include<vector>#include<queue>#include<stack>#include<algorithm>#defineLL Long Longusing namespacestd;Const intmaxn=2e6+7;intn,h;intA[MAXN],SUM[MAXN];intMain () {intn,h;  while(SCANF ("%d%d", &n,&h)! =EOF) {         for(intI=0; i<n;i++) scanf ("%d",&A[i]); intC=0,k=n>>1;  for(intI=0;i< (1&LT;&LT;K); i++) {LL s=0;  for(intj=0; j<k;j++)            {                if((1&LT;&LT;J) &i) s+=A[j]; }            if(s<=h) sum[c++]=s; } sort (Sum,sum+c); intres=n-K; BOOLok=false;  for(intI=0;i< (1<<res); i++) {LL s=0;  for(intj=0; j<res;j++)            {                if((1&LT;&LT;J) &i) s+=a[k+J]; }            if(S&GT;H)Continue; if(Sum[lower_bound (sum,sum+c,h-s)-sum]==h-s) {OK=true;  Break; }} puts (ok?"Yes":"No"); }    return 0;}
View Code

Acdream 1726 A Math game (binary find)

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.