結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー しらっ亭しらっ亭
提出日時 2015-06-12 21:40:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 25 ms / 5,000 ms
コード長 2,552 bytes
コンパイル時間 68 ms
コンパイル使用メモリ 10,872 KB
実行使用メモリ 9,144 KB
最終ジャッジ日時 2023-09-20 21:05:35
合計ジャッジ時間 2,418 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
8,880 KB
testcase_01 AC 25 ms
8,932 KB
testcase_02 AC 24 ms
8,924 KB
testcase_03 AC 23 ms
8,884 KB
testcase_04 AC 22 ms
8,936 KB
testcase_05 AC 23 ms
8,928 KB
testcase_06 AC 20 ms
8,872 KB
testcase_07 AC 22 ms
9,056 KB
testcase_08 AC 22 ms
9,044 KB
testcase_09 AC 22 ms
9,044 KB
testcase_10 AC 22 ms
8,920 KB
testcase_11 AC 23 ms
9,032 KB
testcase_12 AC 25 ms
8,984 KB
testcase_13 AC 24 ms
9,000 KB
testcase_14 AC 23 ms
8,904 KB
testcase_15 AC 24 ms
9,144 KB
testcase_16 AC 24 ms
8,920 KB
testcase_17 AC 24 ms
8,916 KB
testcase_18 AC 23 ms
9,016 KB
testcase_19 AC 24 ms
8,916 KB
testcase_20 AC 23 ms
8,896 KB
testcase_21 AC 24 ms
9,040 KB
testcase_22 AC 23 ms
9,080 KB
testcase_23 AC 25 ms
8,940 KB
testcase_24 AC 23 ms
9,056 KB
testcase_25 AC 24 ms
9,080 KB
testcase_26 AC 24 ms
9,088 KB
testcase_27 AC 24 ms
9,128 KB
testcase_28 AC 23 ms
9,096 KB
testcase_29 AC 24 ms
9,136 KB
testcase_30 AC 24 ms
8,980 KB
testcase_31 AC 24 ms
9,132 KB
testcase_32 AC 24 ms
9,056 KB
testcase_33 AC 24 ms
9,132 KB
testcase_34 AC 20 ms
8,952 KB
testcase_35 AC 21 ms
8,876 KB
testcase_36 AC 21 ms
8,936 KB
testcase_37 AC 23 ms
8,988 KB
testcase_38 AC 23 ms
9,024 KB
testcase_39 AC 23 ms
9,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
from functools import reduce


def gen_odd():
    n = 3
    while True:
        yield n
        n += 2


def primes():
    yield 2
    g = gen_odd()
    while True:
        prime = next(g)
        s_prime = prime * prime
        ps = []
        while s_prime != prime:
            yield prime
            ps.append(prime)
            prime = next(g)
        pred = lambda x, ps = ps: all(x % p for p in ps)
        g = filter(pred, g)


def divl(ns, p, rangen):
    mods = [n % p for n in ns]
    if all(m for m in mods):
        # 全ての n の素因数じゃなかったので次の素数へ
        return ns, False
    else:
        # p がいずれかの n の素因数だったので、割れるものを割る
        ns = [ns[i] if mods[i] else (ns[i] // p) for i in rangen]
        return ns, True


def lcm(ns):
    """
    最小公倍数を求める
    """

    ps = primes()
    p = next(ps)
    rangen = list(range(len(ns)))

    nn = []

    while True:
        max_n = max(*ns)
        max_bound = int(math.sqrt(max_n))
        if p > max_bound:
            break

        ns, divable = divl(ns, p, rangen)
        if divable:
            # p がいずれかの n の素因数だったので、割れるものを割る
            nn.append(p)
        else:
            # 全ての n の素因数じゃなかったので次の素数へ
            p = next(ps)

    # 残った数で割る
    for i in rangen:
        ni = ns[i]

        ns, divable = divl(ns, ni, rangen)

        if divable:
            nn.append(ni)

    # 全ての割れた素数と残った値を掛ける
    lp = reduce(lambda a, b: a * b, nn, 1)
    ln = reduce(lambda a, b: a * b, ns, 1)
    return lp * ln


def solve(N, K, xys):

    # 1回のループで各位置がどこに移動するのか求める
    # M[i] は i 番目の棒が1回のループでどこに移動するか
    M = list(range(N))
    for i in range(K):
        xi, yi = xys[i]
        M[xi], M[yi] = M[yi], M[xi]

    # 各縦棒のループ数値が何回で元の位置に戻るかを計算 O(?)
    L = []
    for n in range(N):
        p = M[n]
        l = 1
        while p != n:
            p = M[p]
            l += 1
        L.append(l)

    # 各縦棒のループ数の最小公倍数が答え
    return lcm(L)


def main():
    N = int(input())
    K = int(input())
    xys = []
    for i in range(K):
        x, y = map(int, input().split())
        xys.append((x - 1, y - 1))

    a = solve(N, K, xys)
    print(a)


if __name__ == '__main__':
    main()
0