首页 > 其他分享 >取模+组合数

取模+组合数

时间:2024-07-24 12:07:32浏览次数:5  
标签:取模 return 组合 int res i64 constexpr MInt

jiangly的板子

//------取模机------//
using i64 = long long;
template<class T>
constexpr T power(T a, i64 b) {
    T res {1};
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
} // 快速幂

constexpr i64 mul(i64 a, i64 b, i64 p) {
    i64 res = a * b - i64(1.L * a * b / p) * p;
    res %= p;
    if (res < 0) {
        res += p;
    }
    return res;
} // 取模乘

template<i64 P>
struct MInt {
    i64 x;
    constexpr MInt() : x {0} {}
    constexpr MInt(i64 x) : x {norm(x % getMod())} {}

    static i64 Mod;
    constexpr static i64 getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(i64 Mod_) {
        Mod = Mod_;
    }//只有P<=0, setMod才生效
    constexpr i64 norm(i64 x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr i64 val() const {
        return x;
    }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MInt inv() const {
        return power(*this, getMod() - 2);
    }
    constexpr MInt &operator*=(MInt rhs) & {
        if (getMod() < (1ULL << 31)) {
            x = x * rhs.x % int(getMod());
        } else {
            x = mul(x, rhs.x, getMod());
        }
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) & {
        return *this *= rhs.inv();
    }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
    friend constexpr bool operator<(MInt lhs, MInt rhs) {
        return lhs.val() < rhs.val();
    }
};

template<>
i64 MInt<0>::Mod = 998244353; //只有P<=0, Mod才生效

constexpr int P = 998244353; //在这设置要用的模数
using Z = MInt<P>;
//------取模机------//
//----计算组合数----//
struct Comb {
    int n;
    std::vector<Z> _fac; //阶乘
    std::vector<Z> _invfac; //阶乘的逆元
    std::vector<Z> _inv; //数字的逆元

    Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {}
    Comb(int n) : Comb() {
        init(n);
    }

    void init(int m) {
        m = std::min<i64>(m, Z::getMod() - 1);
        if (m <= n) return;
        _fac.resize(m + 1);
        _invfac.resize(m + 1);
        _inv.resize(m + 1);

        for (int i = n + 1; i <= m; i++) {
            _fac[i] = _fac[i - 1] * i;
        }
        _invfac[m] = _fac[m].inv();
        for (int i = m; i > n; i--) {
            _invfac[i - 1] = _invfac[i] * i;
            _inv[i] = _invfac[i] * _fac[i - 1];
        }
        n = m;
    }

    Z fac(int m) {
        if (m > n) init(2 * m);
        return _fac[m];
    }
    Z invfac(int m) {
        if (m > n) init(2 * m);
        return _invfac[m];
    }
    Z inv(int m) {
        if (m > n) init(2 * m);
        return _inv[m];
    }
    Z C(int n, int m) {
        if (n < m || m < 0) return 0;
        return fac(n) * invfac(m) * invfac(n - m);
    }
    Z A(int n, int m) {
        if (n < m || m < 0 ) return 0;
        return fac(n) * invfac(m);
    }
} comb;
//----计算组合数----//

标签:取模,return,组合,int,res,i64,constexpr,MInt
From: https://www.cnblogs.com/Kescholar/p/18320586

相关文章

  • 代码随想录算法训练营第40天 | 完全背包问题:完全背包基础理论、518.零钱兑换II、377.
    完全背包基础理论https://programmercarl.com/背包问题理论基础完全背包.html#其他语言版本卡码网完全背包例题https://kamacoder.com/problempage.php?pid=1052518.零钱兑换IIhttps://leetcode.cn/problems/coin-change-ii/description/代码随想录https://programmercarl......
  • Verilog HDL 的简单组合逻辑设计代码学习记录
    1.比较数据a和b,若两个数据相同则输出1,否则输出0(a、b均为单比特)看需求就简单设置输入a,b,输出o。modulecompare(a,b,o);inputa;inputb;outputo;//先来第一种写法,使用?:,这里是默认全是wire类型assigno=(a==b)?1'b1:1'b0;//第二种写法,使用ifelserego;alwa......
  • # 代码随想录算法训练营第38天 | 01背包问题:1049.最后一块石头的重量II(最多能装多少)、
    1049.最后一块石头的重量IIhttps://leetcode.cn/problems/last-stone-weight-ii/代码随想录https://programmercarl.com/1049.最后一块石头的重量II.html#算法公开课494.目标和https://leetcode.cn/problems/target-sum/description/代码随想录https://programmercarl.com......
  • spring使用mysql数据库实现关键字别字、拼音、拼音首字母、拼音所有首字母组合搜索
    1、实现思路前端传入的文字、拼音、别字、拼音首字母、拼音所有首字母组合传入到后台,通过后台接口转成拼音,然后通过转换后的拼音结合sql语句查询匹配。2、后台实现pom配置:<!--中文转拼音--><dependency><groupId>com.belerweb</groupId><artifactId>pinyin4......
  • 将三个经过训练的二元分类模型组合成 keras 中的单个多分类模型
    我有三个经过训练的二元分类模型,它们在输出层使用sigmoid激活进行训练。第一个模型返回从0到1的概率标量,以检查图像是否为数字零或不是。第二个模型返回从0到1的概率标量来检查图像是否是数字ONE或否。第三个模型返......
  • Oracle SQL:多字段组合去重技巧详解
    在Oracle数据库中,如果你需要根据多个字段的组合进行去重,最常用的几种方法是使用 DISTINCT 关键字、GROUPBY 子句,或者使用窗口函数 ROW_NUMBER()。1.使用 DISTINCT 关键字如果想要获取所有唯一的记录组合,可以使用 DISTINCT 关键字。这将返回指定列的唯一组合。SE......
  • Linux C++ 065-设计模式之组合模式
    LinuxC++065-设计模式之组合模式本节关键字:Linux、C++、设计模式、组合模式相关库函数:概念组合模式(CompositePattern),又叫做部分-整体模式,使得用户对单个对象和组合对象的使用具有一致性。它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理......
  • 16-setup()与组合式API-computed-watch
    16-setup()与组合式API-computed-watch一、setup()1、基本使用:setup()钩子是在组件中使用组合式API的入口,通常只在以下情况下使用:需要在非单文件组件中使用组合式API时。需要在基于选项式API的组件中集成基于组合式API的代码时。<script>import{ref}......
  • 【组合总和】python刷题记录
    目录思路:回溯法框架:本题中(元素不可重复可复选)如果不重复使用重复使用代码:​拓展1:元素无重复不可复选子集问题:组合问题:全排列问题:拓展2:元素可重复不可复选再--子集问题:PS:润到递归了。下面是超级回溯大法!!!!!思路:使用回溯法解决问题----能够穷举所有解回溯法框架:......
  • ORACLE vs MySQL 对组合索引包含IN LIST执行计划研究(ORACLE部分)_PART1
    本文主要研究下组合索引包含in条件(多个值),在单表查询,关联查询这两种SQL查询结果在ORACLE和MySQL里的区别。ORACLE具有强大的优化器,一般来说,组合索引在ORACLE里不管是单表还是关联查询,都可以选择optimal的执行计划,只要统计信息等是准确的。MySQL的优化器相对来说,要弱不少,很多功能不......