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. 一款炫酷Loading动画--载入失败

    简单介绍 上一篇文章一款炫酷Loading动画–载入成功.给大家介绍了成功动画的绘制过程,这篇文章将接着介绍载入失败特效的制作. 相比成功动画,有了前面的经验,失败动画的过程就显得比較简单了. 动画结 ...

  2. Oracle Temp 表空间切换

    一.TEMP表空间作用 暂时表空间主要用途是在数据库进行排序运算.管理索引.訪问视图等操作时提供暂时的运算空间,当运算完毕之后系统会自己主动清理.当 oracle 里须要用到 sort 的时候. PG ...

  3. Kafka无法消费!?究竟是bug的“沦陷”还是配置的“扭曲”?

    在一个月黑风高的夜晚,突然收到现网生产环境Kafka消息积压的告警,梦中惊醒啊,马上起来排查日志. 问题现象 消费请求卡死在查找Coordinator Coordinator为何物?Coordinat ...

  4. CentOS 7下安装Hadoop2.2

    这里就介绍CentOS的安装了,直接进入Hadoop2.2伪分布模式安装. 1.安装包下载 1.1.下载JDK1.7 眼下JDK的版本号是jdk1.8.0_25.这里下载的是jdk1.7.0_67. ...

  5. js实现伪音乐盒

    支持快进 <div class="music-part"> <div class="box-bg"></div> <d ...

  6. JS高级技巧学习小结

    安全类型检測 var isArray = value instanceof Array; 以上代码要返回true,value必须是一个数组,并且还必须与Array构造函数在同一个全局作用域中(Arra ...

  7. openstack instance resize

    Error: No valid host was found. No valid host found for resize

  8. Docker为什么刚运行就退出了

    引言 最近群里的好多新接触Docker的朋友,好多都遇到了相同的问题,使用 $ docker run -d ubuntu /bin/bash 运行了一个简单的容器后,然后docker ps -a 进行 ...

  9. vscode常用的快捷键

    对于编程人员来说,记住一些常用的快捷键能够提高工作效率:我认为,对于编程人员来说,掌握一些常用的快捷键是非常有必要的! Ctrl + Shift + N 打开新的编辑器窗口 Ctrl + Shift ...

  10. Horspool和BM算法解析

    最近算法中学到了Horspool,KMP,BM三种算法.接下来给大家做个分享. Horspool算法: 算法思路: 1.分为匹配串,原串 2.从右往左依次匹配: 一旦遇到不匹配的,原串相对于匹配串 移 ...