题目链接:

Bob and Alice are playing numbers

DESCRIPTION
Bob and his girl friend are playing game together.This game is like this: There are nn numbers. If op = 11,Bob wants to find two numbers aiai and ajaj,that aiai & ajaj will become maximum value. If op = 22,Bob wants to find two numbers aiai and ajaj,that aiai ^ ajaj will become maximum value. If op = 33,Bob wants to find two numbers aiai and ajaj,that aiai | ajaj will become maximum value. Notice: & for bitwise AND. (4 & 2) is 0, (4 & 5) is 4. ^ for bitwise XOR. (4 ^ 2) is 6, (4 ^ 5) is 1. | for bitwise OR . (4 | 2) is 6, (4 | 5) is 5.
INPUT
First line is a positive integer TT , represents there are TT test cases. For each test case: First line includes two numbers n(2≤n≤100000),op(1≤op≤3)n(2≤n≤100000),op(1≤op≤3). The next line contains nn numbers: a1,a2,...,an(1≤ai≤1000000)a1,a2,...,an(1≤ai≤1000000).
OUTPUT
For the ii-th test case , first output Case #i: in a single line. Then output the answer of ii-th test case.
SAMPLE INPUT
 
3
2 1
4 2
2 2
4 2
2 3
4 2
SAMPLE OUTPUT
 
Case #1: 0
Case #2: 6
Case #3: 6
 
题意:
 
^ & |三种操作找到能得到的最大值;
 
思路:
 
异或的值就是经典的trie树,&可以转化成trie树上的贪心,|也是一个贪心了,对于一个数x[i],那么就找它所有为0的位置,看最大能贪心多少;
 
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const int mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=(1<<20)+10;
const int maxn=1e5+110;
const double eps=1e-12; int ch[21*maxn][2],sz,val[21*maxn],s[23],dp[21],x[maxn],n;
bool vis[N*2];
void insert(int x)
{
mst(s,0);
int n=0,u=0;
while(x)s[n++]=(x&1),x>>=1;
for(int i=20;i>=0;i--)
{
int c=s[i];
if(!ch[u][c])
{
ch[sz][0]=ch[sz][1]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
val[u]++;
}
}
inline int query_or()
{
memset(vis,false,sizeof(vis));
For(i,1,n)vis[x[i]]=true;
for(int i=(1<<20);i>0;i--)
{
for(int j=0;j<=20;j++)
{
if(!(i&(1<<j)))vis[i]|=vis[i|(1<<j)];
}
}
int ans=0,t[22];
for(int i=1;i<=n;i++)
{
int num=0,temp=0;
for(int j=20;j>=0;j--)
{
if(!(x[i]&(1<<j)))t[num++]=(1<<j);
}
for(int j=0;j<num;j++)
{
if(vis[temp|t[j]])temp|=t[j];
}
ans=max(ans,temp|x[i]);
}
return ans;
}
void same(int &l,int r)
{
if(!r)return ;
if(!l)
{
ch[sz][0]=ch[sz][1]=0;
l=sz++;
}
val[l]+=val[r];
same(ch[l][0],ch[r][0]);
same(ch[l][1],ch[r][1]);
}
inline int query_and()
{
int u=0,ans=0;
for(int i=20;i>=0;i--)
{
if(val[ch[u][1]]>=2)u=ch[u][1],ans|=dp[i];
else same(ch[u][0],ch[u][1]),u=ch[u][0];
}
return ans;
}
inline int query_xor(int x)
{
mst(s,0);
int n=0,u=0,ans=0;
while(x)s[n++]=(x&1),x>>=1;
for(int i=20;i>=0;i--)
{
int c=s[i];
if(ch[u][c^1])ans|=dp[i],u=ch[u][c^1];
else u=ch[u][c];
}
return ans;
}
inline void Init()
{
sz=1;
mst(ch[0],0);
mst(val,0);
for(int i=0;i<=20;i++)
dp[i]=(1<<i);
}
int main()
{
int t,Case=0;
read(t);
while(t--)
{
int op,ans=0;
read(n);read(op);
Init();
read(x[1]);insert(x[1]);
For(i,2,n)
{
read(x[i]);
if(op==2)ans=max(ans,query_xor(x[i]));
if(op<3)insert(x[i]);
}
if(op==1)ans=query_and();
else if(op==3)ans=query_or();
printf("Case #%d: %d\n",++Case,ans);
}
return 0;
}

  

ifrog-1028 Bob and Alice are playing numbers(trie树)的更多相关文章

  1. 玲珑学院OJ 1028 - Bob and Alice are playing numbers 字典树,dp

    http://www.ifrog.cc/acm/problem/1028 题解处:http://www.ifrog.cc/acm/solution/4 #include <cstdio> ...

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

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

  3. ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)

    Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...

  4. codeforces 446C DZY Loves Fibonacci Numbers 线段树

    假如F[1] = a, F[2] = B, F[n] = F[n - 1] + F[n - 2]. 写成矩阵表示形式可以很快发现F[n] = f[n - 1] * b + f[n - 2] * a. ...

  5. Codeforces446C DZY Loves Fibonacci Numbers(线段树 or 分块?)

    第一次看到段更斐波那契数列的,整个人都不会好了.事后看了题解才明白了一些. 首先利用二次剩余的知识,以及一些数列递推式子有下面的 至于怎么解出x^2==5(mod 10^9+9),我就不知道了,但是要 ...

  6. Bnuoj-29359 Deal with numbers 线段树

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=29359 题意:一个数列,有三种操作: 1.区间[a,b]之间大于零的数整出c. 2.区间[ ...

  7. Codeforces 446C DZY Loves Fibonacci Numbers [线段树,数论]

    洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...

  8. CF446C DZY Loves Fibonacci Numbers 线段树 + 数学

    有两个性质需要知道: $1.$ 对于任意的 $f[i]=f[i-1]+f[i-2]$ 的数列,都有 $f[i]=fib[i-2]\times f[1]+fib[i-1]\times f[2]$ 其中 ...

  9. Game

    Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

随机推荐

  1. linux下导入、导出mysql数据库命令 下载文件到本地

    一.下载到本地 yum install lrzsz sz filename  下载 rz filename  上传   linux下导入.导出mysql数据库命令 一.导出数据库用mysqldump命 ...

  2. Visual Studio 2013下JSON可视化工具

          Visual Studio 2013现在我们有个小工具可以实现JSON可视化,这样给我们调试JSON提供了便利. JSON这种数据格式已经比较流行,在WEB前端随处可见. 在你需要安装VS ...

  3. asp.net 发送邮件

    asp.net 发送邮件 System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();            msg.To. ...

  4. Ansible用于网络设备管理 part 0 安装和登录网络设备

    真实在是累了,但是又怕第二天早上又忘掉,在这先大概写写. 安装: http://www.tecmint.com/install-and-configure-ansible-automation-too ...

  5. js一些小题(二)

    ******************************************************************* 一个全局下的函数: function test() { aler ...

  6. jquery实现页面控件拖动效果js代码

    ;(function($) { var DragPanelId = "divContext"; var _idiffx = 0; var _idiffy = 0; var _Div ...

  7. [ html canvas putImageData ] canvas绘图属性 putImageData 属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  8. 参加2015年TOP100会议的零散笔记

    2015年出差很少,感到整个技术都已经荒废了,收到12月份TOP100的会议通知后,还是去充点电吧,不然心慌啊.对于软件大会这种大杂烩式的会议已经没有多少兴趣了,看看这个TOP100组织得有何不同? ...

  9. android Java BASE64编码和解码一:基础

    今天在做Android项目的时候遇到一个问题,需求是向服务器上传一张图片,要求把图片转化成图片流放在 json字符串里传输. 类似这样的: {"name":"jike&q ...

  10. JAVA基础学习day13--String、StringBuilder与StringBuffer与包装类

    一.String 1.1.String String 类是final修饰的,是顶级类,不可被继承 String 类代表字符串.Java 程序中的所有字符串字面值(如 "abc" ) ...