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 k digits 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.

Examples
Input

Copy
1 1
Output

Copy
1
Input

Copy
3 2
Output

Copy
5

抱歉,我太菜了,只会二分来数位dp解决;
貌似有题解是用组合数学解决的,orz ;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream> //#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++) inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
}
/*
int n, m;
int st, ed;
struct node {
int u, v, nxt, w;
}edge[maxn<<1]; int head[maxn], cnt; void addedge(int u, int v, int w) {
edge[cnt].u = u; edge[cnt].v = v; edge[cnt].w = w;
edge[cnt].nxt = head[u]; head[u] = cnt++;
} int rk[maxn]; int bfs() {
queue<int>q;
ms(rk);
rk[st] = 1; q.push(st);
while (!q.empty()) {
int tmp = q.front(); q.pop();
for (int i = head[tmp]; i != -1; i = edge[i].nxt) {
int to = edge[i].v;
if (rk[to] || edge[i].w <= 0)continue;
rk[to] = rk[tmp] + 1; q.push(to);
}
}
return rk[ed];
}
int dfs(int u, int flow) {
if (u == ed)return flow;
int add = 0;
for (int i = head[u]; i != -1 && add < flow; i = edge[i].nxt) {
int v = edge[i].v;
if (rk[v] != rk[u] + 1 || !edge[i].w)continue;
int tmpadd = dfs(v, min(edge[i].w, flow - add));
if (!tmpadd) { rk[v] = -1; continue; }
edge[i].w -= tmpadd; edge[i ^ 1].w += tmpadd; add += tmpadd;
}
return add;
}
ll ans;
void dinic() {
while (bfs())ans += dfs(st, inf);
}
*/ ll dp[100][100], m;
int num[100], len, k; ll dfs(int pos, int limit, int sum) {
if (pos < 0)return sum == k;
if (!limit&&dp[pos][sum] != -1)return dp[pos][sum];
ll ans = 0;
int up = limit ? num[pos] : 1;
for (int i = 0; i <= up; i++) {
ans += dfs(pos - 1, limit&&i == up, sum + (i == 1));
}
if (!limit)dp[pos][sum] = ans;
return ans;
} ll sol(ll x) {
len = 0;
while (x) {
num[len++] = x % 2; x /= 2;
}
return dfs(len - 1, 1, 0);
} int main()
{
//ios::sync_with_stdio(0);
//memset(head, -1, sizeof(head));
while (cin >> m >> k) {
ll l = 1, r = 1000000000000000000;
memset(dp, -1, sizeof(dp));
while (l <= r) {
ll mid = (l + r) / 2;
ll res = sol(2 * mid) - sol(mid);
if (res == m) {
l = mid; break;
}
else if (res < m)l = mid + 1;
else r = mid - 1;
}
cout << l << endl;
}
return 0;
}

CF431D Random Task 二分+数位dp的更多相关文章

  1. Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)

    题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. POJ3208 Apocalypse Someday(二分 数位DP)

    数位DP加二分 //数位dp,dfs记忆化搜索 #include<iostream> #include<cstdio> #include<cstring> usin ...

  3. shuoj 1 + 2 = 3? (二分+数位dp)

    题目传送门 1 + 2 = 3? 发布时间: 2018年4月15日 22:46   最后更新: 2018年4月15日 23:25   时间限制: 1000ms   内存限制: 128M 描述 埃森哲是 ...

  4. hihocoder #1301 : 筑地市场 二分+数位dp

    #1301 : 筑地市场 题目连接: http://hihocoder.com/problemset/problem/1301 Description 筑地市场是位于日本东京都中央区筑地的公营批发市场 ...

  5. 2019.02.15 codechef Favourite Numbers(二分+数位dp+ac自动机)

    传送门 题意: 给444个整数L,R,K,nL,R,K,nL,R,K,n,和nnn个数字串,L,R,K,数字串大小≤1e18,n≤65L,R,K,数字串大小\le1e18,n\le65L,R,K,数字 ...

  6. CSP模拟赛 number (二分+数位DP)

    题面 给定整数m,km,km,k,求出最小和最大的正整数 nnn 使得 n+1,n+2,-,2nn+1,n+2,-,2nn+1,n+2,-,2n 中恰好有 mmm 个数 在二进制下恰好有 kkk 个 ...

  7. CodeChef FAVNUM FavouriteNumbers(AC自动机+数位dp+二分答案)

    All submissions for this problem are available. Chef likes numbers and number theory, we all know th ...

  8. hihocoder #1301 : 筑地市场 数位dp+二分

    题目链接: http://hihocoder.com/problemset/problem/1301?sid=804672 题解: 二分答案,每次判断用数位dp做. #include<iostr ...

  9. Luogu2022 有趣的数-二分答案+数位DP

    Solution 我好像写了一个非常有趣的解法233, 我们可以用数位$DP$ 算出比$N$小的数中 字典序比 $X$ 小的数有多少个, 再和 $rank$进行比较. 由于具有单调性, 显然可以二分答 ...

随机推荐

  1. SUSE eth0 No such device

    删除 etc/udev/rules.d/70-persistent-net.rules 文件  之后重启让系统重新生成eth0配置文件 rm -f etc/udev/rules.d/70-persis ...

  2. rsync 简单使用 非默认ssh端口 分别从远程获取及推送本地的文件到远程

    rsync: did not see server greetingrsync error: error starting client-server protocol (code 5) at mai ...

  3. nfs cron shell 作业

    作业一: nginx反向代理三台web服务器,实现负载均衡 所有的web服务共享一台nfs的存储 2台服务器 nginx [lb] :101.200.206.6 nginx [web]:101.200 ...

  4. Don’t panic, it’s just a kernel panic (ZT)

    http://blog.kreyolys.com/2011/03/17/no-panic-its-just-a-kernel-panic/ One of the main young sysadmin ...

  5. 问题:Oracle 树形遍历;结果:使用oracle进行遍历树操作

    使用oracle进行遍历树操作   1:首先数据库中表必须是树形结构的 2:super_department_id 为 department_id 的父节点编号 3:以下语句的执行结果是:depart ...

  6. c# 代码调用ssis包

    https://docs.microsoft.com/en-us/sql/integration-services/run-manage-packages-programmatically/loadi ...

  7. select 动态添加option函数

    转自:https://lym6520.iteye.com/blog/309937 经常会用到select动态添加元素,写了个方法,方便调用!  ... /** * 功能:select对象动态添加Opt ...

  8. 部署和调优 1.7 samba 部署和优化-1

    Samba服务可以实现linux上共享一个目录,windows上面访问. 安装 yum install -y samba samba-client 配置文件在 vim /etc/samba/smb.c ...

  9. DAY20-Django之FileField与ImageField

    ImageField 和 FileField 可以分别对图片和文件进行上传到指定的文件夹中. 1. 在下面的 models.py 中 : picture = models.ImageField(upl ...

  10. DAY2-python数据类型、字符编码、文件处理

    阅读目录 一.引子 二.数字 三.字符串 四.列表 五.元祖 六.字典 七.集合 八.数据类型总结 九.运算符 十.字符编码 十一.文件处理 一.引子 1 什么是数据? x=10,10是我们要存储的数 ...