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 ...
随机推荐
- 《计算机组成原理 》& 《计算机网络》& 《数据库》 Roadmap for self-taugh student
计算机组成原理: UCB的这门课绝对是不错的资源. Great Ideas in Computer Architecture (Machine Structures) B站:https://www.b ...
- 【Java】面向对象
重新搞一波 复习巩固 简单记录 慕课网 imooc Java 零基础入门-Java面向对象-面向对象 都是视频课件里的. 文章目录 面向对象 什么是对象 什么是面向对象 类 什么是对象的属性和方法 类 ...
- 【Oracle】Oracle 10g下载路径
ORACLE 10g下载地址 下载方法: 直接复制下面的链接,打开迅雷,自动会识别下载的内容 Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise ...
- 安装python性能检测工具line_profiler
line_profiler是一款监测python的CPU密集型性能问题的强大工具,可以对函数进行逐行分析,在linux上安装时一切正常,然而今天在win10 64位系统安装失败了 pip3 insta ...
- 通过SE14重建数据库表
通过程序中的SQL语句向数据库表中插入的内容,系统无法转换,并且已经存在于数据库表中,那么当对该表进行保存数据的修改时,可能会导致该表从数据库中的删除. 举了例子:(完全是为了方便理解) SAP系统, ...
- REUSE_ALV_GRID_DISPLAY_LVC 的fieldcat定义
在使用REUSE_ALV_GRID_DISPLAY_LVC函数的时候,需要注意的是,内表中如果有P类型的或者数据元素为BDMNG等类型是,在定义fieldcat的时候,注意要指定fieldcat-da ...
- Android 中使用 config.gradle
各位同学大家好,当然了如果不是同学,那么大家也同好.哈哈. 大家知道config.gradle 是什么吗?我也不知道.开个完笑,其实config.gradle 就是我们为了统一gradle 中的各种配 ...
- MYSQL基础知识的复习1
数据库(是存放数据的仓库) 1.根据存储量以及安全性上来划分: 大型数据库:DB2 Oracle(毕业) Hbase 银行 公安局(不加班 没网) 移动 中型数据库:mysql sqlserver(. ...
- Python语言程序设计---函数的定义与使用
推荐一个Python学习交流的q群:610380249 在学习Python的过程中,有什么不懂的问题都可以发群里,一起讨论. 1 函数的理解和定义 函数是一段代码的表示,所指定的参数是一种占位符,如果 ...
- Py数据类型—整形与字符串
数据类型 在指针的右边输入.可以触发功能列表: 数字(整形):也就是123之类的,不能是abcd和中文之类的,数据类型为int 1.强制字符转换 a="123" b=int(a) ...