-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2400.cpp
More file actions
24 lines (24 loc) · 703 Bytes
/
2400.cpp
File metadata and controls
24 lines (24 loc) · 703 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
class Solution {
public:
long long mod = 1e9 + 7;
long long inv(long long a) {
if (a == 1) return 1;
return (mod - mod / a) * inv(mod % a) % mod;
}
int comb(int n, int k) {
long long res = 1L;
for (int i = 0; i < k; ++i) {
res = res * (n - i) % mod;
res = res * inv(i + 1) % mod;
}
return (int)(res % mod);
}
int numberOfWays(int startPos, int endPos, int k) {
int diff = endPos - startPos;
if ((k + diff) & 1) return 0;
int forward = (k + diff) / 2;
int backward = (k - diff) / 2;
if (backward < 0) return 0;
return comb(k, min(forward, backward));
}
};