HDU4876ZCC loves cards(多校题)

HDU4876ZCC loves cards(多校题)

大家好,又见面了,我是全栈君。

ZCC loves cards

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

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

题意:给n个数。从中选出k个数,这k个数能够随意排列,一旦定了顺序就不能改变,在这个确定的顺序。选出m(m<=k)个数异或得到的值x,在这个顺序得到的全部x的值中找出一个最大值R,这些数中使得存在一个连续的范围L~R。

#include<stdio.h>
#include<string.h>
int n,k,L,ans[25];
int a[13],aa[13],R,flag[150];
int vist[10];
void find(int tk)
{
    if(tk==k-1)
    {
        memset(flag,0,sizeof(flag));
        for(int i=0;i<k-1;i++)
        a[i+k]=a[i];
        int maxa=0;
        for(int i=0;i<k;i++)//枚举一个确定序列的连续片断的异或值
        {
            int x=a[i]; flag[x]=1; if(maxa<x)maxa=x;
            for(int j=i+1;j-i+1<=k;j++)
            {
                x^=a[j]; flag[x]=1;if(maxa<x)maxa=x;
            }
        }
        int r=0;
        for(int i=L;i<=maxa;i++)//找出这个最大值R,使得这些数存在L~R范围的数都存在。
        if(flag[i]==0)break;
        else r=i;
        if(r>R)R=r;
        return ;
    }
    tk++;
    for(int i=0;i<k;i++)
    if(vist[i]==0)
    {
        a[tk]=aa[i]; vist[i]=1; find(tk); vist[i]=0;
    }
}
bool panduan()//放宽条件(随意顺序)推断
{
    memset(flag,0,sizeof(flag));
    int maxa=0;
    for(int i=1;i<(1<<k);i++)
    {
        int x=0;
        for(int j=0;(1<<j)<=i;j++)
        if((1<<j)&i)x^=aa[j];
        flag[x]=1;
        if(maxa<x)maxa=x;
    }
    int r=0;
    for(int i=L;i<=maxa;i++)
    if(flag[i]==0)break;
    else r=i;
    return r>R;
}
void CNM(int tk,int i)
{
    if(tk==k-1)
    {
        if(panduan())
        find(-1);
        return ;
    }
    tk++;
    for(int j=i+1;j<n;j++)
    {
        aa[tk]=ans[j]; CNM(tk,j);
    }
}
int main()
{
    while(scanf("%d%d%d",&n,&k,&L)>0)
    {
        R=0; memset(vist,0,sizeof(vist));
        for(int i=0;i<n;i++)
        scanf("%d",&ans[i]);
        CNM(-1,-1);
        printf("%d\n",R);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116456.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • eval在python中是什么意思_如何在Python中使用eval ?

    eval在python中是什么意思_如何在Python中使用eval ?Python中的eval是什么?在Python中,我们有许多内置方法,这些方法对于使Python成为所有人的便捷语言至关重要,而eval是其中一种。eval函数的语法如下:eval(expression,globals,locals)如上所示,eval函数采用三个参数:expression–需要一个字符串,该字符串将被解析并评估为Python表达式globals(可选)–一个字典,用于指定…

    2025年6月10日
    0
  • Git输出格式——Git placeholders

    Git输出格式——Git placeholders

    2021年9月1日
    54
  • 范冰:增长黑客入门训练营

    范冰:增长黑客入门训练营之前刚入门产品的时候,增长的概念已经很流行了,连着读了SeanEllis的《增长黑客:如何低成本实现爆发式成长》和范冰的《增长黑客:创业公司的用户与收入增长秘籍》以及相应的公开课,如果你不知道SeanEllis,那我觉得你应该认真花点时间去了解一下这位“增长黑客之父”了,之前已经分享过SeanEllis的公开课和关于这本书的读书笔记,比较开心的是无意中发现2019年《增长黑客:创业公司的用户与收入增长秘籍》的作者范冰就已经亲自开了这本增长黑客的课程,还是觉得好物不容错过!欢迎要资源,欢迎交流沟通过~

    2022年5月11日
    71
  • redis配置文件密码_windows查看redis版本

    redis配置文件密码_windows查看redis版本此设置并未成功,待完善1、启动redis服务,双击redis-server.exe或者在redis文件夹下运行redis-server.exeredis.windows.conf2、在redis文件夹下打开命令窗口,刚开始连接服务,因为初始没有密码,所以无需输入,即可连接服务,窗口中输入redis-cli.exe-h127.0.0.1-p6379//无需添加…

    2022年9月5日
    2
  • javastream流详解_Java获取文件流的所有方式

    javastream流详解_Java获取文件流的所有方式一、Stream流引入Lambda表达式,基于Lambda所带来的函数式编程,又引入了一个全新的Stream概念,用于解决集合类库既有的鼻端。(Lambda表达式详解在上篇博客内容)现有一个需求:将list集合中姓张的元素过滤到一个新的集合中然后将过滤出来的姓张的元素中,再过滤出来长度为3的元素,存储到一个新的集合中1.用常规方法解决需求/…

    2022年10月6日
    0
  • 达芬奇五年沉浮

    达芬奇五年沉浮达芬奇五年沉浮

    2022年5月18日
    56

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号