← All writing

iSaber的算法模板 (13)

C++ 工具与整数域

分篇:1 · 2 · 3 · 4 · 5 · 6 · 7 · 8 · 9 · 10 · 11 · 12 · 13

6 杂项

6.0 重载 (Overload)

priority_queue:

默认大根堆

cpp
struct node
{
    int x;
    bool operator < (const node b) const
    {
        return x < b.x; //大根堆
        // return x > b.x; //小根堆
    }
};

priority_queue <int, vector <int>, greater<int> > q; //小根堆(greater升序)
priority_queue <int, vector <int>, less<int> > q; //大根堆(less降序)

6.1 内建函数

cpp
//返回x的最后一位1的是从后向前第几位,比如7368(1110011001000)返回4。
int __builtin_ffs (unsigned int x)
    
//返回前导的0的个数 (count leading zero)
int __builtin_clz (unsigned int x)

//返回末尾的0的个数,和__builtin_clz相对 (count tr)
int __builtin_ctz (unsigned int x)
    
//返回二进制表示中1的个数。
int __builtin_popcount (unsigned int x)
    
//返回x的奇偶校验位,也就是x的1的个数模2的结果。
int __builtin_parity (unsigned int x)
    
//返回n的最高位1所在的位是第几位(下标从0开始) 要求n > 0
//__lg(1) == 0
int __lg(int __n)   

6.2 编译器优化

cpp
#pragma GCC optimize(2)  //O2
#pragma GCC optimize(3,"Ofast","inline")  //O3

6.3 整数域

cpp
// assert -mod <= x < 2 * mod
int norm(int x)
{
    if(x < 0)
        x += mod;
    if(x >= mod)
        x -= mod;
    return x;
}
//模质数mod下的整数域(mod是质数才能使所有非零数存在逆元)
struct Z
{
    int x;
    Z(int x = 0):x(norm(x)) {};
    Z(ll x): x(norm(x % mod)) {};
    int val() const 
    {
        return x;
    }
    Z operator -() const 
    {
        return Z(norm(mod - x));
    }
    Z & operator +=(const Z &rhs) 
    {
        x = norm(x + rhs.x);
        return *this;
    }
    Z & operator -= (const Z &rhs)
    {
        x = norm(x - rhs.x);
        return *this;
    }
    Z & operator *= (const Z &rhs)
    {
        x = ll(x) * rhs.x % mod;
        return *this;
    }
    Z & operator /= (const  Z &rhs)
    {
        return *this *= rhs.inv();
    }
    friend Z operator +(const Z &lhs, const Z &rhs)
    {
        Z res = lhs;
        res += rhs;
        return res;
    }
    friend Z operator -(const Z &lhs, const Z &rhs)
    {
        Z res = lhs;
        res -= rhs;
        return res;
    }
    friend Z operator *(const Z &lhs, const Z &rhs)
    {
        Z res = lhs;
        res *= rhs;
        return res;
    }
    friend Z operator /(const Z &lhs, const Z &rhs)
    {
        Z res = lhs;
        res /= rhs;
        return res;
    }
    Z pow(int p) const
    {
        if(x == 0)
            return 0;
        Z res = 1, a = x;
        while(p)
        {
            if(p & 1)
                res *= a;
            a *= a;
            p >>= 1;
        }
        return res;
    }
    Z inv() const
    {
        assert(x != 0);
        return (*this).pow(mod - 2);
    }
    friend istream & operator >> (istream &in, Z &rhs)
    {
        ll v;
        in >> v;
        rhs = Z(v);
        return in;
    }
    friend ostream & operator << (ostream &out, const Z &rhs)
    {
        return out << rhs.x;
    }
};


最后,如果发现有错误恳请指正


← 上一篇

← Back to ArchiveBack to top ↑

Comments

No approved comments yet.

Your display name and plain-text comment become public only after owner approval. No account or email is required.