D. Random Task
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. She came closer and read: "Find such positive integer n,
that among numbers n + 1, n + 2, ..., 2·n there
are exactly m numbers which binary representation contains exactly kdigits
one".

The girl got interested in the task and she asked you to help her solve it. Sasha knows that you are afraid of large numbers, so she guaranteed that there is an answer that doesn't exceed 1018.

Input

The first line contains two space-separated integers, m and k (0 ≤ m ≤ 1018; 1 ≤ k ≤ 64).

Output

Print the required number n (1 ≤ n ≤ 1018).
If there are multiple answers, print any of them.

Sample test(s)
input
1 1
output
1
input
3 2
output
5

题意:

求一个n,使得n+1到2n这些数的二进制中恰好有k个1的数有m个。

思路:

在k同样的情况下,打表之后发现单调性,能够二分。

问题转化为n+1到2n二进制表示之后1的个数为k的有多少个?

进一步统一,假设知道区间[0,x]内的二进制表示之后1的个数为k有cal(x)个,那么答案就是cal(2n)-cal(n)了。

如今要 求cal(x)。

假设x为  101101 。k=3;

由于求比小于等于x的数二进制表示之后1的个数为k的有多少个,所以当第一位为0的时候。后面5位中须要3位为0才干满足条件,C[5][3]。前两位同样,第三位为0的时候。前面已经有2个1。后面三位仅仅需1个1就可以,C[3][1]...

从样例中你应该能得出结论了吧。

逐位枚举。假设前d为同样,已有num个1,该位为1。我们能够将该位置0(由于求的是[0,x]内的二进制表示之后1的个数为k有多少个),那么后面还须要k-num个1,后面还有i为的话,那么ans+= C[i][k-num]。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 15
#define MAXN 100005
#define OO (1LL<<35)-1
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std; ll n,m,k,d,ans,tot,flag,cnt;
ll C[70][70]; ll cal(ll x)
{
ll i,j,res=0,num=0;
for(i=62;i>=0;i--)
{
if(x&(1LL<<i))
{
if(k-num>=0) res+=C[i][k-num];
num++;
}
}
return res;
}
int main()
{
ll i,j,t,le,ri,mid,cnt;
for(i=0;i<=64;i++) C[i][0]=1;
for(i=1;i<=64;i++)
{
for(j=1;j<=i;j++)
{
C[i][j]=C[i-1][j]+C[i-1][j-1];
}
}
while(cin>>m>>k)
{
le=1; ri=1LL<<62;
while(le<=ri)
{
mid=(le+ri)>>1;
cnt=cal(2*mid)-cal(mid);
if(m<=cnt)
{
ans=mid;
ri=mid-1;
}
else le=mid+1;
}
cout<<ans<<endl;
}
return 0;
}

Codeforces Round #247 (Div. 2) D. Random Task的更多相关文章

  1. Codeforces Round #247 (Div. 2) ABC

    Codeforces Round #247 (Div. 2) http://codeforces.com/contest/431  代码均已投放:https://github.com/illuz/Wa ...

  2. Codeforces Round #273 (Div. 2)-B. Random Teams

    http://codeforces.com/contest/478/problem/B B. Random Teams time limit per test 1 second memory limi ...

  3. Codeforces Round #273 (Div. 2) B . Random Teams 贪心

    B. Random Teams   n participants of the competition were split into m teams in some manner so that e ...

  4. Codeforces Round #247 (Div. 2) B

    B. Shower Line time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. Codeforces Round #247 (Div. 2) B - Shower Line

    模拟即可 #include <iostream> #include <vector> #include <algorithm> using namespace st ...

  6. Codeforces Round #247 (Div. 2)

    A.水题. 遍历字符串对所给的对应数字求和即可. B.简单题. 对5个编号全排列,然后计算每种情况的高兴度,取最大值. C.dp. 设dp[n][is]表示对于k-trees边和等于n时,如果is== ...

  7. Codeforces Round #247 (Div. 2) C题

    赛后想了想,然后就过了.. 赛后....... 我真的很弱啊!想那么多干嘛? 明明知道这题的原型就是求求排列数,这不就是 (F[N]-B[N]+100000007)%100000007: F[N]是1 ...

  8. Codeforces Round #247 (Div. 2) C. k-Tree (dp)

    题目链接 自己的dp, 不是很好,这道dp题是 完全自己做出来的,完全没看题解,还是有点进步,虽然这个dp题比较简单. 题意:一个k叉树, 每一个对应权值1-k, 问最后相加权值为n, 且最大值至少为 ...

  9. [Codeforces Round #247 (Div. 2)] A. Black Square

    A. Black Square time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. 《Lua程序设计》第2章 类型与值 学习笔记

    Lua中的8中基础类型:nil(空).boolean(布尔).number(数字).string(字符串).userdata(自定义类型).function(函数).thread(线程)和table( ...

  2. 微信-苹果机无法播放声音Audio

    今天同事测试活动的时候,背景音效(多个)苹果机无法发出声音,几个手机都试过,没什么用.不过安卓机上倒是没这个问题,正常. 最开始以为是js冲突问题,将页面上所有的东西进行加处理(清空,一个个加进来), ...

  3. <转>特征工程(一)

    转自http://blog.csdn.net/han_xiaoyang/article/details/50481967 1. 引言 再过一个月就是春节,相信有很多码农就要准备欢天喜地地回家过(xia ...

  4. GCC编译命令常用选项

    GCC是GUN Compiler Collection的简称,除编译程序外,还包含其他相关工具.GCC可将高级语言编写的源代码构建成计算机直接执行的二进制代码.GCC是Linux平台下最常用的编译程序 ...

  5. 【PHP】使用GD库实现 图像生成、缩放、logo水印和简单验证码

    gd库是php最常用的图片处理库之一(另外一个是imagemagick),可以生成图片.验证码.水印.缩略图等等.要使用gd库首先需要开启gd库扩展, windows系统下需要在php.ini中将ex ...

  6. 剑指offer——49

    丑数 因子只含2,3,5的数称为丑数. 怎么求第K大的丑数呢.K可以为10^7 最简单的做法是,对每个数判断是否为丑数. 复杂度为O( n * log(n) ),理论上是不行的. uglys[i] 来 ...

  7. vue案例 - 使用vue实现自定义多选与单选的答题功能

    4月底立得flag,五月底插上小旗,结果拖到六月底七月初才来执行.说什么工作忙都是借口,就是睡的比猪早,起的比猪晚. 本来实现多选单选这个功能,vue组件中在表单方面提供了一个v-model指令,非常 ...

  8. 百度编辑器(UEditor)自定义工具栏

    百度编辑器(UEditor)自定义工具栏的自定义 百度编辑器默认功能比较齐全,但是不一定是我们所需要的,有的功能可以去掉,用自己想要的就可以了,可以参考百度官方文档! 百度编辑器默认配置展示界面 如何 ...

  9. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验八:PS/2模块② — 键盘与组合键

    实验八:PS/2模块② — 键盘与组合键 实验七之际,我们学习如何读取PS/2键盘发送过来的通码与断码,不过实验内容也是一键按下然后释放,简单按键行为而已.然而,实验八的实验内容却是学习组合键的按键行 ...

  10. Jmeter TCP取样器配置及发送图解

    最近在通过Jmeter测试TCP发送请求时,遇到相关问题,现记录 查看管方文档,TCP发送有三种启用方式: TCPClientImpl:文本数据,默认为这种 BinaryTCPClientImpl:传 ...