Mess 杂项算法

2021/10/19 11:10:12

本文主要是介绍Mess 杂项算法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Mess 杂项算法

Q_read/Q_write

inline int qr(){
    int x = 0, f = 1; char c = getchar();
    while(!isdigit(c)) { 
        if(c == '-') f = -f;
        c = getchar();
    }
    while(isdigit(c)) { 
        x = (x << 1) + (x << 3) + (c ^ 48); 
        c = getchar(); 
    }
    return x * f;
}
inline void qw(int a) {
    if(a < 0) putchar('-'), a = -a;
    if(a > 9) qw(a / 10);
    putchar((a % 10) ^ 48);
}

DEBUG

#define dbg(x) cerr << #x << "=" << x << '\n'

template<class t, class u>
ostream &operator<<(ostream &os, const pair<t, u> &p) {
    return os << "(" << p.first << ", " << p.second << ")";
}

template<class t>
ostream &operator<<(ostream &os, const vector<t> &v) {
    os << "[" << (*v.begin());
    for (int i = 1;i < v.size();i++) os << ", " << v[i];
    return os << "]";
}

Mint

constexpr int P = 1000000007;
using ll = long long;
// assume -P <= x < 2P
int norm(int x) {if(x < 0)x += P;if (x >= P)x -= P;return x;}
template<class T>T power(T a, int b){T res = 1;for (; b; b /= 2, a *= a)if (b & 1)res *= a;return res;}
struct Mint {
    int x;Mint(int x = 0) : x(norm(x)){}
    int val() const {return x;}
    Mint operator-() const {return Mint(norm(P - x));}
    Mint inv() const {assert(x != 0);return power(*this, P - 2);}
    Mint &operator*=(const Mint &rhs) { x = ll(x) * rhs.x % P; return *this;}
    Mint &operator+=(const Mint &rhs) { x = norm(x + rhs.x); return *this;}
    Mint &operator-=(const Mint &rhs) { x = norm(x - rhs.x); return *this;}
    Mint &operator/=(const Mint &rhs) {return *this *= rhs.inv();}
    friend Mint operator*(const Mint &lhs, const Mint &rhs) {Mint res = lhs; res *= rhs; return res;}
    friend Mint operator+(const Mint &lhs, const Mint &rhs) {Mint res = lhs; res += rhs; return res;}
    friend Mint operator-(const Mint &lhs, const Mint &rhs) {Mint res = lhs; res -= rhs; return res;}
    friend Mint operator/(const Mint &lhs, const Mint &rhs) {Mint res = lhs; res /= rhs; return res;}
};

Unordered_map/set Hash

struct haha {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

Random

随机数生成

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

随机打乱

random_shuffle(x.begin(), x.end());


这篇关于Mess 杂项算法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程