hdu 4876 ZCC loves cards

ZCC loves cards

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1929    Accepted Submission(s): 503


Problem Description
ZCC loves playing cards. He has n magical cards and each has a number on it. He wants to choose k cards and place them around in any order to form a circle. He can choose any several consecutive cards the number of which is m(1<=m<=k) to play a magic. The magic is simple that ZCC can get a number x=a1⊕a2...⊕am, which ai means the number on the ith card he chooses. He can play the magic infinite times, but once he begin to play the magic, he can’t change anything in the card circle including the order.
ZCC has a lucky number L. ZCC want to obtain the number L~R by using one card circle. And if he can get other numbers which aren’t in the range [L,R], it doesn’t matter. Help him to find the maximal R.
 

Input
The input contains several test cases.The first line in each case contains three integers n, k and L(k≤n≤20,1≤k≤6,1≤L≤100). The next line contains n numbers means the numbers on the n cards. The ith number a[i] satisfies 1≤a[i]≤100.
You can assume that all the test case generated randomly.
 

Output
For each test case, output the maximal number R. And if L can’t be obtained, output 0.
 

Sample Input
4 3 1 2 3 4 5
 

Sample Output
7
Hint
⊕ means xor
 

Author
鎮海中學
 

Source
 

暴力+剪枝

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
//#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF 1e9
#define MAXN 21
#define maxn 30
#define mod 1000000007
#define eps 1e-7
#define pi 3.1415926535897932384626433
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define scan(n) scanf("%d",&n)
#define scanll(n) scanf("%I64d",&n)
#define scan2(n,m) scanf("%d%d",&n,&m)
#define scans(s) scanf("%s",s);
#define ini(a) memset(a,0,sizeof(a))
#define out(n) printf("%d\n",n)
ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int l,r,k,n;
int a[maxn], tmp[maxn], save[maxn];
int vis[200];
void calmax(int cur,int sum) // 2 ^ k = 64枚舉
{
	vis[sum] = 1;
	if(cur == k) return ;
	calmax(cur + 1,sum ^ save[cur]);
	calmax(cur + 1,sum);
}
bool check()
{
	ini(vis);
	calmax(0,0);
	for(int i = l;i <= r; i++) //當隨意取也達不到當前r時,說明這組選法不滿足,不用進入全排列
	{
		if(!vis[i]) return false;
	}
	return true;
}
void solve()
{
	if(!check()) return;
	rep(i,k) tmp[i] = save[i];
	do //全排列計算,一次複雜度A(5,5) * (6 * 6 + 128)
	{
		ini(vis);
		for(int i = 0;i < k; i++)
		{
			int ans = 0;
			for(int j = i; j < k + i; j++)
			{
				ans ^= tmp[j % k];
				vis[ans] = 1;
			}
		}
		for(int i = l;i <= 128; i++)
		{
			if(!vis[i])
			{
				r = max(r,i-1);
				break;
			}
		}
	}while(next_permutation(tmp + 1, tmp + k));
}
void dfs(int cur,int num) //C(20,6)取數
{
	if(num == k)
	{
		solve();
		return;
	}
	for(int i = cur;i < n; i++)
	{
		save[num] = a[i];
		dfs(i + 1,num + 1);
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
#endif  
	while(~scanf("%d%d%d",&n,&k,&l))
	{
		rep(i,n) scan(a[i]);
		sort(a,a+n);
		r = l - 1;
		dfs(0, 0);
		if(r < l) puts("0");
		else out(r);
	}
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章