problem A
Turtle and Piggy Are Playing a Game
Turtle and Piggy are playing a number game.
First, Turtle will choose an integer 𝑥, such that 𝑙≤𝑥≤𝑟, where 𝑙,𝑟 are given. It’s also guaranteed that 2𝑙≤𝑟.
Then, Piggy will keep doing the following operation until 𝑥 becomes 1:
- Choose an integer 𝑝 such that 𝑝≥2 and 𝑝∣𝑥 (i.e. 𝑥 is a multiple of 𝑝).
- Set 𝑥 to \(\tfrac{𝑥}{𝑝}\), and the score will increase by 1.
The score is initially 0. Both Turtle and Piggy want to maximize the score. Please help them to calculate the maximum score.
Input
Each test contains multiple test cases. The first line contains the number of test cases \(𝑡 (1≤𝑡≤10^4)\). The description of the test cases follows.
The first line of each test case contains two integers \(𝑙,𝑟 (1≤𝑙≤𝑟≤10^9,2𝑙≤𝑟)\) — The range where Turtle can choose the integer from.
Output
For each test case, output a single integer — the maximum score.
Example
input
5
2 4
3 6
2 15
6 22
114514 1919810
output
2
2
3
4
20
Note
In the first test case, Turtle can choose an integer 𝑥, such that 2≤𝑥≤4. He can choose 𝑥=4. Then Piggy can choose 𝑝=2 for 2 times. After that, 𝑥 will become 1, and the score will be 2, which is maximized.
In the second test case, Turtle can choose an integer 3≤𝑥≤6. He can choose 𝑥=6. Then Piggy can choose 𝑝=2, then choose 𝑝=3. After that, 𝑥 will become 1, and the score will be 2, which is maximum.
In the third test case, Turtle can choose 𝑥=12.
In the fourth test case, Turtle can choose 𝑥=16.
solution 1
fun main() {
repeat(readln().toInt()) {
println(solve())
}
}
fun solve(): Int {
val (_, r) = readln().split(' ').map(String::toInt)
return 31 - r.countLeadingZeroBits()
}
solution 2
fun main() {
repeat(readln().toInt()) {
readln().split(' ')
.map(String::toInt)
.let { (_, r) -> 31 - r.countLeadingZeroBits() }
.apply(::println)
}
}
problem B
Turtle and an Infinite Sequence
There is a sequence \(𝑎_0,𝑎_1,𝑎_2,…\) of infinite length. Initially \(𝑎_𝑖=𝑖\) for every non-negative integer 𝑖.
After every second, each element of the sequence will simultaneously change. 𝑎𝑖 will change to \(𝑎_{𝑖−1}∣𝑎_𝑖∣𝑎_{𝑖+1}\) for every positive integer 𝑖. \(𝑎_0\) will change to \(𝑎_0∣𝑎_1\). Here, \(∣\) denotes bitwise OR.
Turtle is asked to find the value of 𝑎𝑛 after 𝑚 seconds. In particular, if 𝑚=0, then he needs to find the initial value of 𝑎𝑛. He is tired of calculating so many values, so please help him!
Input
Each test contains multiple test cases. The first line contains the number of test cases \(𝑡 (1≤𝑡≤10^4)\). The description of the test cases follows.
The first line of each test case contains two integers \(𝑛,𝑚 (0≤𝑛,𝑚≤10^9)\).
Output
For each test case, output a single integer — the value of \(𝑎_𝑛\) after 𝑚 seconds.
Example
input
9
0 0
0 1
0 2
1 0
5 2
10 1
20 3
1145 14
19198 10
output
0
1
3
1
7
11
23
1279
19455
Note
After 1 second, \([𝑎_0,𝑎_1,𝑎_2,𝑎_3,𝑎_4,𝑎_5]\) will become [1,3,3,7,7,7].
After 2 seconds, \([𝑎_0,𝑎_1,𝑎_2,𝑎_3,𝑎_4,𝑎_5]\) will become [3,3,7,7,7,7].
solution 1
fun main() {
repeat(readln().toInt()) {
println(solve())
}
}
fun solve(): Int {
val (n, m) = readln().split(' ').map(String::toInt)
val cnt = 32 - (n + m).countLeadingZeroBits()
var ans = n
repeat(cnt) {
if ((n + m) % (1.shl(it)) < 2 * m) {
ans = ans or 1.shl(it)
}
}
return ans
}
solution 2
fun main() {
repeat(readln().toInt()) {
println(solve())
}
}
fun solve(): Int {
val (n, m) = readln().split(' ').map(String::toInt)
var l = n.minus(m).coerceAtLeast(0)
var r = n + m
var cnt = 0
while (l != r) {
cnt++
l = l.shr(1)
r = r.shr(1)
}
repeat(cnt) {
r = r.shl(1).xor(1)
}
return r
}
solution 3
fun main() {
repeat(readln().toInt()) {
println(solve())
}
}
fun solve(): Int {
val (n, m) = readln().split(' ').map(String::toInt)
val l = n.minus(m).coerceAtLeast(0)
val r = n + m
(30 downTo 0).forEach {
if (l.xor(r).and(1.shl(it)) != 0) {
return l.or(r).or(1.shl(it) - 1)
}
}
return l.or(r)
}
solution 4
fun main() {
repeat(readln().toInt()) {
println(solve())
}
}
fun solve(): Int {
val (n, m) = readln().split(' ').map(String::toInt)
if (m == 0) return n
val l = n.minus(m).coerceAtLeast(0)
val r = n + m
val cnt = l.xor(r).countLeadingZeroBits()
return l.or(r).or(Int.MAX_VALUE.shr(cnt))
}