1005 - Rooks
Topic: combinatorics + Binomial Coefficient
This file contains 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
#include<bits/stdc++.h> | |
using namespace std; | |
typedef long long ll; | |
ll nCr(int n, int k) | |
{ | |
ll ans[n+1][k+1]; | |
int i,j; | |
for(int i = 0; i <= n; i++){ | |
for(int j = 0; j <= min(i,k); j++){ | |
if(j == 0 || j == i) | |
ans[i][j] = 1; | |
else{ | |
ans[i][j] = ans[i-1][j-1] + ans[i-1][j]; | |
} | |
} | |
} | |
return ans[n][k]; | |
} | |
int main() | |
{ | |
int T; | |
scanf("%d", &T); | |
for(int cs = 1; cs <= T; cs++) | |
{ | |
ll n,k; | |
cin >> n >> k; | |
ll res = nCr(n,k); | |
for(int i =0; i < k; i++){ | |
res *= n; | |
n--; | |
} | |
printf("Case %d: ",cs); | |
cout << res << endl; | |
} | |
return 0; | |
} |
No comments:
Post a Comment