結果

問題 No.377 背景パターン
ユーザー convexineqconvexineq
提出日時 2020-03-05 20:28:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,112 ms / 5,000 ms
コード長 2,040 bytes
コンパイル時間 155 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-04-22 03:16:01
合計ジャッジ時間 5,660 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
10,880 KB
testcase_01 AC 32 ms
11,136 KB
testcase_02 AC 29 ms
11,008 KB
testcase_03 AC 31 ms
10,880 KB
testcase_04 AC 30 ms
11,008 KB
testcase_05 AC 30 ms
10,880 KB
testcase_06 AC 31 ms
11,008 KB
testcase_07 AC 31 ms
11,008 KB
testcase_08 AC 31 ms
10,880 KB
testcase_09 AC 32 ms
10,880 KB
testcase_10 AC 29 ms
10,880 KB
testcase_11 AC 31 ms
10,880 KB
testcase_12 AC 31 ms
11,008 KB
testcase_13 AC 41 ms
11,136 KB
testcase_14 AC 42 ms
11,008 KB
testcase_15 AC 32 ms
11,008 KB
testcase_16 AC 2,013 ms
11,648 KB
testcase_17 AC 2,112 ms
11,392 KB
testcase_18 AC 34 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8
# Your code here!
"""
素因数分解
input: N
output: [[p1,e1],[p2,e2],...] の形で素因数分解する。
        N=1なら空のリストを返す
"""
def prime_factorize(N): #素因数分解
    exponent = 0
    while N%2 == 0:
        exponent += 1
        N //= 2
    if exponent: factorization = [[2,exponent]]
    else: factorization = []
    i=1
    while i*i <=N:
        i += 2
        if N%i: continue
        exponent = 0
        while N%i == 0:
            exponent += 1
            N //= i
        factorization.append([i,exponent])
    if N!= 1: factorization.append([N,1])
    assert N != 0, "zero"
    return factorization

def factorization_to_divisors(fac):
    res = [1]
    for p,e in fac:
        nres = []
        for x in res:
            for i in range(e+1):
                nres.append(x)
                x *= p
        res = nres
    return res


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

h,w,k = [int(i) for i in read().split()]

"""
Polya
(i,j) in Z/(h)*Z/(w)  の周期は lcm(h//gcd(i,h),w//gcd(j,w)) なので、
自由度は
d := h*w//lcm(h//gcd(i,h),w//gcd(j,w)) = gcd(i,h)*gcd(j,w)*gcd(h//gcd(i,h),w//gcd(j,w))
gcd(i,h) = a なる i は phi(h//a) 個ある
"""

hfac = prime_factorize(h)
wfac = prime_factorize(w)
hdiv = factorization_to_divisors(hfac)
wdiv = factorization_to_divisors(wfac)

div = list(set(hdiv+wdiv))
ph = [p for p,e in wfac]
pw = [p for p,e in hfac]
primes = list(set(ph+pw))
totient = {i:i for i in div}

for x in div:
    for p in primes:
        if x%p==0: totient[x] = totient[x]//p*(p-1)

from math import gcd

num = {}
# ここでは、aa = h//a, bb = w//b のつもり
for aa in hdiv:
    for bb in wdiv:
        d = h*w//(aa*bb)*gcd(aa,bb)
        if d in num:
            num[d] += totient[aa]*totient[bb]
        else:
            num[d] = totient[aa]*totient[bb]

#print(num)

ans = 0
MOD = 10**9+7
for d,v in num.items():
    ans += v*pow(k,d,MOD)%MOD
    ans %= MOD

print(ans*pow(h*w,MOD-2,MOD)%MOD)
0