์‹ค๋ฒ„4 : ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ๋ฌธ์ œ์ด๋‹ค.

์ƒ๊ฐ

๊ณ ๋“ฑํ•™๊ต ๋•Œ ๋งŽ์ด ํ’€์–ด๋ณธ ํ•จ์ˆ˜์˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์™€ ๊ฐ™๋‹ค. ๊ฒฐ๊ตญ์€ ๊ณต์—ญ๋‹จ์—์„œ N๊ฐœ์˜ site๋ฅผ ๊ณ ๋ฅด๊ฒŒ ๋˜๋ฉด ์ž๋™์œผ๋กœ ์ˆœ์„œ๊ฐ€ ๊ฒฐ์ •๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. M๊ฐœ์˜ ์›์†Œ ์ค‘์— N๊ฐœ์˜ ์›์†Œ๋ฅผ ๊ณ ๋ฅด๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฒƒ์€ ๊ฒฐ๊ตญ ์กฐํ•ฉ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์™€ ๊ฐ™๋‹ค. ๊ทธ ๊ณผ์ •์—์„œ ์ค‘๋ณต๋˜๋Š” ์กฐํ•ฉ ๊ฐ’์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฒƒ์„ ๋”ฐ๋กœ ์ €์žฅํ•˜๊ฒŒ ๋˜๋ฉด ์‹œ๊ฐ„์ด ๋งค์šฐ ๋งŽ์ด ๋‹จ์ถ•๋œ๋‹ค.

Code

#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
ll memo[31][31];
 
ll combination(int M, int N){
    if (M == N || N == 0) return 1;
    if (memo[M-1][N-1] == 0) memo[M-1][N-1] = combination(M-1, N-1);
    if (memo[M-1][N] == 0) memo[M-1][N] = combination(M-1, N);
    return memo[M-1][N-1] + memo[M-1][N];
}
 
int main(){
    int T;
    cin >> T;
    for (int tc = 0; tc < T; tc++) {
        int N, M;
        cin >> N >> M;
        cout << combination(M, N) << '\n';
    }
}
 

Reference