Created
December 7, 2013 14:43
-
-
Save inage/7843112 to your computer and use it in GitHub Desktop.
重複組合せ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 重複組合せ | |
| ## 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