結果
| 問題 |
No.377 背景パターン
|
| コンテスト | |
| ユーザー |
convexineq
|
| 提出日時 | 2020-03-05 20:09:09 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 1,758 ms / 5,000 ms |
| コード長 | 1,902 bytes |
| コンパイル時間 | 106 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 11,264 KB |
| 最終ジャッジ日時 | 2024-10-14 02:06:33 |
| 合計ジャッジ時間 | 4,895 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 14 |
ソースコード
# 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)
convexineq