CF-gym/101810 J、T-Shirts Dilemma
题目链接:点我
题意:
给你一个区间[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的更多相关文章
- gym 101810 M. Greedy Pirate (LCA)
题目:https://codeforc.es/gym/101810/problem/M 题意:给 你一颗树,下面有m次查询,求u->v的最大值是多少,输入两点之间都会有两条边,正边有正权,反边有 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- 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 ...
- 【codeforces.com/gym/100240 J】
http://codeforces.com/gym/100240 J [分析] 这题我搞了好久才搞出样例的11.76....[期望没学好 然后好不容易弄成分数形式.然后我‘+’没打..[于是爆0... ...
- CF Gym 102028G Shortest Paths on Random Forests
CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- codeforces Gym 100500 J. Bye Bye Russia
Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
随机推荐
- Go语言从入门到放弃(设置 go get 为国内源)
前言 Go语言学到 Gin 框架了, 其实每天学习是比较辛苦的事情, 坚持下去! 在使用 Go 过程中发现, 最无奈的是Go的一些模块下不下来, 即便挂了V, 油管2k不卡的那种, 依旧是 time ...
- 数组的方法some和includes
some() 方法用于检测数组中的元素是否满足指定条件(函数提供). some() 方法会依次执行数组的每个元素: 如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测. 如果没 ...
- InheritableThreadlocal使用问题排查
背景 在做一个微服务系统的时候,我们的参数一般都是接在通过方法定义来进行传递的,类似这样 public void xxx(Param p, ...){ // do something } 然后这时有个 ...
- AgileConfig - RESTful API 介绍
AgileConfig AgileConfig是一个基于.net core开发的轻量级配置中心. AgileConfig秉承轻量化的特点,部署简单.配置简单.使用简单.学习简单,它只提取了必要的一些功 ...
- ALV中的fieldcat详解
字段目录是用来控制ALV显示的网格中每个字段的属性的,比如字段的顺序,对齐方式,可编辑状态,颜色,等等.常用的字段如下: Row_pos: 默认值为0,可选值为1.2.3,既最大分3级别显示 c ...
- Linux Ubuntu系统版本通过Crontab设置定时任务的执行
Linux Ubuntu系统版本通过Crontab设置定时任务的执行 本文由本人收集网络信息总结而来 特别鸣谢:https://linux.zone/2258 1 crontab 简单介绍以及语法使用 ...
- Redis-第五章节-8种数据类型
目录 一.Redis对key的操作 二.五种数据类型 String类型 List(集合) Set(集合) Hash(哈希) Zset(有序集合) 三.三种特殊数据类型 geospatial(地理位置) ...
- 集成Redis缓存
一.简介 1.场景 由于首页数据变化不是很频繁,而且首页访问量相对较大,所以我们有必要把首页数据缓存到redis中,减少数据库压力和提高访问速度. 2.RedisTemplate Jedis是Redi ...
- vue3.0 composition API
一.Setup函数 1.创建时间:组件创建之前被调用,优先与created被调用,this指向的实例为window,created所指向的实例为proxy 2.this指向:不会指向组件实例 3.参数 ...
- Property or method "previewUrl" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components,
Property or method "previewUrl" is not defined on the instance but referenced during rende ...