跳到正文

剪贴板

剪贴板 x84slyse

原文
剪贴板信息

剪贴板信息

收录
更新

自动取模!!!

C
template <typename T>  T power(T a, LL b) {
    T c{1}; for (; b; b /= 2, a *= a) if (b & 1) c *= a;
    return c;
}
template <int P> struct MInt {
    int x;
    MInt() : x{} {}
    MInt(LL x_) : x{norm(x_ % getMod())} {}
    static int Mod;
    static int getMod() { return P > 0 ? P : Mod; }
    static void setMod(int Mod_) { Mod = Mod_; }
    int up(int x) const {
        if (x < 0) x += getMod();
        return x;
    }
    int down(int x) const {
        if (x >= getMod()) x -= getMod();
        return x;
    }
    int norm(int x) const {
        return up(down(x));
    }
    int val() const { return x; }
    explicit  operator int() const { return x; }
    MInt operator-() const {
        MInt res; res.x = norm(getMod() - x); return res;
    }
    MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    MInt &operator+=(MInt rhs) & { return x = down(x + rhs.x), *this; }
    MInt &operator-=(MInt rhs) & { return x = up(x - rhs.x), *this; }
    MInt &operator*=(MInt rhs) & { return x = 1ll * x * rhs.x % getMod(), *this; }
    MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); }
    friend MInt operator+(MInt lhs, MInt rhs) { return lhs += rhs; }
    friend MInt operator-(MInt lhs, MInt rhs) { return lhs -= rhs; }
    friend MInt operator*(MInt lhs, MInt rhs) { return lhs *= rhs; }
    friend MInt operator/(MInt lhs, MInt rhs) { return lhs /= rhs; }
    friend bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); }
    friend  bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); }
    friend  std::istream &operator>>(std::istream &is, MInt &a) {
        LL x = 0; is >> x, a = MInt(x); return is;
    }
    friend  std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
};

using Z = MInt<mod>;
C
namespace modint {

template <typename T>  T power(T a, LL b) {
    T c{1}; for (; b; b /= 2, a *= a) if (b & 1) c *= a;
    return c;
}

template <int P> struct MInt {
    int x;
    MInt() : x{} {}
    MInt(LL x_) : x{norm(x_ % getMod())} {}
    static int Mod;
    static int getMod() { return P > 0 ? P : Mod; }
    static void setMod(int Mod_) { Mod = Mod_; }
    int up(int x) const {
        if (x < 0) x += getMod();
        return x;
    }
    int down(int x) const {
        if (x >= getMod()) x -= getMod();
        return x;
    }
    int norm(int x) const {
        return up(down(x));
    }
    int val() const { return x; }
    explicit  operator int() const { return x; }
    MInt operator-() const {
        MInt res; res.x = norm(getMod() - x); return res;
    }
    MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    MInt &operator+=(MInt rhs) & { return x = down(x + rhs.x), *this; }
    MInt &operator-=(MInt rhs) & { return x = up(x - rhs.x), *this; }
    MInt &operator*=(MInt rhs) & { return x = 1ll * x * rhs.x % getMod(), *this; }
    MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); }
    friend MInt operator+(MInt lhs, MInt rhs) { return lhs += rhs; }
    friend MInt operator-(MInt lhs, MInt rhs) { return lhs -= rhs; }
    friend MInt operator*(MInt lhs, MInt rhs) { return lhs *= rhs; }
    friend MInt operator/(MInt lhs, MInt rhs) { return lhs /= rhs; }
    friend bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); }
    friend  bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); }
    friend  std::istream &operator>>(std::istream &is, MInt &a) {
        LL x = 0; is >> x, a = MInt(x); return is;
    }
    friend  std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
};

template <typename Z> struct comb {
    std::vector<Z> fact, inv_fact;
    comb(int n) {
        fact.resize(n + 1, Z(1));
        inv_fact.resize(n + 1, Z(1));
        for (int i = 1; i <= n; i++) { fact[i] = fact[i - 1] * i; }
        inv_fact[n] = Z{1} / fact[n];
        for (int i = n - 1; i >= 0; i--) {
            inv_fact[i] = inv_fact[i + 1] * (i + 1);
        }
    }
    Z get(int n, int m) const {
        if (n < m || m < 0) return 0;
        return fact[n] * inv_fact[m] * inv_fact[n - m];
    }
};
}

using Z = modint::MInt<mod>;
using C = modint::comb<Z>;

C comb{2000 * 2 + 7};

Z get(int n, int m, int k){
    if(k >= min(n, m))return comb.get(n + m, m);
    else return comb.get(n + m, k);
}