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;
}
给定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;
}
相关文章推荐
- PowerDesigner使用教程|使用方法
- 面向对象设计-OOD
- user、order、pay_log、pay_daybook表
- 初始HashMap
- 交互的可迭代的最近点(ICP)
- 王高利:Linux_shell三剑客之sed命令使用详解
- TortoiseGit安装配置
- 小马哥---高仿苹果6 主板型号z13刷机拆机主板图 6572芯片nand字库
- 【问题跟踪】KryoException: java.io.IOException: No space left on device
- Tomcat之内置数据源(DBCP)的配置(for JDBC)
- 自然语言处理之语言模型综述
- linux sshd 的使用
- 个人对webdriver的理解(十)
- The Primer of Classes
- 系统例程(Utility)及常用工具
- 个人对webdriver的理解(九)
- 使用storyboard中的AutoLayout进行适配问题
- VBA调用sqlserver带参数的存储过程
- AsyncTask+HttpClient网络请求图片实现自动轮播+小圆点
- 【ESRI论坛6周年征文】ArcEngine注记(Anno/ Label/Element等)处理专题 -入门篇