vijos1904 學姐的幸運數字

本文版權歸ljh2000和博客園共有,歡迎轉載,但須保留此聲明,並給出原文連接,謝謝合做。ios

 

 

本文做者:ljh2000
做者博客:http://www.cnblogs.com/ljh2000-jump/
轉載請註明出處,侵權必究,保留最終解釋權!spa

 

題目連接:vijos1904blog

正解:搜索get

解題報告:博客

  考慮&操做而言,與一個數的&,至少會致使$1$的個數減半,由於一個數和它的非的$1$顯然分佈在不一樣位置,而我能夠選擇把它和它的非操做的結果取個$min$,容易發現,由於有$64$位,最多$7$次操做就能使得答案爲$0$.string

  因此其實$n>=7$的話就不用作了,那麼只有$n<=6$的狀況,就爆搜就行了。it

 

//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦關終屬楚;苦心人,天不負,臥薪嚐膽,三千越甲可吞吳。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <ctime>
#define lc root<<1
#define rc root<<1|1
#define reg(i,x) for(int i=first[x];i;i=nxt[i])
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MAXN = 150;
int n;
ULL a[MAXN],ans;

inline ULL getint(){
    ULL w=0; char c=getchar(); while(c<'0'||c>'9') c=getchar();
	while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return w;
}

inline void dfs(int x,ULL val){
	if(x==n+1) {
		if(val<ans) ans=val;
		return ;	
	}
	dfs(x+1,val^a[x]);	dfs(x+1,val^(~a[x]));
	dfs(x+1,val&a[x]);	dfs(x+1,val&(~a[x]));
	dfs(x+1,val|a[x]);	dfs(x+1,val|(~a[x]));
}

inline void work(){
	int T=getint(),Case=0;
	while(T--) {
		n=getint(); for(int i=1;i<=n;i++) a[i]=getint();
		if(n>=7) { ans=0; }
		else {
			ans=0; ans=~ans;
			dfs(2,a[1]);
			dfs(2,~a[1]);//!!!
		}
		printf("Case #%d: %llu\n",++Case,ans);
	}
}

int main()
{
    work();
    return 0;
}
//有志者,事竟成,破釜沉舟,百二秦關終屬楚;苦心人,天不負,臥薪嚐膽,三千越甲可吞吳。
相關文章
相關標籤/搜索