板子函数

· · 个人记录

技巧

1.整数快读

inline int read(){
    int x = 0, f = 1; char c = getchar();
    while (c < '0' || c > '9') {if (c == '-') f = -1;c = getchar(); }
    while (c >= '0' && c <= '9') {x = x * 10 + c - 48; c = getchar(); }
    return x * f;
}

2.整数快写

inline void write(ll x){
    ll x2 = x;
    if (x2 < 0) {putchar('-'); x2 = -x; }
    if (x2 > 9) write(x2 / 10);
    putchar(x % 10 + '0');
}

3.字符串快读

inline string sread(){
    string s = "";
    char c = getchar();
    while (c == ' ' || c == '\n' || c == '\r') c = getchar();
    while (c != ' ' && c != '\n' && c != '\r') {
        s += c;
        c = getchar();
    }
    return s;
}

4.字符串快写

inline void swrite(string s){
    for (int i = 0; s[i] != '\0'; i++) putchar(s[i]);
}

5.cin, cout加速

ios::sync_with_stdio(false);

6.高精

typedef long long ll;
const ll maxlen = 20000;
inline string sread() {
    string s = "";
    char c = getchar();
    while (c != ' ' && c != '\n' && c != '\r') {
        s += c;
        c = getchar();
    }
    return s;
}
struct bigint {
    bool isneg;
    string s;
};
void read(bigint& a) {
    char op;
    string tmp;
    cin >> op;
    tmp = sread();
    if (op == '-') {
        if (tmp == "0") a.isneg = false;
        else a.isneg = true;
        a.s = tmp;
    }
    else {
        a.isneg = false;
        a.s = op + tmp;
    }
}
void write(bigint a) {
    if (a.isneg && a.s != "0") cout << '-';
    cout << a.s;
}
bool operator == (const bigint& x, const bigint& y) {
    if (x.s == "0" && y.s == "0") return true;
    if (x.isneg ^ y.isneg) return false;
    if (x.s != y.s) return false;
    return true;
}
bool operator != (const bigint& x, const bigint& y) {
    if (x.isneg ^ y.isneg) return true;
    if (x.s != y.s) return true;
    return false;
}
bool operator > (const bigint& x, const bigint& y) {
    if (x.s == "0" && y.s == "0") return false;
    if (x.isneg && !y.isneg) return false;
    if (!x.isneg && y.isneg) return true;
    if (x.isneg) {
        if (x.s.length() > y.s.length()) return false;
        if (x.s.length() < y.s.length()) return true;       
        for (ll i = 0; i < x.s.length(); i++) {
            if (x.s[i] > y.s[i]) return false;
            else if (x.s[i] < y.s[i]) return true;
        }
        return false;
    }
    else {
        if (x.s.length() > y.s.length()) return true;
        if (x.s.length() < y.s.length()) return false;
        for (ll i = 0; i < x.s.length(); i++) {
            if (x.s[i] > y.s[i]) return true;
            else if (x.s[i] < y.s[i]) return false;
        }
        return false;
    }
}
bool operator < (const bigint& x, const bigint& y) {
    if (x.s == "0" && y.s == "0") return false;
    if (x.isneg && !y.isneg) return true;
    if (!x.isneg && y.isneg) return false;
    if (x.isneg) {
        if (x.s.length() < y.s.length()) return false;
        if (x.s.length() > y.s.length()) return true;
        for (ll i = 0; i < x.s.length(); i++) {
            if (x.s[i] < y.s[i]) return false;
            if (x.s[i] > y.s[i]) return true;
        }
        return false;
    }
    else {
        if (x.s.length() < y.s.length()) return true;
        if (x.s.length() > y.s.length()) return false;
        for (ll i = 0; i < x.s.length(); i++) {
            if (x.s[i] < y.s[i]) return true;
            if (x.s[i] > y.s[i]) return false;
        }
        return false;
    }
}
bool operator >= (const bigint& x, const bigint& y) {
    return (x > y || x == y);
}
bool operator <= (const bigint& x, const bigint& y) {
    return (x < y || x == y);
}

bigint abs(bigint x) {
    return { false, x.s };
}
bigint SameSgnNoNegAdd(bigint x, bigint y) {
    ll a[maxlen], b[maxlen], sum[maxlen + 1];
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(sum, 0, sizeof(sum));
    ll tmp = max(x.s.length(), y.s.length());
    for (ll i = x.s.length() - 1; i >= 0; i--) a[x.s.length() - i] = x.s[i] - '0';
    for (ll i = y.s.length() - 1; i >= 0; i--) b[y.s.length() - i] = y.s[i] - '0';
    for (ll i = 1; i <= tmp + 1; i++) {
        sum[i] += a[i] + b[i];
        sum[i + 1] = sum[i] / 10;
        sum[i] %= 10;
    }
    bool flag = false;
    string ans;
    for (ll i = tmp + 1; i >= 1; i--) {
        if (sum[i]) flag = true;
        if (flag || i == 1) ans += sum[i] + '0';
    }
    return { false, ans };
}
bigint SameSgnNoNegSub(bigint x, bigint y) {
    ll a[maxlen], b[maxlen], sum[maxlen + 1];
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(sum, 0, sizeof(sum));
    ll tmp = max(x.s.length(), y.s.length());
    for (ll i = x.s.length() - 1; i >= 0; i--) a[x.s.length() - i] = x.s[i] - '0';
    for (ll i = y.s.length() - 1; i >= 0; i--) b[y.s.length() - i] = y.s[i] - '0';
    for (ll i = 1; i <= tmp; i++) {
        sum[i] += a[i] - b[i];
        sum[i + 1] = (sum[i] < 0 ? -1 : 0);
        sum[i] = (sum[i] + 10) % 10;
    }
    bool flag = false;
    string ans;
    for (ll i = tmp; i >= 1; i--) {
        if (sum[i]) flag = true;
        if (flag || i == 1) ans += sum[i] + '0';
    }
    return { false, ans };
}
bigint operator + (const bigint& x, const bigint& y) {
    if (x.isneg ^ y.isneg) {
        bigint abs1 = abs(x), abs2 = abs(y);
        if (abs1 > abs2) return { x.isneg, SameSgnNoNegSub(abs1, abs2).s };
        else return { y.isneg, SameSgnNoNegSub(abs2, abs1).s };
    }
    return { x.isneg, SameSgnNoNegAdd(abs(x), abs(y)).s };
}
bigint operator - (const bigint& x, const bigint& y) {
    bigint yy;
    yy.isneg = y.isneg ^ true;
    yy.s = y.s;
    return x + yy;
}
bigint SameSgnNoNegMul(bigint x, bigint y) {
    ll a[maxlen], b[maxlen], sum[maxlen + 1];
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(sum, 0, sizeof(sum));
    ll tmp = x.s.length() + y.s.length() + 1;
    for (ll i = x.s.length() - 1; i >= 0; i--) a[x.s.length() - i] = x.s[i] - '0';
    for (ll i = y.s.length() - 1; i >= 0; i--) b[y.s.length() - i] = y.s[i] - '0';
    for (ll i = 1; i <= x.s.length(); i++) 
        for (ll j = 1; j <= y.s.length(); j++) 
            sum[i + j - 1] += a[i] * b[j];
    for (ll i = 1; i <= tmp; i++) {
        if (sum[i] >= 10) sum[i + 1] += sum[i] / 10;
        sum[i] %= 10;
    }
    bool flag = false;
    string ans;
    for (ll i = tmp; i >= 1; i--) {
        if (sum[i]) flag = true;
        if (flag || i == 1) ans += sum[i] + '0';
    }
    return { false, ans };
}
bigint SameSgnNoNegDiv(bigint x, bigint y) {
    if (x.s == "0") return { false, "0" };
    if (x.s.length() < y.s.length()) return { false, "0" };
    if (y.s == "0") return { false, "inf" };
    ll a[maxlen], b[maxlen];
    string ans1;
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    for (ll i = x.s.length() - 1; i >= 0; i--) a[x.s.length() - i] = x.s[i] - '0';
    for (ll i = y.s.length() - 1; i >= 0; i--) b[y.s.length() - i] = y.s[i] - '0';
    ll now = y.s.length();
    bigint rem = { false, x.s.substr(0, y.s.length()) };
    bigint zero = { false, "0"};
    while (now <= x.s.length()) {
        ll tmp = 0;
        while (rem >= zero) {
            rem = rem - y;
            tmp++;
        }
        rem = rem + y;
        if (rem.s == "0") rem.s = x.s[now];
        else rem.s = rem.s + x.s[now];
        now++;
        tmp--;
        ans1 += tmp + '0';
    }
    bigint ans = { false, "" };
    bool flag = false;
    for (ll i = 0; i < ans1.length(); i++) {
        if (ans1[i] > '0') flag = true;
        if (flag || i == ans1.length() - 1) ans.s += ans1[i];
    }
    return ans;
}
bigint operator * (const bigint& x, const bigint& y) {
    bool sgn = x.isneg ^ y.isneg;
    return { sgn, SameSgnNoNegMul(abs(x), abs(y)).s };
}
bigint operator / (const bigint& x, const bigint& y) {
    bool sgn = x.isneg ^ y.isneg;
    return { sgn, SameSgnNoNegDiv(abs(x), abs(y)).s };
}
bigint operator % (const bigint& x, const bigint& y) {
    bigint zero = { false, "0" };
    if (x >= zero) return x - (x / y) * y;
    else return x - (x / y) * y + abs(y);
}

算法基础

1.前缀和

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100001;
int n, m, a[maxn], p[maxn];
int main(){
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    p[1] = a[1];
    for (int i = 2; i <= n; i++) p[i] = p[i - 1] + a[i];
    for (int i = 1; i <= m; i++){
        int l, r;
        cin >> l >> r;
        cout << p[r] - p[l - 1] << endl;
    } 
    return 0;
} 

2.差分

#include <bits/stdc++.h>
using namespace std;
const int maxn= 100001;
int a[maxn], d[maxn];
int n, p;
int main()
{
    int sum = 0;
    cin >> n >> p;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        d[i] = a[i] - a[i - 1];
    }
    for (int i = 0; i < p; i++) {
        int x, y, z;
        cin >> x >> y >> z;
        d[x] += z;
        d[y + 1] -= z;
    }
    int min = 100000000;
    for (int i = 1; i <= n; i++) {
        a[i] = a[i - 1] + d[i];
        if (a[i] < min) min = a[i];
    }
    cout << min;
}

3.逆序对/归并排序

typedef long long ll;
ll ans = 0;
ll aa[500001];
ll tmp[500001];
ll n;
void msort(ll s, ll e, ll ){
    if (s == e) return;
    ll mid = (s + e) / 2, i = s, j = mid + 1, k = s;
    msort(s, mid);
    msort(mid + 1, e);
    while (i <= mid && j <= e){
        if (a[i] <= a[j]) tmp[k++] = a[i++];
        else { 
            tmp[k++] = a[j++];
            ans += mid - i + 1; 
        } 
    } 
    while (i <= mid) tmp[k++] = a[i++];
    while (j <= e) tmp[k++] = a[j++];
    for (int l = s; l <= e; l++) a[l] = tmp[l];
}

数据结构

1.ST表(Sparce Table)

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100001;
const int logN = 16;
int st[MAXN][logN + 1], Logn[MAXN + 1];
int n, m;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
int main() {
    n = read();
    m = read();
    for (int i = 1; i <= n; i++) st[i][0] = read();
    Logn[1] = 0;
    Logn[2] = 1;
    for (int i = 3; i < MAXN; i++) Logn[i] = Logn[i / 2] + 1;
    for (int j = 1; j <= Logn[n]; j++){
        for (int i = 1; i + (1 << j) - 1 <= n; i++){
            st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);    
        }
    }
    for (int i = 1; i <= m; i++) {
        int x, y;
        x = read();
        y = read();
        int s = Logn[y - x + 1];
        cout << max(st[x][s], st[y - (1 << s) + 1][s]) << '\n';
    }
    return 0;
}

2.维护区间和的树状数组

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll aa[500001];
ll lowbit(ll x){
    return x & (-x);
}
struct Binary_Indexed_Tree{
    ll t[500001];
    void init(ll a[], ll n){
        ll s[500001];
        for (ll i = 1; i <= n; i++) {
            s[i] = s[i - 1] + a[i];
            t[i] = s[i] - s[i - lowbit(i)];
        }
    }
    ll getsum(ll x) {
        ll ans = 0;
        while (x) {
            ans += t[x];
            x -= lowbit(x);
        }
        return ans;
    }
    void add(ll n, ll x, ll num) {
        while (x <= n) {
            t[x] = t[x] + num;
            x = x + lowbit(x);
        }
    }
    ll getsum(ll l, ll r) {
        return getsum(r) - getsum(l - 1);
    }
};
Binary_Indexed_Tree bit;
signed main(){
    ll n, q;
    cin >> n >> q;
    for (ll i = 1; i <= n; i++) cin >> aa[i];
    bit.init(aa, n);
    for (ll i = 1; i <= q; i++){
        ll op;
        cin >> op;
        if (op == 1) {
            ll x, k;
            cin >> x >> k;
            bit.add(n, x, k);
        } else {
            ll l, r;
            cin >> l >> r;
            cout << bit.getsum(l, r) << endl;
        }
    }
    return 0;
}

3.支持区间求和和区间加值的线段树

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll; 
ll tree[400001], a[100001], lazy[400001];
void build(ll x, ll l, ll r) {
    if (l == r) {
        tree[x] = a[l];
        return;
    }
    ll mid = (l + r) / 2;
    build(2 * x, l, mid);
    build(2 * x + 1, mid + 1, r);
    tree[x] = max(tree[2 * x], tree[2 * x + 1]);
}
void pushdown(ll x) {
    lazy[2 * x] += lazy[x];
    lazy[2 * x + 1] += lazy[x];
    tree[2 * x] += lazy[x];
    tree[2 * x + 1] += lazy[x];
    lazy[x] = 0;
}
ll getmax(ll x, ll pl, ll pr, ll l, ll r) {
    if (r < pl || l > pr) return -1;
    if (pl == l && pr == r) return tree[x];
    if (lazy[x]) pushdown(x);
    ll mid = (pl + pr) / 2;
    return max(getmax(2 * x, pl, mid, l, min(r, mid)), getmax(2 * x + 1, mid + 1, pr, max(mid + 1, l), r));
}
void add(ll x, ll pl, ll pr, ll l, ll r, ll d) {
    if (r < pl || l > pr) return;
    if (pl == l && pr == r) {
        tree[x] += d, lazy[x] += d;
        return;
    }
    if (lazy[x]) pushdown(x);
    ll mid = (pl + pr) / 2;
    add(2 * x, pl, mid, l, min(r, mid), d);
    add(2 * x + 1, mid + 1, pr, max(l, mid + 1), r, d);
    tree[x] = max(tree[x * 2], tree[x * 2 + 1]);
}
signed main() {
    ll n, q;
    cin >> n >> q;
    for (ll i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    while (q--) {
        ll op;
        cin >> op;
        if (op == 1) {
            ll l, r;
            cin >> l >> r;
            cout << getmax(1, 1, n, l, r) << '\n';
        }
        if (op == 2) {
            ll l, r, x;
            cin >> l >> r >> x;
            add(1, 1, n, l, r, x);
        }
    }
    return 0;
}

4.单调栈

typedef long long ll;
struct MonotonicStack {
    stack <ll> st;
    void push(ll x){
        if (st.empty()) {
            st.push(x);
            return;
        }
        while (!st.empty() && st.top() < x) st.pop();
        st.push(x);
    }
    void pop(){ st.pop(); }
    ll top(){ return st.top(); }
    bool empty(){ return st.empty(); }
};

5.单调队列

typedef long long ll;
struct MonotonicQueue {
    deque <ll> dq;
    void push(ll x){
        if (dq.empty()) {
            dq.push_back(x);
            return;
        }
        while (!dq.empty() && dq.back() >= x) dq.pop_back();
        dq.push_back(x);
    }
    void pop_front(){ dq.pop_front(); }
    void pop_back(){ dq.pop_back(); }
    ll front(){ return dq.front(); }
    ll back(){ return dq.back(); }
    bool empty(){ return dq.empty(); }
};

6.Trie

typedef long long ll;
ll trie[3000001][65];
ll cnt2[3000001];
ll cnt = 0;
void insert(string s) {
    ll p = 0;
    for (ll i = 0; i < s.length(); i++) {
        ll thing = toint(s[i]);
        if (!trie[p][thing]) trie[p][thing] = ++cnt;
        p = trie[p][thing];
        cnt2[p]++;
    }
}
ll findprefix(string s) {
    ll p = 0;
    for (ll i = 0; i < s.length(); i++) {
        ll thing = toint(s[i]);
        if (!trie[p][thing]) return 0;
        p = trie[p][thing];
    }
    return cnt2[p];
}

数学

1.快速幂,龟速乘

typedef long long ll;
ll qpow(ll a, ll b, ll mod) {
    ll ans = 1;
    while (b) {
        if (b & 1) ans = (ans * a) % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
ll qmul(ll a, ll b, ll m) {
    ll ans = 0;
    while (b) {
        if (b & 1) ans = (ans + a) % m;
        a = (a + a) % m;
        b >>= 1;
    }
    return ans;
}

2.矩阵快速幂

#include <bits/stdc++.h>
using namespace std;
const long long maxn = 101;
const long long mod = 1000000007;
long long n, k;
struct ary{
    long long a[maxn][maxn];
    ary(){
        memset(a, 0, sizeof(a));
    }
    inline void build(){
        for (int i = 1; i <= n; i++) {
            a[i][i] = 1;
        } 
    }
};
ary operator *(const ary &a, const ary &b){
    ary ans;
    for (int k = 1; k <= n; k++){
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= n; j++){
                ans.a[i][j] = (ans.a[i][j] + a.a[i][k] * b.a[k][j] % mod) % mod;
            }
        }
    }
    return ans;
}
int main() {
    ary a;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a.a[i][j];
    ary ans;
    ans.build();
    while (k){
        if (k & 1) ans = ans * a;
        a = a * a;
        k >>= 1;
    }
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
            cout << ans.a[i][j] << " ";
        }
        cout << endl;
    } 
    return 0;
}

3.GCD (最大公因数)

#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y){
    if (y == 0) return x;
    return gcd(y, x % y);
}
int main(){
  int a, b;
  cin >> a >> b;
    cout << gcd(a, b);
}

4.判断质数

#include <bits/stdc++.h>
using namespace std;
bool prime(int n) {
    if (n <= 1) return false;
    if (n == 2) return true;
    for (int i = 2; i * i <= n; i++) if (!(n % i)) return false;
    return true;
}

5.EXGCD (扩展欧几里得算法)

typedef long long ll;
ll exgcd(ll a, ll b, ll &x, ll &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll ans = exgcd(b, a % b, y, x);
    y = ans - (a / b) * x;
    return ans;
}

6.线性筛质数(欧拉筛)

typedef long long ll;
vector<ll> prime;
bool not_prime[10000001];
ll phi[10000001];
void Euler(ll n) {
    phi[1] = 1;
    for (ll i = 2; i <= n; i++) {
        if (!not_prime[i]) {
            prime.push_back(i);
            phi[i] = i - 1;
        }
        for (auto j : prime) {
            if (i * j > n) break;
            not_prime[i * j] = true;
            if (i % j == 0) {
                phi[i * j] = phi[i] * j;
                break;
            }
            phi[i * j] = phi[i] * phi[j];
        }
    }
}

7.单个数求\varphi(x)

typedef long long ll;
ll phi(ll n) {
  ll ans = n;
  for (ll i = 2; i * i <= n; i++)
    if (!(n % i)) {
      ans = ans / i * (i - 1);
      while (n % i == 0) n /= i;
    }
  if (n > 1) ans = ans / n * (n - 1);
  return ans;
}

8.EXGCD求逆元

typedef long long ll;
ll exgcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll ans = exgcd(b, a % b, x, y);
    ll temp = x;
    x = y;
    y = temp - (a / b) * y;
    return ans;
}
ll mod_inverse(ll a, ll m) {
    ll x, y;
    ll ans = exgcd(a, m, x, y);
    if (ans != 1) return 9223372036854775807;
    else return x % m;
}

9.复数

typedef long long ll;
typedef long double ld;
const ld pi = 3.14159265358979323846;
struct Complex {
    ld re, im;
};
ld modulus(Complex z) {
    return sqrt(z.re * z.re + z.im * z.im);
}
ld arg(Complex z) {
    if (z.im == 0.0 && z.re == 0.0) return 0.0;
    return atan2(z.im, z.re);
}
ld Re(Complex z) {
    return z.re;
}
ld Im(Complex z) {
    return z.im;
}
Complex conj(Complex z) {
    return { z.re, -z.im };
}
Complex operator + (const Complex& z1, const Complex& z2) {
    return { z1.re + z2.re, z1.im + z2.im };
}
Complex operator - (const Complex& z1, const Complex& z2) {
    return { z1.re - z2.re, z1.im - z2.im };
}
Complex operator * (const Complex& z1, const Complex& z2) {
    return { z1.re * z2.re - z1.im * z2.im, z1.re * z2.im + z1.im * z2.re };
}
Complex operator / (const Complex& z1, const Complex& z2) {
    Complex ans;
    ans.re = (z1.re * z2.re + z1.im * z2.im) / (z2.re * z2.re + z2.im * z2.im);
    ans.im = (z1.im * z2.re - z1.re * z2.im) / (z2.re * z2.re + z2.im * z2.im);
    return ans;
}
Complex exp(Complex z, ll precision) {
    ll fact = 1;
    Complex anss = { 0, 0 };
    Complex add = { 1, 0 };
    Complex z3 = { 1, 0 };
    for (ll i = 1; i <= precision; i++) {
        anss = anss + add;
        z3.re = (ld)i;
        add = add * z / z3;
    }
    return anss;
}
void print(Complex z, ll precision) {
    cout << fixed << setprecision(precision);
    if (z.re == 0 && z.im == 0) {
        cout << 0;
        return;
    }
    if (z.re != 0) cout << z.re;
    if (z.im != 0) {
        if (z.re == 0) cout << z.im << "i";
        else {
            if (z.im > 0) cout << "+";
            cout << z.im << "i";
        }
    }
}

10.EXGCD

#include <bits/stdc++.h>
using namespace std;
typedef __int128 ll;
inline ll read() {
    ll x = 0, f = 1; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
    while (c >= '0' && c <= '9') { x = x * 10 + c - 48; c = getchar(); }
    return x * f;
}
inline void write(ll x) {
    ll x2 = x;
    if (x2 < 0) { putchar('-'); x2 = -x; }
    if (x2 > 9) write(x2 / 10);
    putchar(x % 10 + '0');
}
ll exgcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll ans = exgcd(b, a % b, x, y);
    ll temp = x;
    x = y;
    y = temp - (a / b) * y;
    return ans;
}
ll gcd(ll x, ll y) {
    if (y == 0) return x;
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}
ll a[100001], m[100001];
int main() {
    ll k = read();
    for (ll i = 1; i <= k; i++) {
        m[i] = read();
        a[i] = read();
    }
    ll m1 = m[1], a1 = a[1];
    for (ll i = 2; i <= k; i++) {
        ll a2 = a[i], m2 = m[i];
        ll p, q;
        ll e = exgcd(m1, m2, p, q);
        if ((a2 - a1) % e != 0) {
            cout << "-1\n";
            return 0;
        }
        p = p * (a2 - a1) / e;
        q = m2 / e;
        p = (p % q + q) % q;
        a1 = m1 * p + a1;
        m1 = m1 * q;
    }
    write(a1);
    return 0;
}

11.高斯消元

const double esp = 0.000001;
typedef long long ll;
double a[101][101], b[101];
ll maxrow[101];
bool noans = false;
ll n;
void Gauss_Jordan() {
    for (ll i = 1; i < n; i++) {
        for (ll j = i + 1; j <= n; j++) {
            double k2 = a[j][1] / a[i][1];
            bool same = true;
            for (ll k = 2; k <= n; k++) {
                if (abs((a[j][k] / a[i][k] - k2)) > esp) {
                    same = false;
                    break;
                }
            }
            if (same) {
                noans = true;
                return;
            }
        }
    }
    for (ll i = 1; i < n; i++) {
        double mx = 0;
        for (ll j = i + 1; j <= n; j++) {
            if (abs(a[j][i]) >= mx) {
                mx = abs(a[j][i]);
                maxrow[i] = j;
            }
        }
        for (ll j = 1; j <= n; j++) swap(a[i][j], a[maxrow[i]][j]);
        swap(b[i], b[maxrow[i]]);
    }
    for (ll i = 1; i < n; i++) {
        for (ll j = i + 1; j <= n; j++) {
            double factor = a[j][i] / a[i][i];
            for (ll k = 1; k <= n; k++) a[j][k] -= a[i][k] * factor;
            b[j] -= factor * b[i];
        }
    }
    for (ll i = n - 1; i >= 1; i--) {
        b[i + 1] /= a[i + 1][i + 1];
        a[i + 1][i + 1] = 1;
        for (ll j = i + 1; j <= n; j++) {
            b[i] -= a[i][j] * b[j];
            a[i][j] = 0;
        }
    }
    b[1] /= a[1][1];
    a[1][1] = 1;
}

图论

1.Floyd(最短路)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dis[101][101];
void Floyd(ll n){
    for (int k = 1; k <= n; k++){
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= n; j++){
                dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
            }
        }
    }
}
signed main(){
    ll n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
            dis[i][j] = 1e15;
        }
    }
    for (int i = 1; i <= m; i++){
        ll u, v, w;
        cin >> u >> v >> w;
        dis[u][v] = min(dis[u][v], w);
        dis[v][u] = min(dis[v][u], w);
    }
    Floyd(n);
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
            if (i == j) cout << 0 << " ";
            else cout << dis[i][j] << " ";
        }
        cout << endl;
    }
} 

2.Dijkstra(最短路)

#include <iostream>
#include <queue>
#include <vector>
#include <climits>
#include <algorithm>
using namespace std;
typedef long long ll;
struct edge {
    ll t, w;
};
vector<edge> h[200001];
ll dis[200001];
bool vis[200001];
struct node {
    ll v, w;
    bool operator <(const node& other) const {
        return w > other.w;
    }
};
void Dijkstra(ll s, ll n) {
    for (ll i = 1; i <= n; i++) {
        dis[i] = LLONG_MAX;
        vis[i] = false;
    }
    dis[s] = 0;
    priority_queue<node> pq;
    pq.push({ s, 0 });
    while (!pq.empty()) {
        ll u = pq.top().v;
        pq.pop();
        if (vis[u]) continue;
        vis[u] = true;
        for (const auto& edge : h[u]) {
            ll v = edge.t;
            ll w = edge.w;
            if (dis[u] + w < dis[v]) {
                dis[v] = dis[u] + w;
                pq.push({ v, dis[v] });
            }
        }
    }
}
signed main() {
    ll n, m, s;
    cin >> n >> m >> s;
    for (ll i = 1; i <= m; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        h[a].push_back({ b, c });
    }
    Dijkstra(s, n);
    for (int i = 1; i <= n; i++) cout << dis[i] << " ";
    return 0;
}

3.SPFA(Shortest Path Faster Algorithm)(最短路)

#include <iostream>
#include <vector>
#include <string.h>
#include <queue>
using namespace std;
typedef long long ll;
struct edge {
    ll v, w;
};
vector <edge> e[200001];
queue <ll> q;
ll dis[200001], cnt[200001], vis[200001];
bool SPFA(ll s, ll n) {
    memset(dis, 0x3f, sizeof(dis));
    dis[s] = 0, vis[s] = 1;
    q.push(s);
    while (!q.empty()) {
        ll u = q.front();
        q.pop(), vis[u] = 0;
        for (auto ed : e[u]) {
            ll v = ed.v, w = ed.w;
            if (dis[v] > dis[u] + w) {
                dis[v] = dis[u] + w;
                cnt[v] = cnt[u] + 1;
                if (cnt[v] >= n) return false;
                if (!vis[v]) q.push(v), vis[v] = 1;
            }
        }
    }
    return true;
}
signed main() {
    ll n, m, s;
    cin >> n >> m >> s;
    for (ll i = 1; i <= m; i++) {
        ll u, v, w;
        cin >> u >> v >> w;
        e[u].push_back({ v, w });
    }
    if (SPFA(s, n)) for (ll i = 1; i <= n; i++) cout << dis[i] << " ";
}

4.Kruskal(最小生成树)

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll fa[5001];
ll n, m, k;
struct edge {
    ll u, v, w;
};
ll l;
edge g[300001];
void add(ll u, ll v, ll w) {
    l++;
    g[l].u = u;
    g[l].v = v;
    g[l].w = w;
}
ll findroot(ll x) { 
    return fa[x] == x ? x : fa[x] = findroot(fa[x]); 
}
void merge(ll x, ll y) {
    x = findroot(x);
    y = findroot(y);
    fa[x] = y;
}
bool cmp(edge a, edge b) { 
    return a.w < b.w; 
}
void Kruskal() {
    ll tot = 0;
    ll ans = 0;
    for (ll i = 1; i <= m; i++) {
        ll xrt = findroot(g[i].u), yrt = findroot(g[i].v);
        if (xrt != yrt) {
            merge(xrt, yrt);
            tot++;
            ans += g[i].w;
        }
        if (tot >= (n - k)) {
            cout << ans << '\n';
            return;
        }
    }
    cout << "orz\n";
}
signed main() {
    cin >> n >> m;
    k = 1;
    for (ll i = 1; i <= n; i++) fa[i] = i;
    for (ll i = 1; i <= m; i++) {
        ll u, v, w;
        cin >> u >> v >> w;
        add(u, v, w);
    }
    sort(g + 1, g + m + 1, cmp);
    Kruskal();
    return 0;
}

5.Targan(强连通分量)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector <ll> vec[100001];
ll dfn[100001], low[100001], dfncnt, s[100001], in_stack[100001], tp;
ll scc[100001], sc;
ll sz[100001];
void Tarjan(ll u) {
  low[u] = dfn[u] = ++dfncnt, s[++tp] = u, in_stack[u] = 1;
  for (int i = 0; i < vec[u].size(); i++) {
    const ll &v = vec[u][i];
    if (!dfn[v]) {
      Tarjan(v);
      low[u] = min(low[u], low[v]);
    } else if (in_stack[v]) {
      low[u] = min(low[u], dfn[v]);
    }
  }
  if (dfn[u] == low[u]) {
    ++sc;
    do {
      scc[s[tp]] = sc;
      sz[sc]++;
      in_stack[s[tp]] = 0;
    } while (s[tp--] != u);
  }
}
signed main(){
    ll n, m;
    while (true){
        cin >> n >> m;
        memset(dfn, 0, sizeof(dfn));
        memset(low, 0, sizeof(low));
        memset(s, 0, sizeof(s));
        memset(in_stack, 0, sizeof(in_stack));
        tp = 0;
        sc = 0;
        memset(scc, 0, sizeof(scc));
        memset(sz, 0, sizeof(sz));
        for (ll i = 1; i <= 100001; i++) vec[i].clear();
        if (n == 0 && m == 0) return 0;
        for (ll i = 1; i <= m; i++){
            ll u, v;
            cin >> u >> v;
            vec[u].push_back(v); 
        }
        for (ll i = 1; i <= n; i++) if (!dfn[i]) Tarjan(i);
        cout << sc << endl;
    } 
    return 0;
}

DP

树形DP

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool f[6001];
typedef long long ll;
vector <ll> son[6001];
ll w[6001];
ll dp[6001][2];
void work(ll root) {
    dp[root][0] = 0;
    dp[root][1] = w[root];
    for (ll i = 0; i < son[root].size(); i++) {
        work(son[root][i]);
        dp[root][0] += max(dp[son[root][i]][0], dp[son[root][i]][1]);
        dp[root][1] += dp[son[root][i]][0];
    }
}
signed main()
{
    ll n;
    cin >> n;
    for (ll i = 1; i <= n; i++) cin >> w[i];
    for (ll i = 1; i < n; i++) {
        ll father, son2;
        cin >> son2 >> father;
        son[father].push_back(son2);
        f[son2] = true;
    }
    ll rt = 0;
    for (ll i = 1; i <= n; i++) {
        if (f[i] == 0) {
            rt = i;
            break;
        }
    }
    work(rt);
    cout << max(dp[rt][0], dp[rt][1]);
}

字符串

1.多项式字符串哈希(Hash)

typedef unsigned long long ull;
ull Hash(string s, ull b, ull mod){
    ull ans = 0;
    for (int i = s.length() - 1; i >= 0; i--) ans = (ans * b + s[i]) % mod;
    return ans;
}

2.KMP(字符串匹配)

typedef long long ll;
vector <ll> prefix(string s) {
    vector <ll> pi(s.length());
    for (ll i = 1; i < s.length(); i++) {
        ll j = pi[i - 1];
        while (j > 0 && s[i] != s[j]) j = pi[j - 1];
        if (s[i] == s[j]) j++;
        pi[i] = j;
    }
    return pi;
}
vector <ll> KMP(string s1, string s2) {
    string s = s2 + '#' + s1;
    ll l1 = s1.size(), l2 = s2.size();
    vector <ll> ans;
    vector <ll> pi = prefix(s);
    for (ll i = l2 + 1; i <= l1 + l2; i++) if (pi[i] == l2) ans.push_back(i - 2 * l2);
    return ans;
}

3.Manacher(马拉车,最长回文子串)

typedef long long ll;
ll Manacher(string s) {
    string t = "#";
    for (char c : s) {
        t += c;
        t += "#";
    }
    ll l = t.length();
    vector <ll> p(l, 0);
    ll center = 0, right = 0;
    for (ll i = 1; i < l; ++i) {
        if (i < right) p[i] = min(right - i, p[2 * center - i]);
        while (i + p[i] < l && i - p[i] >= 0 && t[i + p[i]] == t[i - p[i]]) {
            p[i]++;
        }
        if (i + p[i] > right) {
            center = i;
            right = i + p[i];
        }
    }
    ll ans = 0;
    for (ll i = 1; i < l; ++i) ans = max(ans, p[i]);
    return ans - 1;
}

计算几何

3维向量

typedef long double ld;
struct Vector{
    ld x, y, z;
    ld modulus(){
        return sqrt(x * x + y * y + z * z);
    }
};
Vector operator + (const Vector &v1, const Vector &v2){
    return {v1.x + v2.x, v1.y + v2.y, v1.z + v2.z};
} 
Vector operator - (const Vector &v1, const Vector &v2){
    return {v1.x - v2.x, v1.y - v2.y, v1.z - v2.z};
} 
Vector operator * (const ld &lambda, const Vector &v){
    return {lambda * v.x, lambda * v.y, lambda * v.z};
} 
Vector cross(Vector v1, Vector v2){
    return {v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x};
}
ld dot(Vector v1, Vector v2){
    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}

附1:常用头文件

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <string.h>
#include <vector>
#include <Windows.h>

附2:加强主函数

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <string.h>
#include <vector>
#include <Windows.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read() {
    ll x = 0, f = 1; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
    while (c >= '0' && c <= '9') { x = x * 10 + c - 48; c = getchar(); }
    return x * f;
}
inline void write(ll x) {
    ll x2 = x;
    if (x2 < 0) { putchar('-'); x2 = -x; }
    if (x2 > 9) write(x2 / 10);
    putchar(x % 10 + '0');
}
inline string sread() {
    string s = "";
    char c = getchar();
    while (c == ' ' || c == '\n' || c == '\r') c = getchar();
    while (c != ' ' && c != '\n' && c != '\r') {
        s += c;
        c = getchar();
    }
    return s;
}
inline void swrite(string s) {
    for (int i = 0; s[i] != '\0'; i++) putchar(s[i]);
}
vector <ll> prefix(string s) {
    vector <ll> pi(s.length());
    for (ll i = 1; i < s.length(); i++) {
        ll j = pi[i - 1];
        while (j > 0 && s[i] != s[j]) j = pi[j - 1];
        if (s[i] == s[j]) j++;
        pi[i] = j;
    }
    return pi;
}
vector <ll> KMP(string s1, string s2) {
    string s = s2 + '#' + s1;
    ll l1 = s1.size(), l2 = s2.size();
    vector <ll> ans;
    vector <ll> pi = prefix(s);
    for (ll i = l2 + 1; i <= l1 + l2; i++) if (pi[i] == l2) ans.push_back(i - 2 * l2);
    return ans;
}
ull Hash(string s, ull b, ull mod) {
    ull ans = 0;
    for (ll i = s.length() - 1; i >= 0; i--) ans = (ans * b + s[i]) % mod;
    return ans;
}
struct edge {
    ll t, w;
};
vector<edge> h[200001];
ll dis[200001];
bool vis[200001];
struct node {
    ll v, w;
    bool operator <(const node& other) const {
        return w > other.w;
    }
};
void Dijkstra(ll s, ll n) {
    for (ll i = 1; i <= n; i++) {
        dis[i] = LLONG_MAX;
        vis[i] = false;
    }
    dis[s] = 0;
    priority_queue<node> pq;
    pq.push({ s, 0 });
    while (!pq.empty()) {
        ll u = pq.top().v;
        pq.pop();
        if (vis[u]) continue;
        vis[u] = true;
        for (const auto& edge : h[u]) {
            ll v = edge.t;
            ll w = edge.w;
            if (dis[u] + w < dis[v]) {
                dis[v] = dis[u] + w;
                pq.push({ v, dis[v] });
            }
        }
    }
}
vector<ll> prime;
bool not_prime[100001];
ll phi[100001];
void Euler(ll n) {
    phi[1] = 1;
    for (ll i = 2; i <= n; i++) {
        if (!not_prime[i]) {
            prime.push_back(i);
            phi[i] = i - 1;
        }
        for (ll j = 0; j < prime.size(); j++) {
            ll prime_j = prime[j];
            if (i * prime_j > n) break;
            not_prime[i * prime_j] = true;
            if (i % prime_j == 0) {
                phi[i * prime_j] = phi[i] * prime_j;
                break;
            }
            phi[i * prime_j] = phi[i] * phi[prime_j];
        }
    }
}
ll qpow(ll a, ll b, ll mod) {
    ll ans = 1;
    while (b) {
        if (b & 1) ans = (ans * a) % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
ll exgcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll ans = exgcd(b, a % b, x, y);
    ll temp = x;
    x = y;
    y = temp - (a / b) * y;
    return ans;
}
#define Yes cout << "Yes"
#define No cout << "No"
#define gcd(a, b) __gcd(a, b)
signed main() {
    return 0;
}