您的位置:首页 > 其它

HDU 4876(就是暴)

2016-04-15 10:35 483 查看
题意:

   给定n(n<=20)个数,选出至多K(K<=6)个数构成一个环,把任意环上的连续区间的异或值记录下来,求从L(L<=100)开始的最长连续区间,使得该区间内所有的数都对应一个区间的异或值。

思路:

  怎么想都会超时,就用最暴力的方法了,剪纸 + 减少枚举,

  首先可以先将C(n,k)中可选的数的组合枚举出来,我们发现不需要枚举出所有的这k个数的排列依次判断,因为是环状 , 可以从K!降到(K -1)!

 然后跟据当前获取的最大值做可行性剪枝。 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define rep(i , n) for(int i =0 ; i<(int)n;i++)
#define rep1(i,x,y) for(int i =(int)x ;i<=(int)y;i++)

const int N = 25;
int n , K , L;
int a
, b
, ans = 0;
int vis[130];
bool judge(){
memset(vis,false ,sizeof(vis));
int lim = (1<<K);
for(int i = 1 ; i<lim ; i++){
int num = 0;
rep(j , K) if((i>>j)&1) num^=b[j];
vis[num] = true;
}
if(ans == 0) return vis[L] == true;
rep1(i , L , ans + 1) if(!vis[i]) return false;
return true;
}
void cal(){
memset(vis,false, sizeof(vis));
for(int m = 1 ; m <= K ; m++){
int now = 0 , lim = K + m - 1;
for(int j = 0 ; j<=lim;j++){
now^=b[j % K];
if(j >= m - 1) {
vis[now] = 1;
now^= b[j - m + 1];
}
}
}
for(int i = L ;;i++){
if(!vis[i]){
ans = max(ans , i - 1);
return ;
}
}
return ;
}
void dfs(int pos,int pos2){
if(pos == n){
if(judge()){
do{
cal();
} while(next_permutation(b + 1 , b + K));
}
return ;
}
if(pos2 < K){
b[pos2] = a[pos];
dfs(pos + 1, pos2 + 1);
}
if(n - pos -1 >= K - pos2)
dfs(pos + 1 , pos2);
}
int main()
{
while(scanf("%d %d %d",&n,&K,&L)==3){
rep(i ,n) scanf("%d",&a[i]);
sort(a , a + n);
ans = 0;
dfs(0 , 0);
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: