[Search] Hdu 4016 Magic Bitwise and operation

Source: Internet
Author: User
Tags cmath

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=4016

Magic Bitwise and operationTime limit:6000/3000 MS (java/others) Memory limit:65768/65768 K (java/others)
Total submission (s): 1315 Accepted Submission (s): 504


Problem descriptiongiven n Integers, your task is to pick K out of them so, the picked number was minimum when do BITW Ise "and" among all of them.
For example, there is three integers 5, 6 and 7. You is asked to pick, them. Your Possible strategy is (5, 6), (5, 7) or (6, 7). The values when does bitwise and all the picked numbers together is as follows:
5 and 6 = 4
5 and 7 = 5
6 and 7 = 6
The smallest one is 4.

Inputthere is multiple test cases for this problem. The first line of the input contains an integer denoting the number of the test cases.
For each test case, there is integers in the first line:n and K, denoting the number of given integers and the Numbe R of integers you is asked to pick out. N <= 40
The second line contains the n integers. You may assume this all integers is small than 2^60.

Notes:there is about one thousand randomly generated test cases. Fortunately 90% of them is relatively small.

Outputfor each test case, output only one integer is the smallest possible value.
Sample Input
33 25 6 78 2238 153 223 247 111 252 253 24740 101143632830316675007 558164877202423550 1152356080752164603 114391100678155 1605 11326550055017512631152919305583327167 1141662230660382702) 862439259920596463 1151777428397603327 1008771132016295871855666336963428351 1151795583225167807 1152634943314572791 1071856693060561407 11326508728034263031124211056982081471 1152917106425982911 1152815392070041535 1080863910568853481 2882303718563509751080720560532488126 864686455262281727 576460673919991167 574191342855241589 11522337600501186511152921504605798263 1152912708241186815 1079738008506187487 1075796261476483027 10808544788207308791152885219917823999 1151725162940854259 1147529498501577715 571956602920235519 11345456306436162481152921218991521790 1152921496000052703 1142788250826440703 1151654831778151421 1152780747522637695

Sample Output
Case #1:4Case #2:9Case #3:36028797086245424

Sourcethe 36th ACM/ICPC Asia Regional Shanghai Site--warmup
Recommendlcy | We have carefully selected several similar problems for you:4017

pid=4012 "target=" _blank "style=" Color:rgb (26,92,200); Text-decoration:none ">4012 4013 4014 4015


topic Meaning:

Find k in the n number, so that the K number and the value of the smallest.

Problem Solving Ideas:

Search + Pruning

Pruning One: if the current value and the last of all values want to be less than the value currently evaluated, return directly. Because with the computation is getting smaller.

pruning two: from small to large sort. The order of the searches has a very large impact on the time to get the best value.

Code:

#include <CSpreadSheet.h> #include <iostream> #include <cmath> #include <cstdio> #include <sstream> #include <cstdlib> #include <string> #include <string.h> #include <cstring># include<algorithm> #include <vector> #include <map> #include <set> #include <stack># include<list> #include <queue> #include <ctime> #include <bitset> #include <cmath># Define EPS 1e-6#define INF 0x3f3f3f3f#define PI acos ( -1.0) #define LL __int64#define ll long long#define Lson l,m, (RT&LT;&L t;1) #define Rson m+1,r, (rt<<1) |1#define m 1000000007//#pragma comment (linker, "/stack:1024000000,1024000000")    using namespace std; #define MAXN 45int n,k;ll sa[maxn],la[maxn];ll ans;void dfs (int cur,int hav,ll now) {if (hav==k)        {if (ans==-1) Ans=now;        else if (Now<ans) Ans=now;    return;    } if (cur>n) return;    if (ans!=-1&& (Now&la[cur]) >=ans) return; DFS (cUr+1,hav+1,now==-1? 

Sa[cur]:(now&sa[cur])); DFS (Cur+1,hav,now);} int main () {//freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout); int t,cnt=0; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&k); for (int i=1;i<=n;i++) scanf ("%i64d", &sa[i]); Sort (sa+1,sa+n+1); La[n]=sa[n]; for (int i=n-1;i>=1;i--) la[i]=la[i+1]&sa[i]; Ans=-1; DFS (1,0,-1); printf ("Case #%d:%i64d\n", ++cnt,ans); } return 0;}



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

[Search] Hdu 4016 Magic Bitwise and operation

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.