結果

問題 No.377 背景パターン
ユーザー maspy
提出日時 2023-05-12 20:52:53
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 3,279 ms / 5,000 ms
コード長 1,395 bytes
コンパイル時間 121 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 129,084 KB
最終ジャッジ日時 2024-11-28 16:35:46
合計ジャッジ時間 18,498 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

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

MOD = 10 ** 9 + 7

U = int(10 ** 4.5 + 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 factor(N):
    div_p = primes[N % primes == 0].tolist()
    for p in div_p:
        e = 0
        while N % p == 0:
            N //= p
            e += 1
        yield (p, e)
    if N > 1:
        yield (N, 1)


def divisor_and_phi(N):
    div = np.array([1], np.int64)
    phi = np.array([1], np.int64)
    for p, e in factor(N):
        div = np.concatenate([div * pow(p, k) for k in range(0, e + 1)])
        phi = np.concatenate([phi] + [phi * (p - 1) * pow(p, k - 1) for k in range(1, e + 1)])
    return div, phi


H, W, K = map(int, read().split())
period_H, cnt_H = divisor_and_phi(H)
period_W, cnt_W = divisor_and_phi(W)

period = np.lcm.outer(period_H, period_W).ravel()
cnt = np.multiply.outer(cnt_H, cnt_W).ravel() % MOD
n_orbit = H * W // period

power = np.ones(len(n_orbit), np.int64)
KK = K
for i in range(64):
    power *= KK ** ((n_orbit >> i) & 1)
    power %= MOD
    KK *= KK
    KK %= MOD

x = (power * cnt % MOD).sum() % MOD
x *= pow(H * W, MOD - 2, MOD)
x %= MOD
print(x)
0