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. LeetCode 21. Merge Two Sorted Lists (合并两个有序链表)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  2. leaning website

      -->SoapUI http://soapui-tutorial.com/home/login/SOAPUI    Module 13 - 1, 2, 3, 4, 5, 6 Module 1 ...

  3. HDU 5858Hard problem

    Hard problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  4. MODULE_DEVICE_TABLE的理解【转】

    本文转载自:http://blog.csdn.net/sidely/article/details/39666471 在Linux IIC驱动中看到一段代码: static struct platfo ...

  5. java的PrintStream(打印输出流)详解(java_io)

    java的PrintStream(打印输出流)详解(java_io) 本章介绍PrintStream以及 它与DataOutputStream的区别.我们先对PrintStream有个大致认识,然后再 ...

  6. Tyvj 1068 巧用扩展KMP

    Tyvj1068 给定两个长度为2*10^5的字符串为A和B 求B在A中匹配后,任意匹配长度的位置个数. KMP算法大家应该烂熟于心才好,这样碰到这样的题才能灵活运用.有时做题真的需要一点灵感.首先, ...

  7. 04、抽取BaseActivity

    // 在使用SDK各组件之前初始化context信息,传入ApplicationContext // 注意该方法要再setContentView方法之前实现 // SDKInitializer.ini ...

  8. PCB 第一个node.js应用

    一.Node安装地址: https://nodejs.org/zh-cn/ 二.Node第一个应用实例 启动webnode.js应用: node webnode.js webnode.js代码: va ...

  9. [Swift通天遁地]四、网络和线程-(5)解析网络请求数据:String(字符串)、Data(二进制数据)和JSON数据

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

  10. [Swift通天遁地]七、数据与安全-(5)高效操作SQLite数据库

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