题目链接:点我

题意:

给你一个区间[a,b],让你从里面选一个连续子区间[x,y](子区间可以为[a,b]),把这个区间的所有数或起来x|x+1|x+2|...|y

你要使得区间[x,y]异或起来的结果要小于等于v。让你输出这个子区间的最大长度

题解:

我们可以先不考虑题目这个问题,换一个简单的问题:

对于任意一个数num,找到一个最大的x,x≤num,使其x|(x+1)|(x+2)|…|num>num

结论:

如果存在这样一个数x,那么num的二进制表示中的最低位的一个0,求这个0如果变成1会增加权值ans,然后x=num-ans

例如num二进制为:110101,那么x=110011

这个时候x|x+1|x+2|...|num刚好大于num,如果从x+1|x+2|...num就不会大于num(可以试试

那么可以说对于可选区间[x,y],如果把y=num,那么可选区间最长长度就是num-x

那么简化的问题就解决了,对于题目也就相当于可选区间[x,y]的右边界y不一定等于v,那么我们就可以枚举所有小于v的值,然后从中取最大区间就可以了

因为我们解决问题的时候求的x是num减去num的最低位二进制表示最低位0的权值,那么对于区间[x,y]右边界y的选取可以枚举0的位置就可以了

我们可以使用lowbit(~a)方式求num的最低位二进制表示最低位0的权值

因为~a就是把a按照二进制形式每一位取反,这样的话每一位的0变成了1,1变成了0

然后使用lowbit(b)方法求出来b的二进制下最低位1的权值

x&(-x),当x为0时结果为0;x为奇数时,结果为1;x为偶数时,结果为x中2的最大次方的因子。

而且这个有一个专门的称呼,叫做lowbit,即取2^k。

代码:

#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <vector>
#include <bitset>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int readint()
{
int tmp = 0, fh = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-') fh = -1;
c = getchar();
}
while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar();
return tmp * fh;
}
inline long long readll()
{
long long tmp = 0, fh = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-') fh = -1;
c = getchar();
}
while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar();
return tmp * fh;
}
ull lowbit(ull x) //求一个数二进制中的最低位1的数值
{
return -x & x;
}
ll min(ll a,ll b)
{
if(a<b) return a;
else return b;
}
ll max(ll a,ll b)
{
if(a>b) return a;
else return b;
}
int main()
{
//printf("%lld\n",1ll<<62);
int t;
scanf("%d",&t);
while(t--)
{
ll a,b,v,len1=0,len2=0;
scanf("%lld%lld%lld",&a,&b,&v);
for(int i=0;i<=62;++i) //1左移62位都1e19呢
{
if((1ll<<i)&b)
len1=i;
if((1ll<<i)&v)
len2=i;
}
if(len2>len1)
{
printf("%lld\n",b-a+1);
continue;
}
if(v<a)
{
printf("0\n");
continue;
}
ll ans=0;
ull res=v;
while(1)
{
ull l; //因为我们要求出来res的最低二进制位0的权值,所以需要取反
ull tmp=lowbit(~res);
if(res>=tmp)
{
l=res-tmp;
}
else l=a-1;
ans=max(ans,min(res,b)-max(l,a-1));
printf("%lld***\n",ans);
res=l;
if(a>res) break;
}
printf("%lld\n",ans);
}
return 0;
}

CF-gym/101810 J、T-Shirts Dilemma的更多相关文章

  1. gym 101810 M. Greedy Pirate (LCA)

    题目:https://codeforc.es/gym/101810/problem/M 题意:给 你一颗树,下面有m次查询,求u->v的最大值是多少,输入两点之间都会有两条边,正边有正权,反边有 ...

  2. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  3. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  4. 【codeforces.com/gym/100240 J】

    http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...

  5. CF Gym 102028G Shortest Paths on Random Forests

    CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...

  6. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  7. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  8. codeforces Gym 100500 J. Bye Bye Russia

    Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...

  9. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

随机推荐

  1. python学习笔记 | strftime()格式化输出时间

    time模块 import time t = time.strftime("%Y-%m-%d %H:%M:%S") print(t) datetime模块 import datet ...

  2. 行业动态 | 利用Cassandra数据库揭开家族祖先的秘密

        FamilySearch选择了基于Apache Cassandra的DataStax Enterprise (DSE)来加速用户增长,并通过更快的反应时间.高可用性以及零数据库宕机来提供强大的 ...

  3. ip访问本机vs调试项目

    环境:win10 vs2019 webapi F5启动调试. 问题:localhost可以访问,127.0.0.1和本机ip访问不了.比如想让别人浏览一下看效果,或者测试人员测试功能,每次修改都有重新 ...

  4. MySQL select join on 连表查询和自连接查询

    连表查询 JOIN ON 操作 描述 inner join 只返回匹配的值 right join 会从右表中返回所有的值, 即使左表中没有匹配 left join 会从左表中返回所有的值, 即使右表中 ...

  5. JDBC代码的优化

    JDBC代码简化以及PreparedStatement和Statement接口 抽取 JDBC的Bug sql语句可以拼接导致登录功能中如果用户名或者密码中出现'or'2'='2则一定可以登录的bug ...

  6. TCP服务器程序

    Linux下编写TCP服务器调用的函数顺序为:socket -> bind -> listen -> accept -> recv/send socket 参见:http:// ...

  7. JVM(八)执行引擎相关内容

    一:两种解释器 JAVA字节码解释器: java字节码===>c++代码==>硬编码. 首先.java文件编译成字节码,遍历每行的字节码指令,因为每个字节码指令的含义都是固定的所以可以根据 ...

  8. gRPC Load Balancing

    gRPC Load Balancing 翻译自:https://grpc.io/blog/grpc-load-balancing/ 这是gRPC负载均衡的第一篇,后续会给出基于golang XDS服务 ...

  9. 页面切换提速30%!京东商城APP首屏耗时监控及优化实践

    https://mp.weixin.qq.com/s/vIG_x1MWC33HKV1GxalVHg 原创 平台研发朱跃棕等 京东零售技术 2020-12-09 网络接口请求可以从接口结构合理性及多接口 ...

  10. Linux 从4.12内核版本开始移除了 tcp_tw_recycle 配置。 tcp_max_tw_buckets TIME-WAIT 稳定值

    被抛弃的tcp_recycle_小米云技术-CSDN博客_sysctl: cannot stat /proc/sys/net/ipv4/tcp_tw_recy https://blog.csdn.ne ...