๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 13239๋ฒˆ - Combinations

by dkswnkk 2021. 11. 4.
 

13239๋ฒˆ: Combinations

When we have a set of n elements from which we take k elements, we have a combination. For example, if we have a set with the numbers from 1 to 5, we have the following different combinations: 1-combinations (1 element from the set each time): (1), (2), (3

www.acmicpc.net

์กฐํ•ฉ์„ ํŒŒ์Šค์นผ ์‚ผ๊ฐํ˜• ์ ํ™”์‹์„ ์ด์šฉํ•˜์—ฌ DP๋กœ ํ‘ผ Solution ์ด๋‹ค.

#include <iostream>
#define MOD 1000000007
using namespace std;



int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int T; cin>>T;
    while(T--){
        long long int arr[1001][1001];
        int N,K; cin>>N>>K;

        for(int i=0; i<=N; i++){
            for(int k=0; k<=K; k++){
                if(i==k||k==0)arr[i][k]=1;
                else arr[i][k]=(arr[i-1][k-1]+arr[i-1][k])%MOD;
            }
        }
        cout<<arr[N][K]<<'\n';
    }
}

๋Œ“๊ธ€