Codeforces 431 D. Random Task 数位dp单调性

很巧妙的单调性......

n,n+1,n+2,.....2*n-2

   n+1,n+2,.....2*n-2,2*n-1,2*n

中间一段是相同的,n和2*n里的1是一样多的所以只有2*n-1不一样.....这是满足单调性的(monotone)


然后就是数位DP了.....



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 exactlyk 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 ≤ 10181 ≤ k ≤ 64).

Output

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



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>

using namespace std;

long long dp[105][105];
long long m;
int k;

int num[105];
long long DFS(int pos, int sum1,bool limit)
{
    if(pos == -1)
    {
        if(sum1==k) return 1;
        else return 0;
    }
    if(limit == 0 && dp[pos][sum1] != -1)
        return dp[pos][sum1];

   long long sum = 0;
    int up = limit ? num[pos] : 1;

    for( int i=0; i<=up; i++ )
    {
        int tp=sum1;
        if(i==1)
            tp++;
        sum += DFS(pos - 1, tp , limit && i == num[pos]);
    }

    if(limit == 0) dp[pos][sum1] = sum;
    return sum;
}

long long Solve(long long n)
{
    int kk = 0;
    while(n)
    {
        num[kk++] = n % 2;
        n /= 2;
    }
    return DFS(kk-1,0,1);
}

long long bin()
{
    long long l=1,r=1e18;
    while(l<r)
    {
        long long mid=(l+r)/2;
        long long tans=Solve(2*mid)-Solve(mid);
        if(tans==m)
            return mid;
        else if(tans>m)
            r=mid-1;
        else
            l=mid+1;
    }
}

int main()
{
    while(~scanf("%lld%d",&m,&k))
    {
        memset(dp,-1,sizeof(dp));
        long long ans=bin();
        printf("%lld\n",ans);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值