結果

問題 No.3028 No.9999
ユーザー titia
提出日時 2025-02-25 06:16:44
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 34 ms / 4,000 ms
コード長 1,150 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2025-02-25 06:16:47
合計ジャッジ時間 2,595 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd

# オイラーのトーシェント関数 φ(x) を求める
def euler_totient(n):
    result = n
    i = 2
    while i * i <= n:
        if n % i == 0:
            while n % i == 0:
                n //= i
            result -= result // i
        i += 1
    if n > 1:
        result -= result // n
    return result

# 高速べき乗法 (繰り返し二乗法)
def mod_pow(base, exp, mod):
    result = 1
    while exp > 0:
        if exp % 2 == 1:
            result = (result * base) % mod
        base = (base * base) % mod
        exp //= 2
    return result

# 約数を列挙
def divisors(n):
    divs = []
    for i in range(1, int(n**0.5) + 1):
        if n % i == 0:
            divs.append(i)
            if i != n // i:
                divs.append(n // i)
    return sorted(divs)

# 循環節の長さを求める
def cycle_length(x):
    if x % 2 == 0 or x % 5 == 0:
        return 0  # 有限小数の場合

    phi_x = euler_totient(x)
    for d in divisors(phi_x):
        if mod_pow(10, d, x) == 1:
            return d
    return phi_x  # 最悪でも φ(x) に収束

N=int(input())
print(cycle_length(N))
0