結果

問題 No.377 背景パターン
ユーザー maspymaspy
提出日時 2023-05-12 20:52:53
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 3,027 ms / 5,000 ms
コード長 1,395 bytes
コンパイル時間 102 ms
コンパイル使用メモリ 11,132 KB
実行使用メモリ 115,032 KB
最終ジャッジ日時 2023-08-19 03:24:01
合計ジャッジ時間 12,647 ms
ジャッジサーバーID
(参考情報)
judge9 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
30,004 KB
testcase_01 AC 142 ms
29,928 KB
testcase_02 AC 139 ms
29,936 KB
testcase_03 AC 137 ms
29,832 KB
testcase_04 AC 136 ms
29,904 KB
testcase_05 AC 138 ms
29,940 KB
testcase_06 AC 138 ms
29,968 KB
testcase_07 AC 137 ms
29,900 KB
testcase_08 AC 135 ms
29,964 KB
testcase_09 AC 140 ms
30,068 KB
testcase_10 AC 143 ms
29,916 KB
testcase_11 AC 141 ms
30,000 KB
testcase_12 AC 140 ms
29,828 KB
testcase_13 AC 153 ms
30,376 KB
testcase_14 AC 152 ms
30,280 KB
testcase_15 AC 138 ms
29,936 KB
testcase_16 AC 2,943 ms
111,004 KB
testcase_17 AC 3,027 ms
115,032 KB
testcase_18 AC 140 ms
29,972 KB
権限があれば一括ダウンロードができます

ソースコード

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