結果

問題 No.3448 ABBBBBBBBC
コンテスト
ユーザー 👑 potato167
提出日時 2025-12-28 06:54:32
言語 PyPy3
(7.3.17)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 1,490 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 667 ms
コンパイル使用メモリ 82,556 KB
実行使用メモリ 102,716 KB
最終ジャッジ日時 2026-02-20 20:51:34
合計ジャッジ時間 2,248 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 4
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys

def solve():
    it = iter(sys.stdin.buffer.read().split())
    T = int(next(it))
    out = []

    for _ in range(T):
        N = int(next(it))
        K = int(next(it))
        k0 = K - 1  # 0-index

        per_a = 72 * N          # 9 * (8N)
        a_idx = k0 // per_a     # 0..8
        a = a_idx + 1
        k0 %= per_a

        per_ab = 8 * N
        b_idx = k0 // per_ab    # 0..8
        r = k0 % per_ab         # rank inside fixed (a,b)

        # b: the b_idx-th digit in 0..9 excluding a
        cnt = 0
        b = -1
        for d in range(10):
            if d == a:
                continue
            if cnt == b_idx:
                b = d
                break
            cnt += 1

        # digits excluding a and b, split by <b and >b
        low = []
        high = []
        for d in range(10):
            if d == a or d == b:
                continue
            if d < b:
                low.append(d)
            elif d > b:
                high.append(d)

        L = len(low)
        H = len(high)  # = 8 - L

        if L > 0 and r < L * N:
            t = r // L              # 0..N-1
            c = low[r % L]
        else:
            r2 = r - L * N          # if L==0, this is r
            # H > 0 always holds
            t = (N - 1) - (r2 // H) # descending in t
            c = high[r2 % H]

        length = t + 3
        out.append(f"{length} {a} {b} {c}")

    sys.stdout.write("\n".join(out))

if __name__ == "__main__":
    solve()
0