forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
431D.go
70 lines (66 loc) · 1.21 KB
/
431D.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
. "fmt"
"io"
"strconv"
)
// github.com/EndlessCheng/codeforces-go
func CF431D(in io.Reader, out io.Writer) {
search := func(l, r int64, f func(int64) bool) int64 {
for l < r {
m := (l + r) >> 1
if f(m) {
r = m
} else {
l = m + 1
}
}
return l
}
var tar int64
var k int
Fscan(in, &tar, &k)
calc := func(s string) int64 {
const lowerC, upperC byte = '0', '1'
n := len(s)
dp := make([][]int64, n)
for i := range dp {
dp[i] = make([]int64, k+1)
for j := range dp[i] {
dp[i][j] = -1
}
}
var f func(p, c1 int, limitUp bool) int64
f = func(p, c1 int, limitUp bool) (res int64) {
if c1 > k {
return
}
if p == n {
if c1 == k {
return 1
}
return
}
if !limitUp {
dv := &dp[p][c1]
if *dv >= 0 {
return *dv
}
defer func() { *dv = res }()
}
up := upperC
if limitUp {
up = s[p]
}
for d := lowerC; d <= up; d++ {
res += f(p+1, c1+int(d&1), limitUp && d == up)
}
return
}
return f(0, 0, true)
}
Fprint(out, search(1, 1e18, func(n int64) bool {
return calc(strconv.FormatInt(n*2, 2))-calc(strconv.FormatInt(n, 2)) >= tar
}))
}
//func main() { CF431D(os.Stdin, os.Stdout) }