結果

問題 No.3501 Digit Products 2
コンテスト
ユーザー Nattt
提出日時 2026-04-17 23:19:15
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 1,690 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 736 ms
コンパイル使用メモリ 20,696 KB
実行使用メモリ 35,340 KB
平均クエリ数 10.47
最終ジャッジ日時 2026-04-17 23:19:58
合計ジャッジ時間 13,804 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 72
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys

def solve():
    first_line = sys.stdin.readline()
    if not first_line:
        return
    N = int(first_line.strip())
    
    P = [0] * (N - 1)
    for i in range(N - 1):
        print(f"? {i} {N - 1}", flush=True)
        resp = int(sys.stdin.readline().strip())
        if resp == -1:
            sys.exit(0)
        P[i] = resp

    candidates = []
    for c in range(1, 10):
        possible = True
        for i in range(N - 1):
            if P[i] % c != 0 or not (0 <= P[i] // c <= 9):
                possible = False
                break
        if possible:
            candidates.append(c)

    if not candidates:
        print("! -1", flush=True)
        return
        
    if len(candidates) == 1:
        c = candidates[0]
        ans = [str(c)] + [str(P[i] // c) for i in range(N - 2, -1, -1)]
        print(f"! {''.join(ans)}", flush=True)
        return

    u, v = -1, -1
    for i in range(N - 1):
        for j in range(i + 1, N - 1):
            if P[i] > 0 and P[j] > 0:
                u, v = i, j
                break
        if u != -1:
            break
            
    if u != -1 and v != -1:
        print(f"? {u} {v}", flush=True)
        Q = int(sys.stdin.readline().strip())
        if Q == -1:
            sys.exit(0)
            
        filtered = [c for c in candidates if (P[u] // c) * (P[v] // c) == Q]
        
        if len(filtered) == 1:
            c = filtered[0]
            ans = [str(c)] + [str(P[i] // c) for i in range(N - 2, -1, -1)]
            print(f"! {''.join(ans)}", flush=True)
        else:
            print("! -1", flush=True)
    else:
        print("! -1", flush=True)

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