結果

問題 No.377 背景パターン
ユーザー 0w10w1
提出日時 2017-04-24 22:11:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,843 ms / 5,000 ms
コード長 1,132 bytes
コンパイル時間 345 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 82,100 KB
最終ジャッジ日時 2023-10-11 13:23:51
合計ジャッジ時間 7,106 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
76,992 KB
testcase_01 AC 117 ms
77,132 KB
testcase_02 AC 115 ms
77,148 KB
testcase_03 AC 116 ms
77,160 KB
testcase_04 AC 109 ms
72,712 KB
testcase_05 AC 110 ms
72,816 KB
testcase_06 AC 109 ms
72,716 KB
testcase_07 AC 110 ms
72,832 KB
testcase_08 AC 110 ms
72,576 KB
testcase_09 AC 111 ms
72,692 KB
testcase_10 AC 111 ms
72,660 KB
testcase_11 AC 110 ms
72,580 KB
testcase_12 AC 112 ms
72,788 KB
testcase_13 AC 181 ms
78,712 KB
testcase_14 AC 174 ms
79,464 KB
testcase_15 AC 112 ms
76,960 KB
testcase_16 AC 1,827 ms
80,620 KB
testcase_17 AC 1,843 ms
82,100 KB
testcase_18 AC 113 ms
77,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 get_prime( x ):
  res = []
  _ = x
  i = 2
  while i * i <= _:
    if _ % i == 0:
      res.append( i )
      while _ % i == 0:
        _ //= i
    i += 1
  if _ > 1:
    res.append( _ )
  return res

hp, wp = get_prime( H ), get_prime( W )

def phi( x, memo = {} ):
  if x in memo:
    return memo[ x ]
  if H % x == 0:
    res = x
    for p in hp:
      if p > x: break
      if x % p == 0:
        res = res // p * ( p - 1 )
    memo[ x ] = res
    return res
  else:
    res = x
    for p in wp:
      if p > x: break
      if x % p == 0:
        res = res // p * ( p - 1 )
    memo[ x ] = res
    return res

def factors( n ):
  return set( reduce( list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if 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 )
0