結果

問題 No.2368 I love a square root of 2
ユーザー gew1fw
提出日時 2025-06-12 16:56:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,096 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 59,776 KB
最終ジャッジ日時 2025-06-12 16:56:39
合計ジャッジ時間 2,588 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

def find_a_b(N):
    if N == 1:
        return (0, 0)
    
    sqrt2 = math.sqrt(2)
    
    # Binary search for X
    low = 0.0
    high = math.sqrt(2 * sqrt2 * N) * 2  # Initial high value
    
    def count_elements(X):
        a_max = int(X)
        total = 0
        for a in range(a_max + 1):
            max_b = int( (X - a) / sqrt2 )
            total += max_b + 1
        return total
    
    # Binary search to find the minimal X where count >= N
    for _ in range(100):  # Sufficient iterations for precision
        mid = (low + high) * 0.5
        cnt = count_elements(mid)
        if cnt >= N:
            high = mid
        else:
            low = mid
    X = high
    
    # Now find a and b such that X = a + sqrt(2)*b
    a_max = int(X)
    for a in range(a_max + 1):
        b_val = (X - a) / sqrt2
        if abs(b_val - round(b_val)) < 1e-9:  # Check if b_val is integer
            b = int(round(b_val))
            return (a, b)
    return (0, 0)  # Shouldn't reach here

# Read input
N = int(input())

# Find a and b
a, b = find_a_b(N)

# Output
print(a, b)
0