Skip to content

Commit 5aabca5

Browse files
committed
CF768B
1 parent 03de25d commit 5aabca5

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

main/700-799/768B.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
. "fmt"
5+
"io"
6+
"math/bits"
7+
)
8+
9+
// github.com/EndlessCheng/codeforces-go
10+
func CF768B(in io.Reader, out io.Writer) {
11+
var n, l, r uint64
12+
Fscan(in, &n, &l, &r)
13+
tot := (uint64(1)<<bits.Len64(n) - 1) >> 1
14+
f := func(m uint64) (res uint64) {
15+
for n, tot := n, tot; m > 0; n >>= 1 {
16+
if m >= tot {
17+
m -= tot
18+
res += n / 2
19+
if m > 0 {
20+
m--
21+
res += n & 1
22+
}
23+
}
24+
tot >>= 1
25+
}
26+
return
27+
}
28+
Fprint(out, f(r)-f(l-1))
29+
}
30+
31+
//func main() { CF768B(os.Stdin, os.Stdout) }

main/700-799/768B_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// https://codeforces.com/problemset/problem/768/B
9+
// https://codeforces.com/problemset/status/768/problem/B
10+
func TestCF768B(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
7 2 5
15+
outputCopy
16+
4
17+
inputCopy
18+
10 3 10
19+
outputCopy
20+
5`
21+
testutil.AssertEqualCase(t, rawText, 0, CF768B)
22+
}

0 commit comments

Comments
 (0)