HDU4876:ZCC loves cards

Posted llguanli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU4876:ZCC loves cards相关的知识,希望对你有一定的参考价值。

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
用全排列的方法来做。这我一開始还真没想到
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int n,k,l,r;
int vis[500],a[500],tem[500],s[500];

void set(int len,int sum)
{
    vis[sum] = 1;
    if(len == k)
        return ;
    set(len+1,sum^tem[len]);
    set(len+1,sum);
}

int check()
{
    memset(vis,0,sizeof(vis));
    set(0,0);
    for(int i = l; i<=r; i++)
        if(!vis[i])
            return 0;
    return 1;
}

void solve()
{
    if(!check()) return ;
    int i,j;
    for(i = 0; i<k; i++)
        s[i] = tem[i];
    do
    {
        memset(vis,0,sizeof(vis));
        for(i = 0; i<k; i++)
        {
            int ans = 0;
            for(j = i; j<k+i; j++)
            {
                ans^=s[(j%k)];
                vis[ans] = 1;
            }
        }
        for(i = l; i<=128; i++)//a[i]最大100,所以不会超过128
            if(!vis[i])
            {
                r = max(r,i-1);
                break;
            }

    }
    while(next_permutation(s+1,s+k));
}

void dfs(int now,int len)
{
    if(len == k)
    {
        solve();
        return ;
    }
    for(int i = now; i<n; i++)
    {
        tem[len] = a[i];
        dfs(i+1,len+1);
    }
}

int main()
{
    int i,j;
    while(~scanf("%d%d%d",&n,&k,&l))
    {
        for(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;
}


以上是关于HDU4876:ZCC loves cards的主要内容,如果未能解决你的问题,请参考以下文章

HDU 4879 ZCC loves march (并查集,set,map)

hdu5676 ztr loves lucky numbers DFS

hdu5676 ztr loves lucky numbers(dfs)

hdu 5656 CA Loves GCD

hdu 5655 CA Loves Stick

hdu-5655 CA Loves Stick(水题)