题意:

给定m,k

0 <= m <= 10^18 ,1 <= k <= 64

求一个数n,满足n+1,n+2,...n+n这n个数中,刚好有m个数的2进制表示法刚好有k个1

保证答案在10^18内

思路:

显然,

对于x,如果x+1,x+2,...,x+x有y个数有k个1

对于x+1,则x+2,x+3,...,x+x+2有k个1的数的个数 >= y

满足单调性,考虑二分:

L = m,r = 10^18

那么问题变为:给定一个数x,x+1,x+2,...,x+x中刚好有k个1的数有多少个

为了方便表达,假设x是2进制数:

则出现k个1的区间有:

[x,11...111],[10...000,2x]

即按照位数分成2段计算

函数go(LL x,int k)作用:求[x,1111111]这个区间中有k个1的数有多少个

则ans = go(x,k) + go(10...000,k) - go((x<<1)&1,k)

判断ans与m的关系就可以不断缩小L,R的范围啦

注意:

求2^x如果用(1 << x)的话,这个时候(1 << x)是默认为int型的,

所以如果超int了,要用(1LL << x)

代码:

  //File Name: cf431D.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年07月09日 星期六 21时34分35秒 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream> #define LL long long using namespace std; int a[],tot;
LL f[][]; void init(){
memset(f,,sizeof f);
for(int i=;i<;i++){
f[i][] = ;
for(int j=;j<=i;j++)
f[i][j] = f[i-][j] + f[i-][j-];
}
} LL go(LL x,int k){
tot = ;
while(x){
a[++tot] = x % ;
x >>= ;
}
LL ans = ;
int pre = ;
for(int i=tot;i>;i--){
if(a[i] == )
pre++;
else{
if(k - pre - >= )
ans += f[i-][k-pre-];
else
break;
}
}
if(pre == k)
ans++;
return ans;
} LL get(LL x,int k){
LL ans = go(x,k);
LL y = (1LL << tot);
//printf("x = %lld y = %lld\n",x,y);
ans = ans + go(y,k) - go( * x + ,k);
return ans;
} LL solve(LL m,int k){
LL l = m,r = (LL)1e18 + ,mid;
while(r - l > ){
mid = (l + r) >> ;
LL cur = get(mid,k);
//printf("mid = %lld cur = %lld\n",mid,cur);
if(cur <= m)
l = mid;
else
r = mid;
}
if(get(l,k) == m)
return l;
else
return r;
} int main(){
init();
LL m;
int k;
cin >> m >> k;
cout << solve(m,k) << endl;
return ;
}

codeforces 431 D. 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 558E : A Simple Task

    E. A Simple Task Problem's Link: http://codeforces.com/problemset/problem/558/E Mean: 给定一个字符串,有q次操作, ...

  3. codeforces 70D Professor's task(动态二维凸包)

    题目链接:http://codeforces.com/contest/70/problem/D Once a walrus professor Plato asked his programming ...

  4. Codeforces 558E A Simple Task (计数排序&&线段树优化)

    题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...

  5. codeforces 932E Team Work(组合数学、dp)

    codeforces 932E Team Work 题意 给定 \(n(1e9)\).\(k(5000)\).求 \(\Sigma_{x=1}^{n}C_n^xx^k\). 题解 解法一 官方题解 的 ...

  6. 【CODEFORCES】 B. Random Teams

    B. Random Teams time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. CF431D Random Task 二分+数位dp

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

  8. Codeforces C. A Simple Task(状态压缩dp)

    题目描述:  A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. Codeforces 840C 题解(DP+组合数学)

    题面 传送门:http://codeforces.com/problemset/problem/840/C C. On the Bench time limit per test2 seconds m ...

随机推荐

  1. 如何读懂 Intel HEX 文件

    什么是 Intel HEX 文件格式   转自:http://www.cnblogs.com/imapla/archive/2013/03/16/2926133.htmlIntel HEX 文件是遵循 ...

  2. linux操作文本文件

    打开文件 #vi 文件名 保存退出 从编辑模式退到命令行模式,按esc键 保存退出命令 #:wq 强制退出,不保存 #:q!

  3. 请让页面中的一个元素(10px*10px)围绕坐标(200, 300) 做圆周运动

    <!DOCTYPE html> <html> <head> <title>Making things move</title> <me ...

  4. 免费web api接口大全

    下面的接口来自互联网,部分功能需要付费 查询手机 http://www.yodao.com/smartresult-xml/search.s?type=mobile&q=手机号码 查询IP h ...

  5. mvn使用问题

    http://mirrors.ibiblio.org/maven2/org/apache/maven/archetypes/ http://blog.csdn.net/u011340807/artic ...

  6. jQuery插件 -- 表单验证插件jquery.validate.js, jquery.metadata.js

    原文地址:http://blog.csdn.net/zzq58157383/article/details/7718352   最常使用JavaScript的场合就是表单的验证,而jQuery作为一个 ...

  7. Java 实现奇数阶幻方的构造

    一.设计的流程图如下所示 二.Java 语言的代码实现 package MagicSquare; //奇数幻方的实现 public class Magic_Odd { //n 为幻方的阶数 publi ...

  8. shell知识点

    各个项目以实践为主.原理及更多细节介绍,请查看官方文档: 例如:bash,grub,postfix,pam,fastcgi,httpd,rsync等诸多项目. 各种总结表格 http://www.cn ...

  9. 在没有spineRunTime情况下手动使用spine数据

    用spine做了一些简单UI动画,结果发现游戏用的cocos2dx版本太低了,无法集成spineRunTime,无奈只好把spine记下的关键帧数据抄到一个txt文件中,然后读取txt,再自己写代码播 ...

  10. 根据N种规格中的M种规格值生成的全部规格组合的一种算法

    近来在开发SKU模块的时候,遇到这样一个需求,某种商品有N(用未知数N来表示是因为规格的数组由用户制定且随时可以编辑的,所以对程序来说,它是一个未知数)类规格,每一类规格又有M个规格值,各种规格值的组 ...