题目描述

求一个\(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. Cobbler自动装机

    preface 我们之前批量安装操作系统的时候都是采用pxe来安装,pxe也是通过网络安装操作系统的,但是PXE依赖于DHCP,HTTP/TFTP,kicstart等支持.安装流程如下所示: 对于上面 ...

  2. TTTTTTTTTTTTTTTTT CF #182 div1 B floyd

    题意: 有 n(3≤n≤100) 个站点,当第一次到达站点 u 的时候会增加寿命 au(1≤au≤103),题目给了 n 个站点的二位空间坐标,每两个站点之间的距离为曼哈顿距离(dis(i, j)=| ...

  3. Springboot 使用Jedis

    Springboot 使用Jedis 依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...

  4. TensorFlow使用记录 (三): Learning Rate Scheduling

    file: tensorflow/python/training/learning_rate_decay.py 参考:tensorflow中常用学习率更新策略 神经网络中通过超参数 learning ...

  5. 【转】有rand7(可以随机生成1到7的数据的随机函数),如何产生rand10(随机产生1-10的数)

    今天停GJP说在面试的时候碰到了一道这样的题目:有rand7(可以随机生成1到7的数据的随机函数),如何产生rand10(随机产生1-10的数) 感觉很有意思,找到了这篇博客,感觉解法很好玩,转载在这 ...

  6. vue中axios的封装(注意这里面异步的概念和用法十分重要)

    todo https://www.cnblogs.com/chaoyuehedy/p/9931146.html

  7. es6字符串的扩展——模板

    todo1.模板字符串 传统的 JavaScript 语言,输出模板通常是这样写的(下面使用了 jQuery 的方法). $('#result').append( 'There are <b&g ...

  8. 当 LAST_INSERT_ID() 带有参数时# 清空重来

    [root@yejr.me]> truncate table t; # 插入1条新记录[root@yejr.me]> insert into t select 0,rand()*1024; ...

  9. 后盾网lavarel视频项目---Laravel 安装代码智能提示扩展「laravel-ide-helper」

    后盾网lavarel视频项目---Laravel 安装代码智能提示扩展「laravel-ide-helper」 一.总结 一句话总结: laravel-ide-helper作用是:代码提示 larav ...

  10. nfs服务安装部署测试

    nfs:网络文件系统作用:某个文件或目录共享,使其它用户可以通过网络访问此共享目录或文件.***特别注意共享的目录权限1.使用nfs需要先安装 yum install -y nfs-utils rpc ...