HDU4876(深搜+剪枝+全排列)

来源:互联网 发布:网络问政的特点 编辑:程序博客网 时间:2024/06/06 09:29

ZCC loves cards

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


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 severalconsecutive 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, butonce 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 12 3 4 5
 

Sample Output
7
Hint
⊕ means xor
 

Author
镇海中学
 

Source
2014 Multi-University Training Contest 2 
#include<stdio.h>#include<string.h>#include<algorithm> #include<iostream>using namespace std;const int maxn=1000;int n,k,l;int r;int a[maxn],tmp[maxn],tes[maxn];bool vis[maxn];void cal(int s,int num){if(num==k+1)return;vis[s]=true;cal(s^tmp[num],num+1);cal(s,num+1);} bool check(){memset(vis,false,sizeof(vis));cal(0,0);for(int i=l;i<=r;i++)if(!vis[i])return false;return true;}void work(){if(!check())return;for(int i=0;i<k;i++){tes[i]=tmp[i];}do{memset(vis,false,sizeof(vis));for(int i=0;i<k;i++){int s=0;for(int j=i;j<i+k;j++){s^=tes[j%k];vis[s]=1;}for(int j=l;j<110;j++){if(!vis[j]){r=max(r,j-1);break;}}}}while(next_permutation(tes,tes+k));}void dfs(int b,int s){if(s==k)work();for(int i=b;i<n;i++){tmp[s]=a[i];dfs(i+1,s+1);}}int main(){while(~scanf("%d%d%d",&n,&k,&l)){for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); r=l-1;dfs(0,0);if(r<l)        printf("0\n");        else        printf("%d\n",r);}return 0;}