結果

問題 No.377 背景パターン
ユーザー cielciel
提出日時 2020-02-09 01:12:49
言語 Crystal
(1.11.2)
結果
AC  
実行時間 285 ms / 5,000 ms
コード長 900 bytes
コンパイル時間 21,678 ms
コンパイル使用メモリ 264,260 KB
実行使用メモリ 5,004 KB
最終ジャッジ日時 2023-09-13 10:27:45
合計ジャッジ時間 23,701 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
外部呼び出し有り
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
4,636 KB
testcase_01 AC 7 ms
4,880 KB
testcase_02 AC 7 ms
4,824 KB
testcase_03 AC 7 ms
4,756 KB
testcase_04 AC 7 ms
4,808 KB
testcase_05 AC 7 ms
4,688 KB
testcase_06 AC 7 ms
4,852 KB
testcase_07 AC 7 ms
4,780 KB
testcase_08 AC 7 ms
4,768 KB
testcase_09 AC 8 ms
4,676 KB
testcase_10 AC 7 ms
4,700 KB
testcase_11 AC 7 ms
4,764 KB
testcase_12 AC 7 ms
4,752 KB
testcase_13 AC 9 ms
4,696 KB
testcase_14 AC 8 ms
4,892 KB
testcase_15 AC 7 ms
4,732 KB
testcase_16 AC 285 ms
5,004 KB
testcase_17 AC 283 ms
4,832 KB
testcase_18 AC 8 ms
4,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

M=1000000007_i64
def pow(x : Int64,y : Int64,m : Int64)
	z=1_i64
	while y>0
		z=z*x%m if y%2>0
		x=x*x%m
		y//=2
	end
	z
end
def prime_division(n)
	Process.run("factor",["factor",n.to_s]){|io|
		h=Hash(Int64,Int64).new(0_i64)
		io.output.gets.not_nil!.split()[1..-1].each{|e|h[e.to_i64]+=1}
		h.map{|k,v|[k,v]}
	}
end
def divisor_totient(a,d,n,t,&blk : (Int64,Int64)->_)
	if d==a.size
		blk.call(n,t)
	else
		(0..a[d][1]).each{|i|
			divisor_totient(
				a,d+1,
				n*pow(a[d][0],i.to_i64,M),
				i==0 ? t : t*(a[d][0]-1)*pow(a[d][0],(i-1).to_i64,M),&blk)
		}
	end
end

cache=Hash(Int64,Int64).new
h,w,k=ARGF.gets_to_end.split.map(&.to_i64)
a=prime_division(h)
b=prime_division(w)
r=0_i64
divisor_totient(a,0,1_i64,1_i64){|ad,at|
	divisor_totient(b,0,1_i64,1_i64){|bd,bt|
		key=w*h//ad.lcm(bd)
		cache[key]=pow(k,key,M) if !cache.has_key?(key)
		r=(r+at*bt%M*cache[key])%M
	}
}
p r*pow(w*h%M,M-2,M)%M
0