結果

問題 No.3501 Digit Products 2
コンテスト
ユーザー praddumna12
提出日時 2026-04-18 04:33:47
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
WA  
実行時間 -
コード長 1,308 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 477 ms
コンパイル使用メモリ 20,700 KB
実行使用メモリ 35,468 KB
平均クエリ数 11.29
最終ジャッジ日時 2026-04-18 04:34:08
合計ジャッジ時間 17,567 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 22 WA * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
import math

input = sys.stdin.readline

N = int(input())

# Step 1: chain queries
p = [0]*(N-1)

for i in range(N-1):
    print(f"? {i} {i+1}", flush=True)
    p[i] = int(input())
    if p[i] == -1:
        exit()

# Step 2: extra query
print("? 0 2", flush=True)
q = int(input())
if q == -1:
    exit()

# Step 3: zero detection
if any(x == 0 for x in p) or q == 0:
    print("! -1", flush=True)
    exit()

# Step 4: compute d1
val = p[0] * p[1]

if val % q != 0:
    print("! -1", flush=True)
    exit()

d1_sq = val // q
d1 = int(math.isqrt(d1_sq))

if d1 * d1 != d1_sq or not (1 <= d1 <= 9):
    print("! -1", flush=True)
    exit()

# Step 5: reconstruct digits
d = [0]*N
d[1] = d1

# backward
if p[0] % d1 != 0:
    print("! -1", flush=True)
    exit()

d[0] = p[0] // d1

# forward
for i in range(1, N-1):
    if p[i] % d[i] != 0:
        print("! -1", flush=True)
        exit()
    d[i+1] = p[i] // d[i]

# Step 6: validate digits
for x in d:
    if not (0 <= x <= 9):
        print("! -1", flush=True)
        exit()

# Step 7: final consistency check
for i in range(N-1):
    if d[i] * d[i+1] != p[i]:
        print("! -1", flush=True)
        exit()

if d[0] * d[2] != q:
    print("! -1", flush=True)
    exit()

# Step 8: output answer
print("!", "".join(map(str, d)), flush=True)
0