Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- constexpr int NMAX = 5e5 + 3;
- constexpr int MOD = 1e9 + 7;
- int fact[NMAX] = {1};
- void precomputeFact() {
- for (int i = 1; i < NMAX; i++) {
- fact[i] = 1LL * i * fact[i - 1] % MOD;
- }
- }
- int logpow(int base, int exp) {
- int ans = 1;
- while (exp) {
- if (exp & 1) {
- ans = 1LL * ans * base % MOD;
- }
- base = 1LL * base * base % MOD;
- exp >>= 1;
- }
- return ans;
- }
- int getComb(int n, int k) {
- if (!k || !n) {
- return 1;
- }
- return 1LL * fact[n] * logpow(1LL * fact[k] * fact[n - k] % MOD, MOD - 2) % MOD;
- }
- void solve() {
- int n, k;
- cin >> n >> k;
- cout << getComb(n, k) << endl;
- }
- signed main() {
- int t(1);
- cin >> t;
- precomputeFact();
- while (t--) {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement