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<<K); i++) {LL s=0; for(intj=0; j<k;j++) { if((1<<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<<J) &i) s+=a[k+J]; } if(S>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)