Skip to content

Instantly share code, notes, and snippets.

@vslaykovsky-zz
Created January 18, 2015 17:56
Show Gist options
  • Save vslaykovsky-zz/d900d28c8921b97288d5 to your computer and use it in GitHub Desktop.
Save vslaykovsky-zz/d900d28c8921b97288d5 to your computer and use it in GitHub Desktop.
MOD=1000000007
def solve a, b
#puts "solve #{a} #{b}"
return 1 if a == 0 or b == 0
$cache = (a + 1).times.map{[-1] * (b + 1)} unless $cache
return $cache[a][b] if $cache[a][b] != -1
r = solve(a, b - 1)
r += solve(a - 1, b) if a - 1 > b
$cache[a][b] = r
end
def solve_stress a, b
#puts "solve_stress #{a} #{b}"
return 1 if a == 0 or b == 0
$cache_stress = (a + 1).times.map{[-1] * (b + 1)} unless $cache_stress
return $cache_stress[a][b] if $cache_stress[a][b] != -1
r = 0
if a > b
r = solve_stress(b, b)
else
r = solve_stress(a - 1, b)
r += solve_stress(a, b - 1) if b - 1 >= a
end
$cache_stress[a][b] = r
end
gets.to_i.times do |i|
a,b = gets.split(/-/).map(&:to_i)
$cache = $cache_stress = nil
puts "Case ##{i + 1}: #{solve(a, b) % MOD} #{solve_stress(a, b) % MOD}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment