結果

問題 No.377 背景パターン
ユーザー cielciel
提出日時 2021-06-15 00:17:32
言語 Ruby
(3.3.0)
結果
AC  
実行時間 2,237 ms / 5,000 ms
コード長 1,093 bytes
コンパイル時間 137 ms
コンパイル使用メモリ 11,388 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2023-08-26 14:25:47
合計ジャッジ時間 6,836 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
外部呼び出し有り
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
15,128 KB
testcase_01 AC 81 ms
15,128 KB
testcase_02 AC 76 ms
15,112 KB
testcase_03 AC 73 ms
15,184 KB
testcase_04 AC 73 ms
15,216 KB
testcase_05 AC 76 ms
15,172 KB
testcase_06 AC 75 ms
15,308 KB
testcase_07 AC 74 ms
15,276 KB
testcase_08 AC 72 ms
15,048 KB
testcase_09 AC 72 ms
15,156 KB
testcase_10 AC 73 ms
15,016 KB
testcase_11 AC 76 ms
15,280 KB
testcase_12 AC 76 ms
15,160 KB
testcase_13 AC 84 ms
15,288 KB
testcase_14 AC 82 ms
15,468 KB
testcase_15 AC 74 ms
15,244 KB
testcase_16 AC 2,209 ms
15,488 KB
testcase_17 AC 2,237 ms
15,488 KB
testcase_18 AC 74 ms
15,108 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#!/usr/bin/ruby
def yield_from(*funcsyms)
	funcsyms.each{|funcsym|
		func = method(funcsym)
		define_method(funcsym){|*args,&blk|
			return to_enum(funcsym,*args) if !blk
			func.(*args){|*a|a.each{|e|blk.(e)}}
		}
	}
end

if false
	require 'prime'
	def prime_division(n) n.prime_division end
else
	def prime_division(n)
		IO.popen('factor '+n.to_s){|io|
			a=[]
			line=io.gets
			line.split[1..-1].group_by{|e|e}.each{|k,v|a<<[k.to_i,v.size]}
			a
		}
	end
end
def divisor_totient(a,d,n,t,&blk)
	return to_enum(:divisor,a,d,n,t) if !block_given?
	if d==a.size
		blk.call [n,t]
	else
		(0..a[d][1]).each{|i|
			divisor_totient(
				a,d+1,
				n*a[d][0]**i,
				i==0 ? t : t*(a[d][0]-1)*a[d][0]**(i-1),
				&blk
			)
		}
	end
end

def pow(x,y,m)
	z=1
	while y>0
		z=z*x%m if y%2>0
		x=x*x%m
		y/=2
	end
	z
end

cache={}
M=1000000007
H,W,K=gets.split.map(&:to_i)
A=prime_division(H)
B=prime_division(W)
r=0
divisor_totient(A,0,1,1){|a,at|
	divisor_totient(B,0,1,1){|b,bt|
		key=W*H/a.lcm(b)
		cache[key]=pow(K,key,M) if !cache.has_key?(key)
		r=(r+at*bt*cache[key])%M
	}
}
p r*pow(W*H,M-2,M)%M
0