题目描述

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

Input

输入包含两个正整数\(m,k\)。$(0<=m<=1e18, 1<=k<=64) $

Output

输出\(n\)。

Sample Input

1 1

3 2

Sample Output

1
5

首先我们要知道这个答案是具有单调性的。

即若满足\(m>n\),则区间\([m,2m]\)内满足条件的数的数量\(>\)区间\([n,2n]\)内满足条件的数的数量。


证明如下:

将区间\([n+1,2n]\)分割成两个区间\([n+1,n+1]\)和\([n+2,2n]\)。

而区间\([n+2,2*(n+1)]\)可以分割成\([n+2,2n]\)和\([2n+1,2n+2]\)。

而\(2n+2\)二进制中\(1\)的数量和\(n+1\)中\(1\)的数量是相同的。

而多了一个\(2n+1\)只有可能让数量增加,所以答案满足单调性。

即对于\(m>n\),则区间\([m,2m]\)内满足条件的数的数量\(>\)区间\([n,2n]\)内满足条件的数的数量。

证毕。


于是我们就可以二分求解了。

现在我们就只用判断\([n+1,2n]\)中满足条件的数的数量即可。

我们发现对于区间\([n+1,2n]\)满足条件的数的数量可以转换为\([1,2n]\)-\([1,n]\)的数量。

而一个前缀的数量肯定会更好求解。

问题进一步转换为求解\([1,n]\)内满足条件数的数量。

这个问题我们可以利用两种方法求解:

方法一:计数DP

对于当前\(n\),我们从二进制位高的开始考虑,首先我们只有在遇到一个\(1\)时才能更新答案。

对于当前遇到的\(1\),我们发现前面的位上的数是固定的,一共有\(tot\)个\(1\),而我们再后面的\(i\)个位上可以选择\(k-tot\)个位置放置\(1\),所以答案增加\(C(i,n-tot)\),然后\(tot++\)即可。

代码如下

#include <bits/stdc++.h>
using namespace std; #define int long long
#define reg register
#define Raed Read
#define clr(a,b) memset(a,b,sizeof a)
#define debug(x) cerr<<#x<<" = "<<x<<endl;
#define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a)
#define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a)
#define drep(a,b,c) for(reg int a=(b),a##_end_=(c); a>=a##_end_; --a)
#define erep(i,G,x) for(int i=(G).Head[x]; i; i=(G).Nxt[i])
#pragma GCC target("avx,avx2,sse4.2")
#pragma GCC optimize(3) inline int Read(void) {
int res=0,f=1;
char c;
while(c=getchar(),c<48||c>57)if(c=='-')f=0;
do res=(res<<3)+(res<<1)+(c^48);
while(c=getchar(),c>=48&&c<=57);
return f?res:-res;
} template<class T>inline bool Min(T &a, T const&b) {
return a>b?a=b,1:0;
}
template<class T>inline bool Max(T &a, T const&b) {
return a<b?a=b,1:0;
} const int N=1e5+5,M=2e5+5,mod=1e9+7; bool MOP1; int m,k,C[70][70]; int solve(int x) {
int Ans=0,tot=0;
drep(i,63,0)if(x&(1ll<<i)) {
if(tot<=k)Ans+=C[i][k-tot];
tot++;
}
return Ans;
} bool MOP2; inline void _main() {
m=Read(),k=Read();
int L=1,R=1e18,Ans=0;
C[0][0]=1;
rep(i,1,64) {
C[i][0]=1;
rep(j,1,i)C[i][j]=C[i-1][j-1]+C[i-1][j];
}
while(L<=R) {
int mid=(L+R)>>1;
if(solve(mid*2)-solve(mid)>=m)Ans=mid,R=mid-1;
else L=mid+1;
}
printf("%lld\n",Ans);
} signed main() {
_main();
return 0;
}

方法二:数位DP

我们可以直接将\(n\)转换为二进制数,那么就可以直接\(dp\)了。

\(dp[i][j]\)代表考虑到第\(i\)位,\(1\)的数量为\(j\)的数的个数。

代码如下

#include <bits/stdc++.h>
using namespace std; #define int long long
#define reg register
#define Raed Read
#define clr(a,b) memset(a,b,sizeof a)
#define debug(x) cerr<<#x<<" = "<<x<<endl;
#define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a)
#define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a)
#define drep(a,b,c) for(reg int a=(b),a##_end_=(c); a>=a##_end_; --a)
#define erep(i,G,x) for(int i=(G).Head[x]; i; i=(G).Nxt[i])
#pragma GCC target("avx,avx2,sse4.2")
#pragma GCC optimize(3) inline int Read(void) {
int res=0,f=1;
char c;
while(c=getchar(),c<48||c>57)if(c=='-')f=0;
do res=(res<<3)+(res<<1)+(c^48);
while(c=getchar(),c>=48&&c<=57);
return f?res:-res;
} template<class T>inline bool Min(T &a, T const&b) {
return a>b?a=b,1:0;
}
template<class T>inline bool Max(T &a, T const&b) {
return a<b?a=b,1:0;
} const int N=1e5+5,M=2e5+5,mod=1e9+7; bool MOP1; int m,k,dp[70][70],A[70]; int dfs(int pos,int tot,int limit) {
if(!pos)return tot==k;
if(!limit&&dp[pos][tot]!=-1)return dp[pos][tot];
int up=limit?A[pos]:1,res=0;
rep(i,0,up)res+=dfs(pos-1,tot+i,limit&(i==up));
if(!limit)dp[pos][tot]=res;
return res;
} int solve(int x) {
int len=0;
while(x)A[++len]=x&1,x>>=1;
return dfs(len,0,1);
} bool MOP2; inline void _main() {
// cerr<<"M="<<(&MOP2-&MOP1)/1024.0/1024.0<<endl;
m=Read(),k=Read();
int L=1,R=1e18,Ans=0;
memset(dp,-1,sizeof dp);
while(L<=R) {
int mid=(L+R)>>1;
if(solve(mid*2)-solve(mid)>=m)Ans=mid,R=mid-1;
else L=mid+1;
}
printf("%lld\n",Ans);
} signed main() {
_main();
return 0;
}

CodeForces-431D Random Task的更多相关文章

  1. Codeforces Round #247 (Div. 2) D. Random Task

    D. Random Task time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. codeforces 478B Random Teams

    codeforces   478B  Random Teams  解题报告 题目链接:cm.hust.edu.cn/vjudge/contest/view.action?cid=88890#probl ...

  3. codeforces 431 D. Random Task 组合数学

    题意: 给定m,k 0 <= m <= 10^18 ,1 <= k <= 64 求一个数n,满足n+1,n+2,...n+n这n个数中,刚好有m个数的2进制表示法刚好有k个1 ...

  4. codeforces 478B Random Teams 解题报告

    题目链接:http://codeforces.com/problemset/problem/478/B 题目意思:有 n 个人,需要将这班人分成 m 个 组,每个组至少含有一个人,同一个组里的人两两可 ...

  5. Codeforces 653F Paper task SA

    Paper task 如果不要求本质不同直接st表二分找出最右端, 然后计数就好了. 要求本质不同, 先求个sa, 然后用lcp求本质不同就好啦. #include<bits/stdc++.h& ...

  6. CF431D Random Task 二分+数位dp

    One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. ...

  7. Codeforces 846F - Random Query

    原题链接:http://codeforces.com/contest/846/problem/F 题意:给一个数列,任意取区间[l, r],问区间内不同数字的个数的期望是多少. 思路: 对于第i个数a ...

  8. Codeforces 1067E - Random Forest Rank(找性质+树形 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 一道不知道能不能算上自己 AC 的 D1E(?) 挺有意思的结论题,结论倒是自己猜出来了,可根本不会证( 开始搬运题解 ing: 碰到这样 ...

  9. Codeforces 788E - New task(线段树)

    Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1E,而且被!我!自!己!搞!出!来!了! 虽然我承认它难度及摆放的位置异常异常虚高,并且就算我到了现场也不可 ...

  10. CodeForces - 846F Random Query(期望)

    You are given an array a consisting of n positive integers. You pick two integer numbers l and r fro ...

随机推荐

  1. Python 标准库、第三方库

    Python 标准库.第三方库 Python数据工具箱涵盖从数据源到数据可视化的完整流程中涉及到的常用库.函数和外部工具.其中既有Python内置函数和标准库,又有第三方库和工具.这些库可用于文件读写 ...

  2. Java容器——Set和顺序存储

    当Set使用自己创建的类型时,存储的顺序如何维护,在不同的Set实现中会有不同,而且它们对于在特定的Set中放置的元素类型也有不同的要求: Set(interface) 存入Set的每个元素都必须是唯 ...

  3. Js基础知识(一) - 变量

    js基础 - 变量 *大家对js一定不会陌生,入门很简单(普通入门),很多人通过网络资源.书籍.课堂等很多途径学习js,但是有些js基础的只是往往被大家遗漏,本章就从js变量类型来说一说js 变量类型 ...

  4. 前端开发——让算法"动"起来

    正文 当然在我们不清楚具体操作细节前我们可以先假设一下,我们能够用什么来实现.按照以前看过的排序动画我将其分为 1.Js操作Dom,再搭配简单的css 2.Canvas动画 之后在查资料的时候发现还有 ...

  5. springboot的优点

    2013年12月12日,spring发布了4.0版本.这个本来只是作为Java平台上的控制反转容器的库,经过将近10年的发展已经成为了一个巨无霸产品.不过其依靠良好的分层设计,每个功能模块都能保持较好 ...

  6. windows spark1.6

    jdk1.7 scala 2.10.5 spark 1.6.1 http://spark.apache.org/downloads.html hadoop 2.6.4 只需要留bin https:// ...

  7. zeppelin安装使用

    官网:http://zeppelin-project.org/  代码:https://github.com/NFLabs/zeppelin  使用:按照官网的视频操作一遍,应该就懂了http://y ...

  8. Centos 6.5 Multipath 初始配置

    # This is a basic configuration file with some examples, for device mapper # multipath. # For a comp ...

  9. leetcode-easy-trees-98. Validate Binary Search Tree-NO

    mycode   不会 注意:root的值要比左子树上所有的数大 参考 # Definition for a binary tree node. # class TreeNode(object): # ...

  10. wordpress 更新时需要FTP 服务器账户密码的解决方法

    首先进入 apache下的wordpress 的目录下 (这是apache服务器默认安装的路径) cd /var/www 接着,给html整个文件夹进行赋值权限,如果不赋予权限 更新的时候会报权限不足 ...