-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGeekinaHate1s.java
47 lines (46 loc) · 1.29 KB
/
GeekinaHate1s.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution{
public long findNthNumer(int n, int k){
// Code Here.
long low = 0, high = (long)(1e18);
dp = new Long[2][65][65];
while(low <= high){
long mid = low + (high - low) / 2;
long count = find(mid, k);
if(count >= n)
high = mid - 1;
else
low = mid + 1;
}
return low;
}
private long find(long n, int k){
char s[] = Long.toBinaryString(n).toCharArray();
reset();
return dp(s, s.length, 1, k);
}
private long dp(char s[], int n, int tight, int k){
if(k < 0)
return 0;
if(n == 0){
return 1;
}
if(dp[tight][k][n] != null)
return dp[tight][k][n];
int ub = (tight == 1 ? (int)(s[s.length - n] - '0') : 1);
long ans = 0;
for(int dig = 0; dig <= ub; dig++){
if(dig == ub)
ans += dp(s, n - 1, tight, k - dig);
else
ans += dp(s, n - 1, 0, k - dig);
}
return dp[tight][k][n] = ans;
}
private void reset(){
for(int i = 0; i < 65; i++){
Arrays.fill(dp[0][i], null);
Arrays.fill(dp[1][i], null);
}
}
private Long dp[][][];
}