Skip to content

Instantly share code, notes, and snippets.

@inage
Created December 7, 2013 14:43
Show Gist options
  • Select an option

  • Save inage/7843112 to your computer and use it in GitHub Desktop.

Select an option

Save inage/7843112 to your computer and use it in GitHub Desktop.
重複組合せ
## 重複組合せ
## http://rubyfiddle.com/riddles/aba0f
n = 3
m = 3
a = [1, 2, 3]
M = 10000
dp = Array.new(n+1,0).map{Array.new(m+1,0)}
(n+1).times{|i|
dp[i][0] = 1
}
n.times{|i|
j = 1
while j <= m
if (j-1-a[i]) >= 0
dp[i+1][j] = (dp[i+1][j-1]+dp[i][j]-dp[i][j-1-a[i]])
else
dp[i+1][j] =(dp[i+1][j-1]+dp[i][j])
end
j += 1
end
}
puts dp[n][m]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment