http://www.ifrog.cc/acm/problem/1028

题解处:http://www.ifrog.cc/acm/solution/4

#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5 + ;
const double eps = 1e-;
const double INF = 1e12;
int ch[N*][],cnt[N*],tot,a[N],n,T,op,kase;
bool dp[(<<)+];
inline int newnode(){
++tot;
ch[tot][]=ch[tot][]=-;
cnt[tot]=;
return tot;
}
void insert(int x){
int ptr = ;
for(int i=;i>=;--i){
int nx = (x&(<<i))?:;
if(ch[ptr][nx]==-)
ch[ptr][nx]=newnode();
ptr = ch[ptr][nx];++cnt[ptr];
}
}
void Union(int &x,int y){
if(y==-)return;
if(x==-)x = newnode();
cnt[x]+=cnt[y];
Union(ch[x][],ch[y][]);
Union(ch[x][],ch[y][]);
}
int ask_xor(int x){
int ret=,ptr=;
for(int i=;i>=;--i){
int nx = (x&(<<i))?:;
if(ch[ptr][nx^]!=-){
ret|=(<<i);
ptr=ch[ptr][nx^];
}
else ptr=ch[ptr][nx];
if(ptr==-)break;
}
return ret;
}
int ask_and(){
int ret=,ptr=;
for(int i=;i>=;--i){
if(ch[ptr][]!=-&&cnt[ch[ptr][]]>=)
ret|=(<<i);
else Union(ch[ptr][],ch[ptr][]);
ptr=ch[ptr][];
}
return ret;
}
void ask_or(){
memset(dp,false,sizeof(dp));
for(int i=;i<=n;++i)dp[a[i]]=true;
for(int i=(<<);i>=;--i){
for(int j=;j<=;++j)
if(!(i&(<<j)))dp[i]|=dp[i|(<<j)];
}
int ret=,p=;
int tmp[];
for(int i=;i<=n;++i){
p=;
for(int j=;j>=;--j)
if(!(a[i]&(<<j)))tmp[++p]=(<<j);
int x=;
for(int j=;j<=p;++j){
if(dp[x|tmp[j]])x|=tmp[j];
}
ret=max(ret,x|a[i]);
}
printf("%d\n",ret);
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&op);
tot=-;newnode();int ret_xor=;
for(int i=;i<=n;++i){
scanf("%d",&a[i]);
if(op==)ret_xor=max(ret_xor,ask_xor(a[i]));
insert(a[i]);
}
printf("Case #%d: ",++kase);
if(op==)printf("%d\n",ret_xor);
else if(op==)printf("%d\n",ask_and());
else ask_or();
}
return ;
}

玲珑学院OJ 1028 - Bob and Alice are playing numbers 字典树,dp的更多相关文章

  1. ifrog-1028 Bob and Alice are playing numbers(trie树)

    题目链接: Bob and Alice are playing numbers DESCRIPTION Bob and his girl friend are playing game togethe ...

  2. 玲珑学院oj 1152 概率dp

    1152 - Expected value of the expression Time Limit:2s Memory Limit:128MByte Submissions:128Solved:63 ...

  3. 玲珑学院OJ 1023 - Magic boy Bi Luo with his excited math problem 树状数组暴力

    分析:a^b+2(a&b)=a+b  so->a^(-b)+2(a&(-b))=a-b 然后树状数组分类讨论即可 链接:http://www.ifrog.cc/acm/probl ...

  4. Light OJ 1114 Easily Readable 字典树

    题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...

  5. 玲珑oj 1028 贪心

    http://www.ifrog.cc/acm/problem/1028 很有趣的一道题,求从n个数里挑出不同的两个,使得他俩'|','&','^'后的值尽量大,求这个最大的结果. 求最大的异 ...

  6. Light OJ 1028 - Trailing Zeroes (I) (数学-因子个数)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1028 题目大意:n除了1有多少个因子(包括他本身) 解题思路:对于n的每个因子 ...

  7. 九度OJ 1028:继续畅通工程 (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3140 解决:1338 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有 ...

  8. [Lonlife1031]Bob and Alice are eating food(递推,矩阵快速幂)

    题目链接:http://www.ifrog.cc/acm/problem/1031 题意:6个水果中挑出n个,使得其中2个水果个数必须是偶数,问有多少种选择方法. 设中0代表偶数,1代表奇数.分别代表 ...

  9. 玲珑学院-ACM比赛1014 - Absolute Defeat

    1014 - Absolute Defeat Time Limit:2s Memory Limit:64MByte Submissions:257Solved:73 DESCRIPTION Eric ...

随机推荐

  1. canvas学习--准备

    一)canvas标签 属性: 1.width 和 height 控制canvas宽高: 2.style添加基本样式 3.class,id属性 4.标签内添加一行文本,主要用于浏览器不支持canvas标 ...

  2. redis 的安装及常用的命令

    前言: redis是一个key-value的存储系统,value支持string.list.set.zset.hash五种类型,且支持数据的本地存储 一.安装redis 前提:linux下需要安装了m ...

  3. nginx下TP5 隐藏入口文件+支持pathinfo模式+配置多项目根目录

    首先说下项目目录情况  跟目录/usr/share/nginx/html/(别说怎么这么深  0.0) html文件夹下面两个目录 pssh  pssh_shop 两个tp5项目分别对应两个二级域名 ...

  4. 第二十三节:scrapy爬虫识别验证码(二)图片验证码识别

    图片验证码基本上是有数字和字母或者数字或者字母组成的字符串,然后通过一些干扰线的绘制而形成图片验证码. 例如:知网的注册就有图片验证码 首先我们需要获取验证码图片,通过开发者工具我们可以得到验证码ur ...

  5. 杭电 2111 Saving HDU (贪心)

    Description 话说上回讲到海东集团面临内外交困,公司的元老也只剩下XHD夫妇二人了.显然,作为多年拼搏的商人,XHD不会坐以待毙的.   一天,当他正在苦思冥想解困良策的时候,突然想到了自己 ...

  6. java中装箱与拆箱

    转载自:https://www.cnblogs.com/dolphin0520/p/3780005.html 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若 ...

  7. CodeForcesGym 100212E Long Dominoes

    Long Dominoes Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on CodeForcesGym. ...

  8. [K/3Cloud] 树形单据体的应用说明

    1.BOSIDE制作单据,支持动态表单,单据,基础资料,报表等域模型. 2.添加列,和原来单据体一样. 3.设置主键列名,父级主键字段名,行类型字段名,节点图片字段名(没有可以为空)   4.运行时展 ...

  9. 交互设计:隐藏或显示大段文本的UI组件有哪些?

    应用场景: 在手机上要给列表中的每一项添加一个大段的介绍,应该用什么UI组件 A: 这里可以用,模态对话框,弹出提示,工具提示这类组件.模态对话框的好处,就是用关闭的按钮,用户操作方便:而弹出提示和工 ...

  10. Count Color POJ - 2777 线段树

    Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...