avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

964E

  2024-08-07        
字数统计: 527字   |   阅读时长: 3min

Question:

E. Triple Operations
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

On the board Ivy wrote down all integers from ll to rr, inclusive.

In an operation, she does the following:

  • pick two numbers xx and yy on the board, erase them, and in their place write the numbers 3x3x and ⌊y3⌋⌊y3⌋. (Here ⌊∙⌋⌊∙⌋ denotes rounding down to the nearest integer).

What is the minimum number of operations Ivy needs to make all numbers on the board equal 00? We have a proof that this is always possible.

Input

The first line contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The only line of each test case contains two integers ll and rr (1≤l<r≤2⋅1051≤l<r≤2⋅105).

Output

For each test case, output a single integer — the minimum number of operations needed to make all numbers on the board equal 00.

Example
Input
Copy
4
1 3
2 4
199999 200000
19 84
Output
Copy
5
6
36
263
Note

In the first test case, we can perform 55 operations as follows:

1,2,3−→−−−−x=1,y=23,0,3−→−−−−x=0,y=31,0,3−→−−−−x=0,y=31,0,1−→−−−−x=0,y=10,0,1−→−−−−x=0,y=10,0,0.1,2,3→x=1,y=23,0,3→x=0,y=31,0,3→x=0,y=31,0,1→x=0,y=10,0,1→x=0,y=10,0,0.

[964E.md](https://codeforces.com/contest/1999/problem/E)

My think:

At first, I thought it was a simple simulation problem: calculate the number of times x when the smallest
number divided by 3 equals 0, and then the answer would be x * 2 plus the number of remaining times when
the left number divided by 3 equals 0. However, this approach is incorrect.

By utilizing the characteristics of exponential growth and performing some simple
calculations, we can quickly determine the minimum number of operations within a given range.
This method has a low time complexity and can efficiently handle large input ranges.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import math
t = int(input())
for i in range(t):
num = list(map(int, input().split()))
l = num[0]
r = num[1]
start_pow3 = 1
cnt_start = 1
while start_pow3 <= (l / 3):
start_pow3 *= 3
cnt_start += 1
ans = cnt_start
pow3 = start_pow3
next_pow3 = start_pow3 * 3
while pow3 <= r:
ans += (min(r + 1, next_pow3) - max(l, pow3)) * cnt_start
cnt_start += 1
pow3 *= 3
next_pow3 *= 3
print(ans)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def min_operations_to_zero(n):
operations = 0
while n > 0:
n = n // 3
operations += 1
return operations

t = int(input())
results = []
for _ in range(t):
l, r = map(int, input().split())
total_operations = 0
oper = min_operations_to_zero(l)
for n in range(l+1, r+1):
total_operations += min_operations_to_zero(n)
total_operations += oper*2
results.append(total_operations)

for result in results:
print(result)
  • Python
  • Answer

扫一扫,分享到微信

微信分享二维码
Top of an article
994.Rotten orange
目录
  1. 1. Question:
  2. 2. My think:
  3. 3. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号