-
Notifications
You must be signed in to change notification settings - Fork 0
/
locks.cpp
53 lines (45 loc) · 880 Bytes
/
locks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;
inline double factorial( int m, int end )
{
// Initialize return value
double ret = 1.0;
// Do: m * (m-1) * (m-2) * ... * (end + 1)
for( int i = m; i > end; --i )
{
ret *= i;
}
// Return
return ret;
}
long long combination( int m, int n )
{
// Combination formula is: m! / n!(m-n)!
// If (m-n) is bigger
if( (m - n) > n )
{
// Do (m * (m-1) * ... * (m - n + 1)) / (n!)
return factorial( m, m - n ) / factorial( n, 0 );
}
// If n is bigger or the same as (m-n)
else
{
// Do (m * (m-1) * ... * (n + 1) / (m - n)!
return factorial( m, n ) / factorial( m - n, 0 );
}
}
int main()
{
// Read the number of cases
int numCases;
cin >> numCases;
// For each, calculate
for( int i = 0; i < numCases; ++i )
{
int x, y;
cin >> x >> y;
cout << combination( x, y - 1 ) << endl;
}
// Return
return 0;
}