Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Last active October 19, 2017 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/2caebce27266a6cd085a713c3073a844 to your computer and use it in GitHub Desktop.
Save whatalnk/2caebce27266a6cd085a713c3073a844 to your computer and use it in GitHub Desktop.
AtCoder ARC #044 B - 最短路問題
R = 10**9+7
N = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)
if a[0] != 0
puts 0
exit
end
amax = a.max
h = Array.new(amax+1, 0)
a.each do |i|
h[i] += 1
end
if h[0] != 1
puts 0
exit
end
h.each do |i|
if i == 0
puts 0
exit
end
end
@pow2 = Array.new(N+1, 0)
@pow2[0] = 1
(N).times do |i|
@pow2[i+1] = @pow2[i] * 2
@pow2[i+1] %= R
end
def pow2mod(n)
ret = 1
if n > N
while n > N
ret *= @pow2[N]
ret %= R
n -= N
end
end
ret *= @pow2[n]
ret %= R
return ret
end
ans = 1
b = 1
h[1..amax].each do |i|
ans *= pow2mod(i * (i - 1) / 2)
ans %= R
ans *= (((@pow2[b] - 1) % R) ** i)
ans %= R
b = i
end
puts ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment