結果

問題 No.691 E869120 and Constructing Array 5
ユーザー lam6er
提出日時 2025-04-15 23:43:06
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,258 bytes
コンパイル時間 664 ms
コンパイル使用メモリ 82,016 KB
実行使用メモリ 86,696 KB
最終ジャッジ日時 2025-04-15 23:46:18
合計ジャッジ時間 29,294 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other WA * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from decimal import Decimal, getcontext, ROUND_HALF_UP

getcontext().prec = 50

def process_query(s):
    # Check if it's an integer by looking at decimal part
    if '.' in s:
        parts = s.split('.')
        decimal_part = parts[1]
        if all(c == '0' for c in decimal_part):
            P = int(parts[0])
            return f"1 {P * P}"
    
    P = Decimal(s)
    # Check if P squared is close to an integer
    x_squared = P ** 2
    e = x_squared.quantize(Decimal('1'), rounding=ROUND_HALF_UP)
    sqrt_e = e.sqrt()
    error = abs(sqrt_e - P)
    if error <= Decimal('1e-10'):
        return f"1 {e.to_eng_string()}"
    else:
        # Three-term approach
        K = Decimal('1') + Decimal('1.5').sqrt() + Decimal('2').sqrt()
        m = P / K
        x = (m ** 2).quantize(Decimal('1'), rounding=ROUND_HALF_UP)
        # Ensure x is even
        if x % 2 != 0:
            x += 1 if (x % 2 == 1) else -1
        e1 = x
        e2 = (x * Decimal('3') / Decimal('2')).quantize(Decimal('1'), rounding=ROUND_HALF_UP)
        e3 = x * 2
        return f"3 {e1.to_eng_string()} {e2.to_eng_string()} {e3.to_eng_string()}"

Q = int(sys.stdin.readline())
for _ in range(Q):
    s = sys.stdin.readline().strip()
    print(process_query(s))
0