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. [GXOI/GZOI2019]与或和(单调栈)

    想了想决定把这几题也随便水个解题报告... bzoj  luogu 思路: 首先肯定得拆成二进制30位啊 此后每一位的就是个01矩阵 Q1就是全是1的矩阵个数 Q2就是总矩阵个数减去全是0的矩阵个数 ...

  2. 零基础入门学习Python(8)--了不起的分支和循环2

    前言 上节课小甲鱼教大家如何正确的打飞机,其要点是判断和循环,判断就是该不该做某事,循环就是持续做某事 知识点 写一个程序 按照100分制,90分以上成绩为A,80到90为B,60到80为C,60以下 ...

  3. pxc集群安装

    一.pxc镜像url:https://hub.docker.com/r/percona/percona-xtradb-cluster/ 二.安装及重命名: 1.安装:docker pull perco ...

  4. 安装composer及切换镜像为国内镜像

    一.下载composer php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 下面的 ...

  5. Ubuntu 系统安装(这里用ubuntu 16.04)

    一.安装Vmware Workstation 12 选择新建虚拟机- 下一步-安装根据红框部分及说明一步一步进行 点击下一步进行 接下来默认下一步,直到如下图 这里的最大磁盘大小100G.不会直接在本 ...

  6. 微信sdk 签名

    <?php namespace app\wechat\service; use think\Config; class Signature { protected $appId ; protec ...

  7. Python之FTP实现

    Python之FTP实现 上传下载: import socket import struct import json import subprocess import os class MYTCPSe ...

  8. 集训第五周动态规划 F题 最大子矩阵和

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

  9. //……关于promise

    什么是promise? promise 翻译成中文的意思是 "承诺" ,一个承诺说出去了说明他是进行中的,承诺兑现了代表成功,没有兑现代表失败了. promise 对象的状态一旦发 ...

  10. Unity Water Shader

    上图是一个物体浸入水中的效果 原理 我们使用相机渲染的整个场景的深度图减去需要忽略的模型的深度,这里忽略的是图中蓝色部分,就保留了其他的深度值. 用到Main Camera渲染的深度贴图: sampl ...