ZCC loves cards
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1868 Accepted Submission(s): 483
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.
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.
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
7Hint⊕ means xor
Author
镇海中学
已被中学生虐惨。
该题正解是爆搜,如果预处理出状态的化会比直接爆搜慢很多,而且剪枝的关键在于先用2的n次方不考虑顺序,如果这种情况下都比前面最小值小,则这种情况不搜索。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[400004],b[55],c[55],d[55];
bool dp[129];
int tot;
int s[7];
void solve(int n,int k,int sum){
dp[sum]=1;
if(n==k) return ;
solve(n+1,k,sum);
solve(n+1,k,sum^b[c[n]]);
}
int pp;
bool vis[8];
void quanpai(int n,int k,int l){
if(n==k){
int i,j;
memset(dp,0,sizeof(dp));
for(i=0;i<k;i++){
if(i==0) s[i]=b[c[d[i]]];
else{
s[i]=s[i-1]^b[c[d[i]]];
}
}
s[k]=0;
for(i=1;i<k;i++){
for(j=0;j<i;j++){
dp[s[i]]=1;
dp[s[j]]=1;
dp[s[k-1]^s[i]]=1;
dp[s[j]^s[i]]=1;
dp[s[k-1]^s[i]^s[j]]=1;
}
}
for(i=l;i<128;i++){
if(!dp[i]) break;
}
if(pp<i-1) pp=i-1;
return ;
}
int i;
for(i=0;i<k;i++){
if(!vis[i]){
vis[i]=1;
d[n]=i;
quanpai(n+1,k,l);
vis[i]=0;
}
}
}
void dfs(int n,int k,int l,int pos,int ss){
int i,j;
if(n==k){
memset(dp,0,sizeof(dp));
solve(0,k,0);
for(i=l;i<=pp;i++){
if(!dp[i]) return ;
}
memset(vis,0,sizeof(vis));
quanpai(0,k,l);
return ;
}
for(i=pos+1;i<=ss;i++){
c[n]=i;
dfs(n+1,k,l,i,ss);
}
return ;
}
int main()
{
int i,n,k,l;
while(scanf("%d%d%d",&n,&k,&l)!=EOF){
for(i=1;i<=n;i++) scanf("%d",&b[i]);
sort(b+1,b+1+n);
if(k>n) k=n ;
pp=l-1;
dfs(0,k,l,0,n);
if(pp<l) printf("0\n");
else printf("%d\n",pp);
}
}