結果

問題 No.377 背景パターン
ユーザー convexineqconvexineq
提出日時 2020-03-05 20:09:09
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,584 ms / 5,000 ms
コード長 1,902 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-04-22 03:15:27
合計ジャッジ時間 4,457 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
11,136 KB
testcase_01 AC 30 ms
11,008 KB
testcase_02 AC 29 ms
11,136 KB
testcase_03 AC 30 ms
11,008 KB
testcase_04 AC 28 ms
11,008 KB
testcase_05 AC 29 ms
11,136 KB
testcase_06 AC 28 ms
11,008 KB
testcase_07 AC 27 ms
11,136 KB
testcase_08 AC 29 ms
11,136 KB
testcase_09 AC 28 ms
11,008 KB
testcase_10 AC 28 ms
11,008 KB
testcase_11 AC 29 ms
11,136 KB
testcase_12 AC 29 ms
11,136 KB
testcase_13 AC 37 ms
11,136 KB
testcase_14 AC 40 ms
11,008 KB
testcase_15 AC 28 ms
11,136 KB
testcase_16 AC 1,521 ms
11,392 KB
testcase_17 AC 1,584 ms
11,264 KB
testcase_18 AC 30 ms
11,264 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) は gcd(i,h)*gcd(j,w)の自由度を持つ 
自由度 a*b なる (i,j) の個数は phi(h/a)*phi(h/b)
"""

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
def lcm(x,y):
    return x//gcd(x,y)*y

num = {}
for a in hdiv:
    for b in wdiv:
        d = lcm(a,b)
        if d in num:
            num[d] += totient[a]*totient[b]
        else:
            num[d] = totient[a]*totient[b]

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

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




0