[Python] Special Event (Python) [closed]

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    Given three positive integers n, m, and t, where n represents the upper limit of a sequence of numbers (1 to n), m is a divisor, and t is a target position, determine the number that would be found at the t-th position after sorting the numbers 1 to n based on their remainders when divided by m. The sorting order is: first by remainder (0, 1, 2, ..., m-1), and within each remainder group, in ascending order of the original number."

    Example:

    "If n = 10, m = 4, and t = 8, the sorted sequence would be [4, 8, 1, 5, 9, 2, 6, 10, 3, 7].
    Because:
    Number that can be divided by m: 4, 8
    Number that when divide by m has a remaining of 1: 1, 5, 9
    Number that when divide by m has a remaining of 3: 3, 7
    Therefore, the number at the 8th position is 10."

    "How can I efficiently find the number at the t-th position in a sequence sorted by remainders? I need a solution that works for large values of n (up to 10^16) and m (up to 10^6) in Python, while respecting a time limit of 8 seconds. The sorting criteria are: first by the remainder when divided by m, and then in ascending order within each remainder group."

    Ok so i have try another the one and it's pretty fast but it fail one test case:
    Input:
    768767
    123
    6250
    Output:
    768750
    And my output was 768727
    This is the code

    def specialEvent(n, m, t):
    counts = [0] * m
    for i in range(1, m + 1):
    counts[i % m] = (n - i) // m + 1

    group_index = 0
    while t > counts[group_index]:
    t -= counts[group_index]
    group_index += 1

    result = group_index + (t - 1) * m
    return result

    Continue reading...

Compartilhe esta Página