結果

問題 No.377 背景パターン
ユーザー 0w10w1
提出日時 2017-04-25 00:59:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,207 ms / 5,000 ms
コード長 922 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 87,240 KB
実行使用メモリ 82,708 KB
最終ジャッジ日時 2023-10-11 13:37:23
合計ジャッジ時間 7,898 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
77,372 KB
testcase_01 AC 116 ms
77,628 KB
testcase_02 AC 119 ms
77,488 KB
testcase_03 AC 118 ms
77,760 KB
testcase_04 AC 109 ms
73,040 KB
testcase_05 AC 111 ms
72,972 KB
testcase_06 AC 110 ms
72,672 KB
testcase_07 AC 106 ms
73,108 KB
testcase_08 AC 107 ms
73,080 KB
testcase_09 AC 106 ms
73,020 KB
testcase_10 AC 104 ms
72,828 KB
testcase_11 AC 105 ms
72,676 KB
testcase_12 AC 104 ms
72,872 KB
testcase_13 AC 195 ms
79,440 KB
testcase_14 AC 179 ms
79,004 KB
testcase_15 AC 109 ms
77,416 KB
testcase_16 AC 2,179 ms
81,272 KB
testcase_17 AC 2,207 ms
82,708 KB
testcase_18 AC 113 ms
77,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import product
from functools import reduce

MOD = int( 1e9 ) + 7

H, W, K = map( int, input().split() )

def gcd( a, b ):
  return a if b == 0 else 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 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 )
hp, wp = [ v for v in hf if is_prime( v ) ], [ v for v in wf if is_prime( v ) ]

def phi( x, memo = {} ):
  if x in memo: return memo[ x ]
  memo[ x ] = reduce( lambda a, b: a // b * ( b - 1 ), [ x ] + [ p for p in ( hp if H % x == 0 else wp ) if x % p == 0 ] )
  return memo[ x ]

ans = sum( pow( K, H // a * W // b * gcd( a, b ), MOD ) * phi( a ) * phi( b ) for a, b in product( hf, wf ) ) * pow( H * W, MOD - 2, MOD ) % MOD
print( ans )
0