Codeforces Round #608 (Div. 2) E. Common Number (二分,构造)
题意:对于一个数\(x\),有函数\(f(x)\),如果它是偶数,则\(x/=2\),否则\(x-=1\),不断重复这个过程,直到\(x-1\),我们记\(x\)到\(1\)的这个过程为\(path(x)\),它表示这个过程中所有\(x\)的值,现在给你一个数\(n\)和\(k\),要你找出最大的数\(x\),并且\(x\)在\(path[1,n]\)中出现的次数不小于\(k\).
题解:我们对于\(x\)可以选择分奇偶来讨论.
1.假如\(x\)为奇数,那么根据题意,我们知道\(x,2x,2x+1,4x,4x+1,4x+2,8x,8x+1,8x+2,8x+3,8x+4....\)这些数包含且只有这些数包含\(x\).
2.假如\(x\)为偶数,那么\(x,x+1,2x,2x+1,2x+2,2x+3,4x,4x+1,4x+2,4x+3,4x+4,4x+5,4x+6,4x+7,...\)这些数包含且只有这些数包含\(x\).
那么我们就可以分奇偶数来二分求答案.代码:
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
#define rep(a,b,c) for(int a=b;a<=c;++a)
#define per(a,b,c) for(int a=b;a>=c;--a)
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) {return a/gcd(a,b)*b;}
ll n,k;
bool check(ll x){
ll tmp=x;
ll cnt=0;
ll cur;
if(x&1) cur=1;
else cur=2;
while(x<=n){
cnt+=min(cur,n-x+1);
x<<=1;
cur<<=1;
}
if(cnt>=k) return true;
return false;
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>k;
ll l=0,r=n;
ll ans=1;
//odd:
while(l<r){
ll mid=(l+r+1)>>1;
ll x=mid*2+1;
if(check(x)) l=mid;
else r=mid-1;
}
ans=max(ans,2*l+1);
l=0,r=n;
//even
while(l<r){
ll mid=(l+r+1)>>1;
ll x=mid*2;
if(check(x)) l=mid;
else r=mid-1;
}
ans=max(ans,2*l);
cout<<ans<<'\n';
return 0;
}
Codeforces Round #608 (Div. 2) E. Common Number (二分,构造)的更多相关文章
- Codeforces Round #608 (Div. 2) E - Common Number (二分 思维 树结构)
- Codeforces Round #608 (Div. 2) E. Common Number
链接: https://codeforces.com/contest/1271/problem/E 题意: At first, let's define function f(x) as follow ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- Codeforces Round #608 (Div. 2)
传送门 A. Suits 签到. Code /* * Author: heyuhhh * Created Time: 2019/12/15 17:16:33 */ #include <iostr ...
- Codeforces Round #427 (Div. 2) B. The number on the board
引子: A题过于简单导致不敢提交,拖拖拉拉10多分钟还是决定交,太冲动交错了CE一发,我就知道又要错过一次涨分的机会.... B题还是过了,根据题意目测数组大小开1e5,居然蒙对,感觉用vector更 ...
- Codeforces Round #585 (Div. 2) B. The Number of Products(DP)
链接: https://codeforces.com/contest/1215/problem/B 题意: You are given a sequence a1,a2,-,an consisting ...
- Codeforces Round #608 (Div. 2) D. Portals
链接: https://codeforces.com/contest/1271/problem/D 题意: You play a strategic video game (yeah, we ran ...
- Codeforces Round #411 div 2 D. Minimum number of steps
D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...
随机推荐
- tensorflow安装使用过程错误及解决方法
tensorflow2.x 使用过程中常见错误(持续更新) 安装配置,使用tensorflow训练模型,转换为tflite模型,并部署与移动端过程中,虽然不难,但是也常出现一些莫名其妙的问题,下面简单 ...
- Linux简单Shell脚本监控MySQL、Apache Web和磁盘空间
Linux简单Shell脚本监控MySQL.Apache Web和磁盘空间 1. 目的或任务 当MySQL数据库.Apache Web服务器停止运行时,重新启动运行,并发送邮件通知: 当服务器磁盘的空 ...
- 使用Jenkins+Blue Ocean 持构建自动化部署之安卓源码打包、测试、邮件通知
什么是BlueOcean? BlueOcean重新考虑了Jenkins的用户体验.BlueOcean由Jenkins Pipeline设计,但仍然兼容自由式工作,减少了团队成员的混乱,增加了清晰度. ...
- dig的安装和使用
-bash: dig: command not found 解决办法: yum -y install bind-utils dig www.baid bu.com 查看a记录 dig www.ba ...
- ctfhub技能树—sql注入—字符型注入
打开靶机 查看页面信息 查询回显位 查询数据库名(查询所有数据库名:select group_concat(schema_name) from information_schema.schemata) ...
- 分布式 ID 生成算法 — SnowFlake
一.概述 分布式 ID 生成算法的有很多种,Twitter 的 SnowFlake 就是其中经典的一种. SnowFlake 算法生成 ID 的结果是一个 64bit 大小的整数,它的结构如下图: 1 ...
- Python Debug工具
最近在github上冒出了一个python的debug神器PySnooper,号称在debug时可以消灭print.那么该工具有哪些优点呢,如何使用该工具呢.本文就介绍该工具的优缺点和使用方式. 前言 ...
- ATtiny3217 x WS2812B梦幻联动
TinyAVR 1-series是Microchip于2018年推出的AVR单片机系列,定位是新一代的8位单片机,ATtiny3217是其中最高端的一款.相比于ATmega328P那个时代的AVR,A ...
- CentOS对接GlusterFS
存储节点部署示例环境,仅供参考 主机名 IP 系统 gfs01 10.10.10.13 CentOS 7.4.1708 gfs02 10.10.10.14 CentOS 7.4.1708 一.Glus ...
- E2.在shell中正确退出当前表达式
E2.在shell中正确退出当前表达式 优雅退出当前表达式 在shell里面输出复杂的多行表达时,经常由于少输入一个引号,一直无法退出当前的表达式求值,也没有办法终止它,以前只能通过两次Ctrl+C结 ...