Snippets

cia_rana 今週のお題:グループで乗るリフト

Created by cia_rana last modified
def combination(n, k)
  k = n - k if k * 2 > n
  k == 0 ? 1 : ((n - k + 1)..n).reduce(:*)/(1..k).reduce(:*)
end

def calc_pattern(m, n, max_num)
  (0..(k = n - m)/max_num).reduce(0) { |s, i| s + [1, -1][i % 2] * combination(m, i) * combination(m + k - 1 - max_num * i, m - 1) }
end

m, n = gets.split.map(&:to_i)
p ((1.0*m/n).ceil..m).inject(0){|sum, r|
  sum + calc_pattern(r, m, n)
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.