您的位置:首页 > 其它

hdu 4876 ZCC loves cards(暴搜+剪枝)

2014-07-27 10:05 495 查看
http://acm.hdu.edu.cn/showproblem.php?pid=4876

ZCC loves cards

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1683 Accepted Submission(s): 424



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, output the maximal number R. And if L can’t be obtained, output 0.

[align=left]Sample Input[/align]

4 3 1
2 3 4 5


[align=left]Sample Output[/align]

7

Hint ⊕ means xor


[align=left]Author[/align]
镇海中学

[align=left]Source[/align]
2014 Multi-University Training Contest 2

题意:给n个数,现在要从n个中选K个出来,并把它们排成一圈,现在可以从K个数中选连续的m个(1<=m<=k),将选中数异或起来就会得到一个数,给定一个L,问最大的R,使得一个圈中L,R中的所有数都出现过。
这个题暴力的复杂度为c(n,k)*k!*k*k,复杂度明显太高,由于数据是随机的,所以暴力+强剪枝应该能过。具体是在求得K个数的时候,先用2^k,判断一下在不排序的情况下能否更新答案,如果不能那么排序以后一定也不能,由于数据随机,所以可以剪掉很多情况。由于是一个环,所以(k-1)!就够了。敲代码的时候注意细节问题,尽量优化常数,这题就能过了。代码如下:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#define nn 110000
#define inff 0x3fffffff
using namespace std;
typedef long long LL;
int n,k,l;
int a[30];
int b[10];
int tem[10];
bool use[210];
int ans;
bool check()
{
int i,j,ix;
for(i=l;i<=l+k*(k-1)+1;i++)
use[i]=false;
for(i=0;i<(1<<k);i++)
{
ix=0;
for(j=0;j<k;j++)
{
if(i&(1<<j))
{
ix=ix^b[j];
}
}
use[ix]=true;
}
for(i=l;i<=l+ans-1;i++)
{
if(!use[i])
return false;
}
return true;
}
void solve()
{
int i,j,ix;
if(check())
{
for(i=0;i<k;i++)
tem[i]=b[i];
do
{
for(i=l;i<=l+k*(k-1)+1;i++)
use[i]=false;
for(i=0;i<k;i++)
{
ix=0;
for(j=i;j<i+k;j++)
{
ix=ix^tem[j%k];
use[ix]=true;
}
}
for(i=l;i<=l+k*(k-1)+1;i++)
{
if(!use[i])
{
ans=max(ans,i-l);
break;
}
}
}
while(next_permutation(tem+1,tem+k));
}
}
void dfs(int id,int num)
{
if(num==k)
{
solve();
return ;
}
if(id>=n||n-id<k-num)
return ;
dfs(id+1,num);
b[num]=a[id];
dfs(id+1,num+1);
}
int main()
{
int i;
while(scanf("%d%d%d",&n,&k,&l)!=EOF)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
ans=0;
dfs(0,0);
if(ans)
printf("%d\n",l+ans-1);
else
puts("0");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: