結果

問題 No.443 GCD of Permutation
ユーザー gew1fw
提出日時 2025-06-12 21:15:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 759 bytes
コンパイル時間 249 ms
コンパイル使用メモリ 82,268 KB
実行使用メモリ 64,116 KB
最終ジャッジ日時 2025-06-12 21:16:26
合計ジャッジ時間 2,583 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 22 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

def compute_gcd(n_str):
    # Check if all characters are the same
    if all(c == n_str[0] for c in n_str):
        return n_str
    
    # Compute sum of digits
    s = sum(int(c) for c in n_str)
    
    # Compute minimal element m
    digits = sorted(n_str)
    # Find the first non-zero digit
    first_non_zero = next(i for i, d in enumerate(digits) if d != '0')
    # Form the minimal element
    minimal_digits = [digits[first_non_zero]] + digits[:first_non_zero] + digits[first_non_zero+1:]
    m_str = ''.join(minimal_digits)
    
    # Compute m mod s
    mod = 0
    for c in m_str:
        mod = (mod * 10 + int(c)) % s
    
    # GCD of s and mod
    g = math.gcd(s, mod)
    return str(g)

n = input().strip()
print(compute_gcd(n))
0