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
| #include <algorithm> #include <iostream> using namespace std; typedef long long LL;
const int N = 100010;
LL qmi(LL a, LL b, LL c) { LL ans = 1; while (b) { if (b & 1) ans = ans * a % c; b >>= 1; a = a * a % c; } return ans; }
LL C(LL a, LL b, LL c) { if (a < b) return 0; LL ans = 1; for (int i = b + 1, j = 1;j <= a - b;j++, i++) { ans = ans * i % c; ans = (LL)ans * qmi(j, c - 2, c) % c; } return ans; }
LL lucas(LL a, LL b, LL c) { if (a < c) return C(a, b, c); return lucas(a / c, b / c, c) * C(a % c, b % c, c) % c; }
int main() { int n; cin >> n; while (n--) { LL a, b, c; cin >> a >> b >> c; cout << lucas(a, b, c) << endl; }
return 0; }
|