結果

問題 No.1039 Project Euler でやれ
ユーザー maspymaspy
提出日時 2023-05-28 04:13:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 2,302 bytes
コンパイル時間 80 ms
コンパイル使用メモリ 11,100 KB
実行使用メモリ 53,292 KB
最終ジャッジ日時 2023-08-27 03:27:04
合計ジャッジ時間 4,313 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 150 ms
33,992 KB
testcase_01 AC 189 ms
41,740 KB
testcase_02 AC 182 ms
46,528 KB
testcase_03 AC 181 ms
53,040 KB
testcase_04 AC 183 ms
53,292 KB
testcase_05 AC 173 ms
47,604 KB
testcase_06 AC 167 ms
43,028 KB
testcase_07 AC 142 ms
31,116 KB
testcase_08 AC 151 ms
34,832 KB
testcase_09 AC 158 ms
38,804 KB
testcase_10 AC 146 ms
31,340 KB
testcase_11 AC 171 ms
44,236 KB
testcase_12 AC 152 ms
33,288 KB
testcase_13 AC 138 ms
30,228 KB
testcase_14 AC 158 ms
39,080 KB
testcase_15 AC 161 ms
41,992 KB
testcase_16 AC 138 ms
29,884 KB
testcase_17 AC 137 ms
29,952 KB
testcase_18 AC 139 ms
30,180 KB
testcase_19 AC 137 ms
29,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
from functools import lru_cache

MOD = 10 ** 9 + 7

U = 10 ** 3 + 10
is_prime = np.zeros(U, np.bool_)
is_prime[2] = 1
is_prime[3::2] = 1
for p in range(3, U, 2):
    if p * p >= U:
        break
    if is_prime[p]:
        is_prime[p * p:: p + p] = 0
primes = np.where(is_prime)[0]


def cumprod(A, MOD=MOD):
    L = len(A)
    Lsq = int(L**.5 + 1)
    A = np.resize(A, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        A[:, n] *= A[:, n - 1]
        A[:, n] %= MOD
    for n in range(1, Lsq):
        A[n] *= A[n - 1, -1]
        A[n] %= MOD
    return A.ravel()[:L]


def make_fact(U, MOD=MOD):
    x = np.arange(U, dtype=np.int64)
    x[0] = 1
    fact = cumprod(x, MOD)
    fact.flags.writeable = False
    return fact


def factor(M):
    pf = primes[M % primes == 0]
    for p in pf:
        e = 0
        while M % p == 0:
            M //= p
            e += 1
        yield (int(p), e)
    if M > 1:
        yield (int(M), 1)


@lru_cache(None)
def GL(p, n):
    # count the element of GL(n,F_p)
    x = 1
    for i in range(n):
        x *= (p ** n - p ** i)
        x %= MOD
    return x % MOD


def Aut(p, e, partition):
    # count Aut(prod Z/p^{n_i})
    x = 1
    smaller = 0
    for i in range(1, e + 1):
        n = sum(k == i for k in partition)
        x *= GL(p, n)
        x *= pow(p, (i - 1) * n * n, MOD)
        x *= pow(p, smaller * n, MOD)
        m = sum(k > i for k in partition)
        x *= pow(p, i * m * n, MOD)
        smaller += n * i
    return x % MOD


def make_partitions(N, largest=None):
    if N == 0:
        yield []
        return
    if largest is None:
        largest = N
    if largest > N:
        largest = N
    for i in range(largest, 0, -1):
        for p in make_partitions(N - i, i):
            yield p + [i]


def compute_p_part(p, e):
    # sum(1 / Aut)
    x = 0
    for par in make_partitions(e):
        aut = Aut(p, e, par)
        x += pow(aut, MOD - 2, MOD)
        x %= MOD
    return x


def solve(M):
    fact = make_fact(M + 10)
    x = 1
    for p, e in factor(M):
        x *= compute_p_part(p, e)
        x %= MOD
    x *= fact[M]
    return x % MOD


M = int(read())
print(solve(M))
0