This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/binomial_coefficient"
#include <cstdint>
#include <map>
#include <numeric>
#include <vector>
using namespace std;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
#include <cstdio>
#include <cstring>
#include <string>
#include <type_traits>
#include "../util/fastio.cpp"
#include "../math/CRT.cpp"
#include "../math/prime/primefactor.cpp"
#include "../math/binom_mod_prime_power.cpp"
ll brute(ll n, ll k, ll mod) {
if (k < 0 || k > n) return 0;
vector<vector<ll>> dp(n + 1, vector<ll>(k + 1));
dp[0][0] = 1 % mod;
for (ll i = 0; i < n; ++i) {
for (ll j = 0; j <= min(i, k); ++j) {
dp[i + 1][j] += dp[i][j];
dp[i + 1][j] %= mod;
if (j != k) {
dp[i + 1][j + 1] += dp[i][j];
dp[i + 1][j + 1] %= mod;
}
}
}
return dp[n][k];
}
int main() {
{
map<ll, BinomModPrimePower> cache;
for (int mod = 2; mod <= 120; ++mod) {
auto pf = prime_factor<ll>(mod);
vector<pair<ll, int>> fac;
for (ll p : pf) {
if (fac.empty() || fac.back().first != p) fac.emplace_back(p, 1);
else fac.back().second++;
}
for (int n = 0; n <= 20; ++n) {
for (int k = 0; k <= n; ++k) {
vector<pair<ll, ll>> rem;
for (auto [p, e] : fac) {
ll pe = 1;
for (int i = 0; i < e; ++i) pe *= p;
auto it = cache.find(pe);
if (it == cache.end()) it = cache.emplace(pe, BinomModPrimePower(p, e)).first;
rem.emplace_back(it->second.C(n, k), pe);
}
if (CRT(rem).first != brute(n, k, mod)) return 1;
}
}
}
}
Scanner sc;
Printer pr;
int t;
ll mod;
sc.read(t, mod);
auto pf = prime_factor<ll>(mod);
vector<pair<ll, int>> fac;
for (ll p : pf) {
if (fac.empty() || fac.back().first != p) fac.emplace_back(p, 1);
else fac.back().second++;
}
vector<BinomModPrimePower> binoms;
vector<ll> mods;
for (auto [p, e] : fac) {
binoms.emplace_back(p, e);
mods.emplace_back(binoms.back().modulus());
}
while (t--) {
ll n, k;
sc.read(n, k);
vector<pair<ll, ll>> rem;
for (int i = 0; i < (int)binoms.size(); ++i) {
rem.emplace_back(binoms[i].C(n, k), mods[i]);
}
pr.println(CRT(rem).first);
}
return 0;
}#line 1 "test/yosupo_binomial_coefficient.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/binomial_coefficient"
#include <cstdint>
#include <map>
#include <numeric>
#include <vector>
using namespace std;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
#include <cstdio>
#include <cstring>
#include <string>
#include <type_traits>
#line 1 "util/fastio.cpp"
using namespace std;
extern "C" int fileno(FILE *);
extern "C" int isatty(int);
template<class T, class = void>
struct is_fastio_range : false_type {};
template<class T>
struct is_fastio_range<T, void_t<decltype(declval<T &>().begin()), decltype(declval<T &>().end())>> : true_type {};
template<class T, class = void>
struct has_fastio_value : false_type {};
template<class T>
struct has_fastio_value<T, void_t<decltype(declval<const T &>().value())>> : true_type {};
struct FastIoDigitTable {
char num[40000];
constexpr FastIoDigitTable() : num() {
for (int i = 0; i < 10000; ++i) {
int x = i;
for (int j = 3; j >= 0; --j) {
num[i * 4 + j] = char('0' + x % 10);
x /= 10;
}
}
}
};
struct Scanner {
static constexpr int BUFSIZE = 1 << 17;
static constexpr int OFFSET = 64;
char buf[BUFSIZE + 1];
int idx, size;
bool interactive;
Scanner() : idx(0), size(0), interactive(isatty(fileno(stdin))) {}
inline void load() {
int len = size - idx;
memmove(buf, buf + idx, len);
if (interactive) {
if (fgets(buf + len, BUFSIZE + 1 - len, stdin)) size = len + (int)strlen(buf + len);
else size = len;
} else {
size = len + (int)fread(buf + len, 1, BUFSIZE - len, stdin);
}
idx = 0;
buf[size] = 0;
}
inline void ensure() {
if (idx + OFFSET > size) load();
}
inline void ensure_interactive() {
if (idx == size) load();
}
inline char skip() {
if (interactive) {
ensure_interactive();
while (buf[idx] && buf[idx] <= ' ') {
++idx;
ensure_interactive();
}
return buf[idx++];
}
ensure();
while (buf[idx] && buf[idx] <= ' ') {
++idx;
ensure();
}
return buf[idx++];
}
template<class T, typename enable_if<is_integral<T>::value, int>::type = 0>
void read(T &x) {
if (interactive) {
char c = skip();
bool neg = false;
if constexpr (is_signed<T>::value) {
if (c == '-') {
neg = true;
ensure_interactive();
c = buf[idx++];
}
}
x = 0;
while (c >= '0') {
x = x * 10 + (c & 15);
ensure_interactive();
c = buf[idx++];
}
if constexpr (is_signed<T>::value) {
if (neg) x = -x;
}
return;
}
char c = skip();
bool neg = false;
if constexpr (is_signed<T>::value) {
if (c == '-') {
neg = true;
c = buf[idx++];
}
}
x = 0;
while (c >= '0') {
x = x * 10 + (c & 15);
c = buf[idx++];
}
if constexpr (is_signed<T>::value) {
if (neg) x = -x;
}
}
template<class T, typename enable_if<!is_integral<T>::value && !is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value && has_fastio_value<T>::value, int>::type = 0>
void read(T &x) {
long long v;
read(v);
x = T(v);
}
template<class Head, class Next, class... Tail>
void read(Head &head, Next &next, Tail &...tail) {
read(head);
read(next, tail...);
}
template<class T, class U>
void read(pair<T, U> &p) {
read(p.first, p.second);
}
template<class T, typename enable_if<is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value, int>::type = 0>
void read(T &a) {
for (auto &x : a) read(x);
}
void read(char &c) {
c = skip();
}
void read(string &s) {
s.clear();
if (interactive) {
ensure_interactive();
while (buf[idx] && buf[idx] <= ' ') {
++idx;
ensure_interactive();
}
while (true) {
int start = idx;
while (idx < size && buf[idx] > ' ') ++idx;
s.append(buf + start, idx - start);
if (idx < size) break;
load();
if (size == 0) break;
}
if (idx < size) ++idx;
return;
}
ensure();
while (buf[idx] && buf[idx] <= ' ') {
++idx;
ensure();
}
while (true) {
int start = idx;
while (idx < size && buf[idx] > ' ') ++idx;
s.append(buf + start, idx - start);
if (idx < size) break;
load();
}
if (idx < size) ++idx;
}
};
struct Printer {
static constexpr int BUFSIZE = 1 << 17;
static constexpr int OFFSET = 64;
char buf[BUFSIZE];
int idx;
bool interactive;
inline static constexpr FastIoDigitTable table{};
Printer() : idx(0), interactive(isatty(fileno(stdout))) {}
~Printer() { flush(); }
inline void flush() {
if (idx) {
fwrite(buf, 1, idx, stdout);
idx = 0;
}
}
inline void pc(char c) {
if (idx > BUFSIZE - OFFSET) flush();
buf[idx++] = c;
if (interactive && c == '\n') flush();
}
inline void print_range(const char *s, size_t n) {
size_t pos = 0;
while (pos < n) {
if (idx == BUFSIZE) flush();
size_t chunk = min(n - pos, (size_t)(BUFSIZE - idx));
memcpy(buf + idx, s + pos, chunk);
idx += (int)chunk;
pos += chunk;
}
}
void print(const char *s) {
print_range(s, strlen(s));
}
void print(const string &s) {
print_range(s.data(), s.size());
}
void print(char c) {
pc(c);
}
void print(bool b) {
pc(char('0' + (b ? 1 : 0)));
}
template<class T, typename enable_if<is_integral<T>::value && !is_same<T, bool>::value, int>::type = 0>
void print(T x) {
if (idx > BUFSIZE - 100) flush();
using U = typename make_unsigned<T>::type;
U y;
if constexpr (is_signed<T>::value) {
if (x < 0) {
buf[idx++] = '-';
y = U(0) - static_cast<U>(x);
} else {
y = static_cast<U>(x);
}
} else {
y = x;
}
if (y == 0) {
buf[idx++] = '0';
return;
}
static constexpr int TMP_SIZE = sizeof(U) * 10 / 4;
char tmp[TMP_SIZE];
int pos = TMP_SIZE;
while (y >= 10000) {
pos -= 4;
memcpy(tmp + pos, table.num + (y % 10000) * 4, 4);
y /= 10000;
}
if (y >= 1000) {
memcpy(buf + idx, table.num + (y << 2), 4);
idx += 4;
} else if (y >= 100) {
memcpy(buf + idx, table.num + (y << 2) + 1, 3);
idx += 3;
} else if (y >= 10) {
unsigned q = (unsigned(y) * 205) >> 11;
buf[idx] = char('0' + q);
buf[idx + 1] = char('0' + (unsigned(y) - q * 10));
idx += 2;
} else {
buf[idx++] = char('0' + y);
}
memcpy(buf + idx, tmp + pos, TMP_SIZE - pos);
idx += TMP_SIZE - pos;
}
template<class T, typename enable_if<!is_integral<T>::value && !is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value && has_fastio_value<T>::value, int>::type = 0>
void print(const T &x) {
print(x.value());
}
template<class T, typename enable_if<is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value, int>::type = 0>
void print(const T &a) {
bool first = true;
for (auto &&x : a) {
if (!first) pc(' ');
first = false;
print(x);
}
}
template<class T>
void println(const T &x) {
print(x);
pc('\n');
}
template<class Head, class... Tail>
void println(const Head &head, const Tail &...tail) {
print(head);
((pc(' '), print(tail)), ...);
pc('\n');
}
void println() {
pc('\n');
}
};
template<class T>
Scanner &operator>>(Scanner &in, T &x) {
in.read(x);
return in;
}
template<class T>
Printer &operator<<(Printer &out, const T &x) {
out.print(x);
return out;
}
/**
* @brief 高速入出力(Fast IO)
*/
#line 1 "math/extgcd.cpp"
template<typename T>
T extgcd(T a, T b, T &x ,T &y){
for (T u = y = 1, v = x = 0; a; ) {
ll q = b/a;
swap(x -= q*u, u);
swap(y -= q*v, v);
swap(b -= q*a, a);
}
return b;
}
/**
* @brief 拡張ユークリッド互除法(Extended GCD)
*/
#line 2 "math/CRT.cpp"
pair<ll, ll> CRT(const vector<pair<ll, ll>> &a){
ll R = 0, M = 1;
for (auto &&i : a) {
ll r = (i.first+i.second)%i.second, m = i.second;
if(m < M) swap(r, R), swap(m, M);
if(M%m == 0){
if(R % m != r) return {};
continue;
}
ll p, q;
ll g = extgcd(M, m, p, q); // p = inv(M') mod m'
ll mm = m/g;
if((r-R)%g) return {0, 0};
ll x = (r-R)/g % mm * p % mm;
R += x*M;
M *= mm;
if(R < 0) R += M;
}
return {R, M};
}
/**
* @brief 中国剰余定理(Chinese Remainder Theorem)
*/
#line 1 "math/prime/primefactor.cpp"
template<typename T>
struct ExactDiv {
T t, i, val;
ExactDiv() {}
ExactDiv(T n) : t(T(-1) / n), i(mul_inv(n)) , val(n) {};
T mul_inv(T n) {
T x = n;
for (int i = 0; i < 5; ++i) x *= 2 - n * x;
return x;
}
bool divide(T n) const {
if(val == 2) return !(n & 1);
return n * this->i <= this->t;
}
};
vector<ExactDiv<uint>> get_prime(int n){
if(n <= 1) return vector<ExactDiv<uint>>();
vector<bool> is_prime(n+1, true);
vector<ExactDiv<uint>> prime;
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; ++i) {
if(is_prime[i]) prime.emplace_back(i);
for (auto &&j : prime){
if(i*j.val > n) break;
is_prime[i*j.val] = false;
if(j.divide(i)) break;
}
}
return prime;
}
const auto primes = get_prime(32000);
template<class T>
vector<T> prime_factor(T n){
vector<T> res;
for (auto &&i : primes) {
while (i.divide(n)){
res.emplace_back(i.val);
n /= i.val;
}
}
if(n != 1) res.emplace_back(n);
return res;
}
/**
* @brief 素因数分解(試し割り)
*/
#line 1 "math/modinv.cpp"
template<typename T>
T mod_inv(T x, T M){
T u = 1, t = 1, v = 0, s = 0, m = M;
while (x) { T q = m/x; swap(s -= q*u, u); swap(t -= q*v, v); swap(m -= q*x, x); }
if(s < 0) s += M;
return s;
}
#line 2 "math/binom_mod_prime_power.cpp"
struct BinomModPrimePower {
ll p, mod;
int q;
ll block_prod;
vector<ll> ppow;
vector<int> prod;
explicit BinomModPrimePower(ll prime, int exponent) : p(prime), mod(1), q(exponent), ppow(exponent + 1, 1) {
for (int i = 0; i < q; ++i) {
mod *= p;
ppow[i + 1] = mod;
}
block_prod = (p == 2 && q >= 3 ? 1 : mod - 1);
prod.assign(mod + 1, 1);
for (int i = 1; i <= mod; ++i) {
prod[i] = prod[i - 1];
if (i % p != 0) prod[i] = (ull)prod[i] * i % mod;
}
}
pair<ll, ll> factorial(ll n) const {
ll x = 1, e = 0;
while (n) {
if (block_prod != 1 && (n / mod) & 1) x = mod - x;
x = (ull)x * prod[n % mod] % mod;
n /= p;
e += n;
}
return {x, e};
}
ll C(ll n, ll k) const {
if (k < 0 || k > n) return 0;
auto [a, ea] = factorial(n);
auto [b, eb] = factorial(k);
auto [c, ec] = factorial(n - k);
ll e = ea - eb - ec;
if (e >= q) return 0;
ll x = (ull)b * c % mod;
return (ull)a * mod_inv(x, mod) % mod * ppow[e] % mod;
}
ll modulus() const {
return mod;
}
};
/**
* @brief 二項係数(mod p^q)
*/
#line 21 "test/yosupo_binomial_coefficient.test.cpp"
ll brute(ll n, ll k, ll mod) {
if (k < 0 || k > n) return 0;
vector<vector<ll>> dp(n + 1, vector<ll>(k + 1));
dp[0][0] = 1 % mod;
for (ll i = 0; i < n; ++i) {
for (ll j = 0; j <= min(i, k); ++j) {
dp[i + 1][j] += dp[i][j];
dp[i + 1][j] %= mod;
if (j != k) {
dp[i + 1][j + 1] += dp[i][j];
dp[i + 1][j + 1] %= mod;
}
}
}
return dp[n][k];
}
int main() {
{
map<ll, BinomModPrimePower> cache;
for (int mod = 2; mod <= 120; ++mod) {
auto pf = prime_factor<ll>(mod);
vector<pair<ll, int>> fac;
for (ll p : pf) {
if (fac.empty() || fac.back().first != p) fac.emplace_back(p, 1);
else fac.back().second++;
}
for (int n = 0; n <= 20; ++n) {
for (int k = 0; k <= n; ++k) {
vector<pair<ll, ll>> rem;
for (auto [p, e] : fac) {
ll pe = 1;
for (int i = 0; i < e; ++i) pe *= p;
auto it = cache.find(pe);
if (it == cache.end()) it = cache.emplace(pe, BinomModPrimePower(p, e)).first;
rem.emplace_back(it->second.C(n, k), pe);
}
if (CRT(rem).first != brute(n, k, mod)) return 1;
}
}
}
}
Scanner sc;
Printer pr;
int t;
ll mod;
sc.read(t, mod);
auto pf = prime_factor<ll>(mod);
vector<pair<ll, int>> fac;
for (ll p : pf) {
if (fac.empty() || fac.back().first != p) fac.emplace_back(p, 1);
else fac.back().second++;
}
vector<BinomModPrimePower> binoms;
vector<ll> mods;
for (auto [p, e] : fac) {
binoms.emplace_back(p, e);
mods.emplace_back(binoms.back().modulus());
}
while (t--) {
ll n, k;
sc.read(n, k);
vector<pair<ll, ll>> rem;
for (int i = 0; i < (int)binoms.size(); ++i) {
rem.emplace_back(binoms[i].C(n, k), mods[i]);
}
pr.println(CRT(rem).first);
}
return 0;
}