forked from ajahuang/CodeChef
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarbles.cpp
More file actions
46 lines (38 loc) · 861 Bytes
/
Marbles.cpp
File metadata and controls
46 lines (38 loc) · 861 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long long ull;
ull combinations(ull n, ull k)
{
if (k > n)
return 0;
// C(n, k) = C(n, n - k)
if (n - k < k)
k = n - k;
ull ret = 1;
for (ull d = 1; d <= k; ++d)
{
ret *= n--;
ret /= d;
}
return ret;
}
int main()
{
int T;
cin >> T;
while ( T-- )
{
ull n, k;
cin >> k >> n;
// Select at least one marble of each color.
k -= n;
/**
This is a typical problem of k-combination with repetitions, where
we want to choose k items out of n with replacement (the order is
not important). The formula is C(n + k - 1, k).
*/
cout << combinations(n + k - 1, k) << endl;
}
return 0;
}