Codeforces Round #247 (Div. 2) D. Random Task
1 second
256 megabytes
standard input
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.
The first line contains two space-separated integers, m and k (0 ≤ m ≤ 1018; 1 ≤ k ≤ 64).
Print the required number n (1 ≤ n ≤ 1018).
If there are multiple answers, print any of them.
1 1
1
3 2
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的更多相关文章
- Codeforces Round #247 (Div. 2) ABC
Codeforces Round #247 (Div. 2) http://codeforces.com/contest/431 代码均已投放:https://github.com/illuz/Wa ...
- 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 ...
- 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 ...
- Codeforces Round #247 (Div. 2) B
B. Shower Line time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #247 (Div. 2) B - Shower Line
模拟即可 #include <iostream> #include <vector> #include <algorithm> using namespace st ...
- Codeforces Round #247 (Div. 2)
A.水题. 遍历字符串对所给的对应数字求和即可. B.简单题. 对5个编号全排列,然后计算每种情况的高兴度,取最大值. C.dp. 设dp[n][is]表示对于k-trees边和等于n时,如果is== ...
- Codeforces Round #247 (Div. 2) C题
赛后想了想,然后就过了.. 赛后....... 我真的很弱啊!想那么多干嘛? 明明知道这题的原型就是求求排列数,这不就是 (F[N]-B[N]+100000007)%100000007: F[N]是1 ...
- Codeforces Round #247 (Div. 2) C. k-Tree (dp)
题目链接 自己的dp, 不是很好,这道dp题是 完全自己做出来的,完全没看题解,还是有点进步,虽然这个dp题比较简单. 题意:一个k叉树, 每一个对应权值1-k, 问最后相加权值为n, 且最大值至少为 ...
- [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 ...
随机推荐
- 【架构师之路】 LVS+Keepalived实现高可用负载均衡
一.原理 1.概要介绍 如果将TCP/IP划分为5层,则Keepalived就是一个类似于3~5层交换机制的软件,具有3~5层交换功能,其主要作用是检测web服务器的状态, ...
- 日记整理---->2017-05-14
学习一下知识吧,好久没有写博客了.如果他总为别人撑伞,你又何苦非为他等在雨中. 学习的知识内容 一.关于base64的图片问题 byte[] decode = Base64.base64ToByteA ...
- Python Tkinter Entry(文本框)
Python学习记录--关于Tkinter Entry(文本框)的选项.方法说明,以及一些示例. 属性(Options) background(bg) borderwidth(bd) cursor e ...
- redhat vi 命令
转载:http://www.cnblogs.com/zhanglong0426/archive/2010/10/07/1845268.html http://blog.sina.com.cn/s/bl ...
- Excel中countif函数的使用方法
1.countif函数的含义 在指定区域中按指定条件对单元格进行计数(单条件计数) 建议和countifs函数结合起来学习,可以观看小编的经验Excel中countifs函数的使用方法. END 2. ...
- 从零开始学习Hadoop--第4章 序列化(转载)
作者对序列化的描述浅显易懂!(https://www.douban.com/note/313096752/) 1. 序列化从头说 在面向对象程序设计中,类是个很重要的概念.所谓“类”,可以将它想像成建 ...
- linux下find(文件查找)命令的用法总结
关联文章:http://blog.chinaunix.net/uid-24648486-id-2998767.html
- 微信小游戏 50M那部分的缓存机制的使用
一.使用 AssetsManager 灵活定制微信小游戏的缓存策略 官网教程:http://developer.egret.com/cn/github/egret-docs/Engine2D/mini ...
- R子集subset
> x<-c(6,1,2,3,NA,12) > x[x>5] #x[5]是未知的,因此其值是否大于5也是未知的 [1] 6 NA 12 > subset(x,x& ...
- DevOps之持续交付
持续交付 持续交付是一种可以帮助团队以更短的周期交付软件的方法,该方法确保了团队可以在任何时间发布出可靠的软件.该方法意在以更快速度更高频率进行软件的构建.测试和发布. 通过对生产环境中的应用程序进行 ...