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
output
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 + 1n + 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.

Input

The first line contains two space-separated integers, m and k (0 ≤ m ≤ 10181 ≤ k ≤ 64).

Output

Print the required number n (1 ≤ n ≤ 1018). If there are multiple answers, print any of them.

Sample test(s)
input
1 1
output
1
input
3 2
output
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;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值