Skip to content

Instantly share code, notes, and snippets.

@prat0318
Created March 2, 2014 00:29
Show Gist options
  • Save prat0318/9299888 to your computer and use it in GitHub Desktop.
Save prat0318/9299888 to your computer and use it in GitHub Desktop.
There are three singers Prateek, Akanksha and gurbinder. They are doing a concert to celebrate my awesomeness. I have asked them to sing X distinct number of songs. Though each one of them want to sing all X songs, but due to their limits, each one has to sing P, A and G songs. All songs can be sung by either 1, 2 or 3 of them. Each of the X son…
def func(x, p, a, g)
return 1 if [x,p,a,g].all?(&:zero?)
return 0 if [p,a,g].any? {|i| i<0} || x == 0 || p+a+g<x
(0..7).map{|i| ("%03b" % i)}.inject(0){|cnt, pos| cnt + func(x-1, p - pos[0].to_i, a - pos[1].to_i, g - pos[2].to_i)}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment