UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#211719#2909. numberLZDQ00ms0kbPython32.0kb2024-09-13 10:56:562024-09-13 10:56:57

answer

import sys
import threading

def main():
    import sys
    # sys.setrecursionlimit(1 << 25)
    q = int(sys.stdin.readline())
    queries = []
    y_max = 0
    for _ in range(q):
        x_str, y_str = sys.stdin.readline().split()
        x = int(x_str)
        y = int(y_str)
        queries.append((x, y))
        y_max = max(y_max, y)
    y_max = min(y_max, 10**5)  # Since y ≤ 1e7

    MAX_X = y_max + 10000  # Slightly larger to avoid index issues
    MAX_K = 24  # Since 2^24 > 1e7, adjust as needed
    INF = MAX_X + 100000

    # Precompute sum of digits
    s = [0] * (MAX_X + 1)
    for i in range(1, MAX_X + 1):
        s[i] = s[i // 10] + i % 10

    # Precompute next[0][x] = x + s(x)
    next_jump = [[0] * (MAX_X + 1) for _ in range(MAX_K + 1)]
    for x in range(1, MAX_X + 1):
        nx = x + s[x]
        if nx <= MAX_X:
            next_jump[0][x] = nx
        else:
            next_jump[0][x] = INF

    # Build jump table
    for k in range(1, MAX_K + 1):
        for x in range(1, MAX_X + 1):
            nx = next_jump[k - 1][x]
            if nx <= MAX_X:
                next_jump[k][x] = next_jump[k - 1][nx]
            else:
                next_jump[k][x] = INF

    # Answer queries
    for x, y in queries:
        curr_x = x
        if curr_x > MAX_X:
            # x is beyond precomputed range
            # Simulate steps until x > y
            while curr_x <= y:
                curr_s = sum(map(int, str(curr_x)))
                curr_x += curr_s
            print(curr_x)
            continue

        # Binary lifting
        for k in range(MAX_K, -1, -1):
            if next_jump[k][curr_x] <= y:
                curr_x = next_jump[k][curr_x]

        # Simulate remaining steps
        while curr_x <= y:
            if curr_x + s[curr_x] > y:
                curr_x += s[curr_x]
                break
            else:
                curr_x += s[curr_x]
        print(curr_x)

# threading.Thread(target=main).start()
main()

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

input:

500000
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 ...

output:


result:


Subtask #2:

score: 0
Skipped

Subtask #3:

score: 0
Time Limit Exceeded

Test #4:

score: 0
Time Limit Exceeded

input:

50
4587480273 4587480273
428862505 500400481
6920415626 7358620174
7787875953 7787884613
4542304779 ...

output:


result:


Subtask #4:

score: 0
Skipped