2014 multi-university training contest 2 - 1005( ZCC Loves cards)

http://acm.hdu.edu.cn/showproblem.php?pid=4876

ZCC loves cards

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


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
 
 
官方题解:
 
  
 
然后看了这位大牛清秀的代码, 感觉人明白了好多 = =.. http://blog.csdn.net/ck_boss/article/details/38129461
之前连递归枚举都不会实在是...
 
 
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;

#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++)
#define fer(i, x, n) for (int i = (int)(x), _n = (int)(n); i < _n; i++)

////////////////////////////////////////////////////////////////////////////////
int l,r,n,k;
int a[30];
int sv[30];
int b[30];
bool vis[300];
void dfs2(int num,int sum){
    vis[sum]=true;
    if(num==k) return;
    dfs2(num+1,sum^sv[num]);
    dfs2(num+1,sum);
}

bool test(){
    memset(vis,0,sizeof(vis));
    dfs2(0,0);
    int i;
    for(i=l;;i++){
        if(vis[i]==false) break;
    }
    if(i-1>r) return true;
    return false;
}

void solve(){
    if(!test()) return;
    rep(i,k) b[i]=sv[i];
    do{
        memset(vis,0,sizeof(vis));
        rep(i,k){
            int tmp = 0;
            rep(j,k){
                tmp ^= b[(i+j)%k];
                vis[tmp]=true;
            }
        }
        int i;
        for(i=l;;i++){
            if(vis[i]==false) break;
        }
        if(i-1>r) r=i-1;
        for(int i=l;i<=l+k*k;i++){
            if(vis[i]==false) break;
            r = max(r,i);
        }
    }while(next_permutation(b,b+k-1));
}

void dfs(int num,int id){
    if(num == k){
        solve();
        return;
    }
    fer(i,id,n){
        sv[num] = a[i];
        dfs(num+1,i+1);
    }
}

int main()
{
//  freopen("in.txt","r",stdin);
    ios_base::sync_with_stdio(0);
    while(scanf("%d%d%d",&n,&k,&l)!=EOF){
        r = l-1;
        rep(i,n) scanf("%d",a+i);
        sort(a,a+n);
        dfs(0,0);
        if(r<l) r=0;
        printf("%d\n",r );
    }
    return 0;
}

 

posted @ 2014-08-28 12:56  rewrite  阅读(161)  评论(0编辑  收藏  举报