ABC145 D - Knight
Table of contents
# Problem
https://atcoder.jp/contests/abc145/tasks/abc145_d
Given integers , , report the number of ways from to .
You can move to only either or .
If impossible, report 0.
# Explanation
For instance, _{5}C_
Inverse is computed with Extended Euclidean algorithm in .
# Time complexity
# Solution
Int X_, Y_;
Int fac[MAX_M];
void input() {
cin >> X_ >> Y_;
}
void buildFact() {
fac[0] = fac[1] = 1;
for (int i = 2; i < MAX_M; i++){
fac[i] = fac[i - 1] * i % MOD;
}
}
// Extended Euclid
Int modinv(Int a, Int p) {
Int b = p, u = 1, v = 0;
while (b) {
Int t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= p;
if (u < 0) u += p;
return u;
}
Int nCk(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * modinv((fac[k] * fac[n - k] % MOD), MOD) % MOD;
}
void solve() {
buildFact();
if ((X_ + Y_) % 3 != 0) {
cout << 0 << endl;
return;
}
Int m = (2 * X_ - Y_) / 3;
Int n = (Y_ - m)/2;
if (m < 0 || n < 0) {
cout << 0 << endl;
return;
}
cout << nCk(m + n, n) << endl;
}
int main(void) {
input();
solve();
return 0;
}
Shun
Remote freelancer. A web and mobile application enginner.
Traveling around the world based on East Asia.
I'm looking forward to your job offers from all over the world!