首页 > 其他分享 >有理数逆元板子

有理数逆元板子

时间:2022-11-10 04:11:06浏览次数:61  
标签:const rhs 板子 return 逆元 bool rational operator 有理数

template <int M = 1000000007>
struct rational{
    ll p, q;
    rational(ll p = 0, ll q = 1):p(p), q(q){}
    rational operator + (const rational &rhs) const{
        return rational(p * rhs.q + q * rhs.p, q * rhs.q);
    }
    rational operator - (const rational &rhs) const{
        return rational(p * rhs.q - q * rhs.p, q * rhs.q);
    }
    rational operator * (const rational &rhs) const{
        return rational(p * rhs.p, q * rhs.q);
    }
    rational operator / (const rational &rhs) const{
        return rational(p * rhs.q, q * rhs.p);
    }
    bool operator < (const rational &rhs) const{
        return p * rhs.q < q * rhs.p;
    }
    bool operator > (const rational &rhs) const{
        return p * rhs.q > q * rhs.p;
    }
    bool operator == (const rational &rhs) const{
        return p * rhs.q == q * rhs.p;
    }
    bool operator != (const rational &rhs) const{
        return p * rhs.q != q * rhs.p;
    }
    bool operator <= (const rational &rhs) const{
        return p * rhs.q <= q * rhs.p;
    }
    bool operator >= (const rational &rhs) const{
        return p * rhs.q >= q * rhs.p;
    }
    void format(){
        ll g = __gcd(p, q);
        p /= g;
        q /= g;
    }
    friend ostream& operator<<(ostream & os, rational rhs) {
        rhs.format();
        os << rhs.p << "/" << rhs.q << endl;
    }
    ll inv(){
        format();
        return (p * quickPow(q, mod - 2)) % mod;
    }
};

 

标签:const,rhs,板子,return,逆元,bool,rational,operator,有理数
From: https://www.cnblogs.com/tiany7/p/16875781.html

相关文章

  • kmp板子
    主串a,模式串b,求b在a中出现的位置 #include<iostream>#include<algorithm>#include<cstring>usingnamespacestd;constintN=1e6+4;intla,lb;intf[N],p[N]......
  • Hash表板子
    #include<bits/stdc++.h>usingnamespacestd;#definelllonglong#defineullunsignedlonglongullrx=1e9+7;llrand(llx,lly){rx*=998244353;returnrx%(y-x+......
  • 1034 有理数四则运算
    本题要求编写程序,计算2个有理数的和、差、积、商。输入格式:输入在一行中按照 a1/b1a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号......
  • 普及-的并查集(都是板子)
        #include<bits/stdc++.h>usingnamespacestd;constintN=1e5+7;structNode{ intbn,ed,t;}a[N];intf[N];intfind(intx){returnx==f[x]?x:......
  • rmq板子
    typedeflonglongLL;constexprinlineintlg2(LLx){returnsizeof(LL)*8-1-__builtin_clzll(x);}template<typenameT,size_tN,size_tK>structRangeQ......
  • 树状数组的板子
    该数据结构可以维护序列的前缀和 1.单点修改,求区间和#include<iostream>usingnamespacestd;constintN=5e5+2;intn,tr[N];intlowbit(intx){retu......
  • st表板子
    constintN=1e5+2;intst[N][20],n,a[N];voidinit(){inti,j;for(i=1;i<=n;i++)st[i][0]=a[i];for(j=1;j<20;j++)for(i=1;i+(1<<j)-1<......
  • 线段树的一些板子
    有2种方式,都是用的lazy标记,但具体用法不同 1)标记永久化 假设现在需要1.区间加值2.求区间和  #include<iostream>#include<algorithm>usingnamespacestd......
  • luogu 1908 逆序对板子
     逆序对的本质是二维偏序 给第一维排序(输入时已排好),统计y(k)>=y(i)k<i的个数用树状数组维护y值前缀和,需要的时候直接查询该题需要离散化这个y,再作为树状数组......
  • C++ 的有理数类 Rational 实现
    classRational{staticinlineintgcd(inta,intb){if(!b)returnabs(a);while((a%=b)&&(b%=a));//doinwhileret......