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());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值