Test instructions
Given M,k
0 <= M <= 10^18, 1 <= k <= 64
Find a number n, satisfy the n+1,n+2,... n+n This n number, just has the m number of 2 binary notation just have K 1
Ensure that the answer is within 10^18
Ideas:
Obviously
For x, if x+1,x+2,..., x+x has a y number of K 1
For X+1, the x+2,x+3,..., x+x+2 has a number of K 1 and >= y
To satisfy Monotonicity, consider two points:
L = M,r = 10^18
So the question becomes: given a number x,x+1,x+2,..., the number of K-1 in X+x is exactly how many
For ease of expression, assume that X is a 2 binary number:
Then the range of K 1 appears:
[X,11...111],[10...000,2X]
Which is divided into 2 segments according to the number of digits.
function Go (LL x,int k) function: For [x,1111111] This interval has K 1 number of how many
Then ans = Go (x,k) + Go (10...000,k)-Go ((x<<1) &1,k)
Judging the relationship between ANS and M can reduce the range of l,r.
Attention:
For 2^x (1 << x), this time (1 << x) is the default int type,
So if it's super int, use (1LL << x)
Code:
//File Name:cf431D.cpp//Author:long//Mail: [email protected]//Created time:2016 July 09 Saturday 21:34 35 seconds#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#defineLL Long Longusing namespacestd;inta[ -],tot; LL f[ -][ -];voidinit () {memset (F,0,sizeoff); for(intI=0;i< -; i++) {f[i][0] =1; for(intj=1; j<=i;j++) F[i][j]= f[i-1][J] + f[i-1][j-1]; }}ll Go (LL x,intk) {Tot=0; while(x) {a[++tot] = x%2; X>>=1; } LL ans=0; intPre =0; for(intI=tot;i>0; i--){ if(A[i] = =1) Pre++; Else{ if(K-pre-1>=0) ans+ = f[i-1][k-pre-1]; Else Break; } } if(Pre = =k) Ans++; returnans;} LLGet(LL x,intk) {LL ans=Go (x,k); LL y= (1LL <<tot); //printf ("x =%lld y =%lld\n", x, y);Ans = ans + go (y,k)-Go (2* x +1, K); returnans;} ll solve (ll M,intk) {LL L= M,r = (LL) 1e18 +1, Mid; while(R-l >1) {Mid= (L + r) >>1; LL cur=Get(MID,K); //printf ("mid =%lld cur =%lld\n", mid,cur); if(cur <=m) L=mid; ElseR=mid; } if(Get(l,k) = =m)returnl; Else returnR;}intMain () {init (); LL m; intK; CIN>> m >>K; cout<< Solve (m,k) <<Endl; return 0;}
CF 431 D. Random Task Combination Math