結果
| 問題 |
No.377 背景パターン
|
| コンテスト | |
| ユーザー |
convexineq
|
| 提出日時 | 2020-03-05 20:25:02 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,027 bytes |
| コンパイル時間 | 164 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 11,520 KB |
| 最終ジャッジ日時 | 2024-10-14 02:07:02 |
| 合計ジャッジ時間 | 5,550 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 3 |
| other | WA * 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) の周期は 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[bb]*totient[bb]
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)
convexineq