ZCC loves cards
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 670 Accepted Submission(s): 130
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.
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.
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
7Hint⊕ means xor
Author
镇海中学
Source
解题报告:比赛时不知道怎么搞,赛后看了官方思想,WA+TLE N次后1.8s过了。然后就是这种优化……问了队友,看了标程,综合在一起之后非常快了。
思路官方说的很清楚了。首先在n个数里枚举k个数出来,复杂度C( n, k )。先用2^k的复杂度检查任意组合这k个数,产生的异或值能不能达到当前的最大值。如果达不到,那么这一组就不用试了;如果达到,那么我们就对每个排列暴力,更新最大值。
实现的时候还是有很多精细的地方,虽然我很水但是我还是说下:
1. 第一次检查时,我们可以检查从L到当前最大值是否都被覆盖了。(标称内学习)
2. 展开所有排列时,每次用一个新的值去标记,比清空vis数组,再标记要快。(标称内学习)
3. 排列中是有重复的,比如1 2 3 4 5 6和2 3 4 5 6 1,以及1 2 3 4 5 6和6 5 4 3 2 1。可以预处理所有的排列,k为6时其实只有60种排列(队友的方法)
以上的几种方法结合在一起能已经能达到非常好的效果了,我的代码跑93ms。而且展开检查时我把所有的情况都处理成k=6的情况,其实改下应该会更快一点。
贴个代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
freopen("in.txt", "r", stdin);
#endif // ACM
work();
}
/***************************************************/
int permutation[60][12];
void init()
{
// 构造排列,去除循环重复和前后重复
// 比如 1 2 3 4 5 6 和 2 3 4 5 6 1
// 比如 1 2 3 4 5 6 和 6 5 4 3 2 1
string order = "123456";
set<string> ss;
do{
bool flag = true;
ff(i, 6)
{
string tmp = order.substr(i) + order.substr(0, i);
if(ss.count(tmp))
{
flag = false;
break;
}
}
string rev = order;
reverse(rev.begin(), rev.end());
ff(i, 6)
{
string tmp = rev.substr(i) + rev.substr(0, i);
if(ss.count(tmp))
{
flag = false;
break;
}
}
if(flag) ss.insert(order);
} while(next_permutation(order.begin(), order.end()));
int tot = 0;
for(set<string>::iterator it = ss.begin(); it!=ss.end(); it++, tot++)
{
ff(j, 6)
{
permutation[tot][j+6] = permutation[tot][j] = (*it).at(j)-'1';
}
}
}
int n, k, l, r, id;
int num[22];
int sav[7];
int vis[129];
int pos[256];
// 2^k复杂度,检查任意异或值能否构造出从l到当前最大r的所有数
bool check()
{
int tot = 0;
pos[tot++] = 0;
ff(i, k)
{
int t = tot;
ff(j, tot) pos[t++] = pos[j] ^ sav[i];
tot = t;
}
// 检查id,不清空数组,加速
++id;
ff(i, tot) vis[pos[i]] = id;
fff(i, l, r+1) if(vis[i] != id)
return false;
return true;
}
void dfs(int sta, int has)
{
if(has == k)
{
if(check() == false) return;
fff(i, k, 6) sav[i] = 0;
ff(p, 60)
{
++id;
ff(i, 6)
{
int tmp = 0;
ff(j, 6) tmp ^= sav[permutation[p][i+j]], vis[tmp] = id;
}
if(vis[l] != id) continue;
int ll = l;
while(vis[ll+1] == id) ll++;
r = max(r, ll);
}
return;
}
fff(i, sta, n-1)
{
sav[has] = num[i];
dfs(i+1, has+1);
}
}
void work()
{
init();
while(~scanf("%d%d%d", &n, &k, &l))
{
ff(i, n) scanf("%d", num+i);
r = 0;
dfs(0, 0);
printf("%d\n", r);
}
}