Skip to content

Instantly share code, notes, and snippets.

@sujoyu
Last active September 9, 2016 05:59
Show Gist options
  • Save sujoyu/b4a4c2ddb1890a879a290f004e52472d to your computer and use it in GitHub Desktop.
Save sujoyu/b4a4c2ddb1890a879a290f004e52472d to your computer and use it in GitHub Desktop.
数字の書いてあるN枚のカードから、足してAになる組み合わせの数をかぞえる
object Main {
def main(args:Array[String]) = {
val sc = new java.util.Scanner(System.in)
val n, a = sc.nextInt
val x = List.fill(n)(sc.nextInt)
val nx = (a :: x).max * n
val dpMemo = collection.mutable.ArrayBuffer.fill(n + 1, n + 1, nx + 1)(-1L)
def dp(j: Int, k: Int, s: Int): Long = {
if (dpMemo(j)(k)(s) >= 0) return dpMemo(j)(k)(s)
val xj = x(j - 1)
val ret = if (j == 0 && k == 0 && s == 0) 1L
else if (j > 0 && s < xj) dp(j - 1, k, s)
else if (j > 0 && k > 0 && s >= xj) dp(j - 1, k, s) + dp(j - 1, k - 1, s - xj)
else 0L
dpMemo(j)(k)(s) = ret
ret
}
val ans = (1 to n).map(k => dp(n, k, k * a)).sum
println(ans)
}
}
@sujoyu
Copy link
Author

sujoyu commented Sep 9, 2016

http://abc044.contest.atcoder.jp/tasks/arc060_a
こちらのAtCoderの過去問です

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment