Skip to content

Instantly share code, notes, and snippets.

@mandos1995
Created April 2, 2023 15:29
var cache = [[Int]](repeating: [Int](repeating: 0, count: 31), count: 31)
for i in 0...30 {
for j in 0...i {
if i == j || j == 0 {
cache[i][j] = 1
continue
}
cache[i][j] = cache[i - 1][j - 1] + cache[i - 1][j]
}
}
func solution() {
let input = readLine()!.split(separator: " ").map { Int($0)! }
let r = input[0], n = input[1]
print(cache[n][r])
}
let t = Int(readLine()!)!
for _ in 0..<t { solution() }
var cache = [[Int]](repeating: [Int](repeating: 0, count: 31), count: 31)
func f(n: Int, r: Int) -> Int {
if cache[n][r] != 0 {
return cache[n][r]
}
if n == r || r == 0 {
return 1
}
cache[n][r] = f(n: n - 1, r: r) + f(n: n - 1, r: r - 1)
return cache[n][r]
}
func solution() {
let input = readLine()!.split(separator: " ").map { Int($0)! }
let r = input[0], n = input[1]
print(f(n: n, r: r))
}
let t = Int(readLine()!)!
for _ in 0..<t { solution() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment