結果
| 問題 | 
                            No.377 背景パターン
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2017-04-24 22:37:37 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 3,337 ms / 5,000 ms | 
| コード長 | 1,157 bytes | 
| コンパイル時間 | 391 ms | 
| コンパイル使用メモリ | 81,892 KB | 
| 実行使用メモリ | 85,360 KB | 
| 最終ジャッジ日時 | 2024-09-13 12:11:03 | 
| 合計ジャッジ時間 | 8,910 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 5 | 
| other | AC * 14 | 
ソースコード
from functools import reduce
MOD = int( 1e9 ) + 7
H, W, K = map( int, input().split() )
def gcd( a, b ):
  if b == 0: return a
  return gcd( b, a % b )
def is_prime( n ):
  return 1 < n and all( n % i for i in range( 2, int( n ** 0.5 ) + 1 ) )
def get_prime( n ):
  return [ i for i in range( 1, int( n ** 0.5 ) + 1 ) if n % i == 0 and is_prime( i ) ] + [ n // i for i in range( 1, int( n ** 0.5 ) + 1 ) if i * i != n and n % i == 0 and is_prime( n // i ) ]
hp, wp = get_prime( H ), get_prime( W )
def phi( x ):
  if H % x == 0:
    res = x
    for p in hp:
      if x % p == 0:
        res = res // p * ( p - 1 )
    return res
  else:
    res = x
    for p in wp:
      if x % p == 0:
        res = res // p * ( p - 1 )
    return res
def factors( n ):
  return [ i for i in range( 1, int( n ** 0.5 ) + 1 ) if n % i == 0 ] + [ n // i for i in range( 1, int( n ** 0.5 ) + 1 ) if i * i != n and n % i == 0 ]
hf, wf = factors( H ), factors( W )
ans = 0
for a in hf:
  for b in wf:
    ans += pow( K, H // a * W // b * gcd( a, b ) % ( MOD - 1 ), MOD ) * phi( a ) * phi( b )
    ans %= MOD
ans = ans * pow( H * W, MOD - 2, MOD ) % MOD
print( ans )