[Search] HDU 4016 magic bitwise AND OPERATION

Source: Internet
Author: User
Tags cmath

Question link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4016

Magic bitwise AND OPERATION Time 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 that the picked number are minimum when do bitwise "and" among all of them.
For example, there are three integers 5, 6 and 7. you are asked to pick two of them. your possible strategy is (5, 6), (5, 7) or (6, 7 ). the values when do bitwise and all the picked numbers together are as follows:
5 and 6 = 4
5 and 7 = 5
6 and 7 = 6
The smallest one is 4.

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

Notes: there are about one thousand randomly generated test cases. Fortunately 90% of them are 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 1143911006781551605 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 4012 4013 4014


Question meaning:

Find K in N to minimize the number of K and the value.

Solution:

Search + pruning

Pruning 1:If the current value and all the last values are less than the current value, return directly. Because the operation and operation are getting smaller and smaller.

Pruning 2:Sort data in ascending order. The search order has a great impact on the time when the optimal value is obtained.

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<<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;}


[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.