結果
| 問題 |
No.377 背景パターン
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-08-14 01:51:14 |
| 言語 | Crystal (1.14.0) |
| 結果 |
AC
|
| 実行時間 | 215 ms / 5,000 ms |
| コード長 | 973 bytes |
| コンパイル時間 | 13,840 ms |
| コンパイル使用メモリ | 307,904 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-30 20:59:59 |
| 合計ジャッジ時間 | 14,769 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
| 外部呼び出し有り |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 14 |
ソースコード
lib C;fun strtoll(s: UInt8*,p: UInt8*,b: Int32): Int64;end
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{|e|C.strtoll(e,nil,10)}
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