ZCC loves cards

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

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
 
Recommend
We have carefully selected several similar problems for you:  4882 4881 4880 4879 4878 
 
搜索
首先找到k个元素后,枚举这k个元素的所有子集,看能不能超过当前得到的最大的R,如果不能,就没有必要在检查
我们容易发现 假如要选出3个元素则编号为1 2 3   2 3 1   3 1 2 这三种排列在检查时是一样的因此枚举全排列时只需要枚举全排列的1 / k即可
最后一个剪枝是 把a数组事先按从大到小排列,若枚举的第一个元素的将所有位置1都无法超过当前最大值,则没有必要从这个元素枚举下去
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue> using namespace std; #define read() freopen("sw.in", "r", stdin) const int MAX_N = ;
const int MAX = ( << );
int N, K, L;
struct node {
int st[];
bool operator < (const node &rhs) const {
for (int i = ; i < K; ++i) {
if (st[i] != rhs.st[i]) return st[i] < rhs.st[i];
}
return ;
}
}; int x[MAX_N];
bool vis[MAX];
int ele[MAX_N];
int _max;
int len = ;
node p[]; int cal(int n) {
int ret = ;
if (n == ) ret = ;
while (n > ) {
++ret;
n >>= ;
} return ( << ret) - ;
} void check() {
int t = , _max1 = ;
/*for (int i = 0; i < K; ++i) printf("%d ", ele[i]);
printf("\n");*/
memset(vis, , sizeof(vis));
int cnt = ;
for (int s = ; s < ( << K); ++s) {
t = ;
for (int i = ; i < K; ++i) {
if (s >> i & ) {
t ^= ele[i];
}
}
if (!vis[t]) {
vis[t] = ;
if (t >= L && t <= _max + ) ++cnt;
} _max1 = max(_max1, t);
} if (_max1 <= _max || cnt < (_max + - L)) return; for (int i = ; i < len; ++i) {
memset(vis, , sizeof(vis));
cnt = ;
for (int start = ; start < K; ++start) {
int v;
for (int l = ; l <= K; ++l) {
v = ;
for (int pos = start, num = ; num <= l; pos = (pos + ) % K, ++num)
v ^= ele[ p[i].st[pos] ];
if (!vis[ v ]) {
vis[v] = ;
if (v >= L && v <= _max + ) ++cnt;
}
} }
int k = _max + ;
if (cnt < (_max + - L)) continue;
while (vis[k] == ) ++k;
_max = max(_max, k - );
} } void dfs(int id, int num) {
if (num >= K) {
//printf("fuck\n");
check();
return ;
}
if (id >= N) return ; if (num == && cal(x[id]) <= _max) return;
ele[num] = x[id];
dfs(id + , num + );
dfs(id + , num);
} int main()
{
//read();
while (scanf("%d%d%d", &N, &K, &L) != EOF) {
for (int i = ; i < N; ++i) scanf("%d", &x[i]);
set <node> Set;
len = ;
int id[] = {, , , , , };
sort(x, x + N, greater<int>());
_max = L - ; do {
node st;
for (int i = ; i < K; ++i) st.st[i] = id[i];
if (Set.find(st) != Set.end()) continue;
// for (int i = 0; i < K; ++i) printf("%d ", id[i]);
//printf("\n");
for (int i = ; i < K; ++i) p[len].st[i] = id[i];
len++;
for (int i = ; i < K; ++i) {
for (int m = , j = i; m < K; ++m, j = (j + ) % K) {
st.st[m] = id[j];
}
Set.insert(st);
} }while (next_permutation(id, id + K)); dfs(, );
printf("%d\n", _max < L ? : _max); }
//cout << "Hello world!" << endl;
return ;
}
 

hdu 4876的更多相关文章

  1. HDU 4876 ZCC loves cards(暴力剪枝)

    HDU 4876 ZCC loves cards 题目链接 题意:给定一些卡片,每一个卡片上有数字,如今选k个卡片,绕成一个环,每次能够再这个环上连续选1 - k张卡片,得到他们的异或和的数,给定一个 ...

  2. hdu 4876 ZCC loves cards(暴力)

    题目链接:hdu 4876 ZCC loves cards 题目大意:给出n,k,l,表示有n张牌,每张牌有值.选取当中k张排列成圈,然后在该圈上进行游戏,每次选取m(1≤m≤k)张连续的牌,取牌上值 ...

  3. hdu 4876(剪枝+暴力)

    题意:给定n,k,l,接下来给出n个数,让你从n个数中选取k个数围成一圈,然后从这k个数中随意选出连续的m(m>=1&&m<=k)个数进行异或后得到[l,r]区间的所有值, ...

  4. HDU 4876 ZCC loves cards _(:зゝ∠)_ 随机输出保平安

    GG,,,g艹 #include <cstdio> #include <iostream> #include <algorithm> #include <st ...

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. ios 仿android gallery控件

    ios 上没有发现与android gallery类似的控件,因为在项目上须要使用到.採用UICollectionView实现 watermark/2/text/aHR0cDovL2Jsb2cuY3N ...

  2. jQuery事件传播,事件流

    一. jQuery事件传播 在DOM2级事件模型中,一旦事件被触发.事件流首先从DOM树顶部(文档节点)向下传播.直到目标节点.然后再从目标节点向上传播到DOM树顶.从上到下的过程被称为捕获阶段.从下 ...

  3. 【OI】同余方程

    一.同余方程的判定 我们知道同余方程是形如 ax ≡ b (mod n)   的东西,用文字表达就是: ax和b除以n的余数相同 那么,经过如下推理:(用=代替恒等于) ax=b (mod n) ax ...

  4. oc77--结构体,NSNumber,NSValue,NSDate,NSCalendar

    // // main.m // OC中的常用结构体 // #import <Foundation/Foundation.h> int main(int argc, const char * ...

  5. linux 网络编程-基础篇01

    #Socket简介 是一个编程接口是一种特殊的文件描述符(everything in Unix is a file)并不仅限于TCPIP协议面向连接(Transmission Control Prot ...

  6. openstack instance resize Resize error: Unable to resize disk down

  7. putty+Xmanager登陆Linux,实现图形界面操作.

  8. [Swift通天遁地]四、网络和线程-(10)处理图片:压缩、缩放、圆角、CoreImage滤镜、缓存

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  9. JavaScript中对象转换为原始值的规则

    JavaScript中对象转换为原始值遵循哪些原则? P52 对象到布尔值对象到布尔值的转换非常简单:所有的对象(包括数字和函数)都转换为true.对于包装对象亦是如此:new Boolean(fal ...

  10. android sqlite中判断某个表是否存在

    <span style="font-size:18px;">sqlite 中判断某个表是否存在的方法,贴出来供大家参考 /** * 判断某张表是否存在 * @param ...