Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Last active July 3, 2016 00:35
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/4771f905ac3e4fb739dbdc52fb7eb9ae to your computer and use it in GitHub Desktop.
Save whatalnk/4771f905ac3e4fb739dbdc52fb7eb9ae to your computer and use it in GitHub Desktop.
AtCoder ABC #041 (Ruby)
s = gets.chomp
i = gets.chomp.to_i
puts s[i-1]
a, b, c = gets.chomp.split(" ").map(&:to_i)
f = 10**9 + 7
puts a * b * c % f
n = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)
a.zip((1..n).to_a).sort{|x, y| -(x[0] <=> y[0])}.each do |b|
puts b[1]
end
# http://abc041.contest.atcoder.jp/submissions/791292
# http://abc041.contest.atcoder.jp/submissions/790956
n, m = gets.chomp.split(" ").map(&:to_i)
x = []
y = []
m.times do
line = gets.chomp.split(" ").map(&:to_i)
x << line[0] - 1
y << line[1] - 1
end
valid = []
dp = Array.new(1<<16, 0)
nn = (1<<n)
def contain(mask, pos)
return !(mask & (1<<pos)).zero?
end
nn.times do |mask|
valid[mask] = true
m.times do |i|
if contain(mask, y[i]) && !contain(mask, x[i]) then
valid[mask] = false
break
end
end
end
dp[0] = 1
nn.times do |mask|
if valid[mask] then
n.times do |i|
if contain(mask, i) && valid[mask^(1<<i)] then
dp[mask] += dp[mask^(1<<i)]
end
end
end
end
puts dp[nn - 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment