Templates

· · 算法·理论

FastIO

#define gc() (iS == iT && (iT = (iS = ibuf) + fread(ibuf, 1, 1 << 20, stdin), iS == iT) ? EOF : *iS++)
#define pc(x) ((oT - obuf == (1 << 20)) && (fwrite(obuf, 1, 1 << 20, stdout), oT = obuf), *oT++ = x)
namespace FastIO {
    char ibuf[1 << 20], obuf[1 << 20], *iS = ibuf, *iT = ibuf, *oT = obuf;
    template <typename _Tp> inline void read(_Tp& x) {
        char c = gc(); x = 0; bool f = 0;
        for(; !std::isdigit(c); c = gc()) f |= c == '-';
        for(; std::isdigit(c); c = gc()) x = (x << 1) + (x << 3) + (c ^ 48);
        f ? x = ~x + 1 : 1;
    }
    template <typename _Tp> inline void write(_Tp x) {
        static int stk[40]; int tp = 0;
        if(x < 0) pc('-'), x = ~x + 1;
        do stk[++tp] = x % 10, x /= 10; while(x);
        while(tp) pc(stk[tp--] + '0');
    }
    inline void read(char& c) { for(c = gc(); !std::isgraph(c); c = gc()); }
    inline void read(std::string& x) { x.clear(); char c = gc(); while(!std::isgraph(c)) c = gc(); while(isgraph(c)) x.push_back(c), c = gc(); }
    inline void write(const char c) { pc(c); }
    inline void write(const std::string x) { for(const char& c : x) pc(c); }
    inline void writesp() { pc(' '); }
    inline void writeln() { pc('\n'); }
    template <typename _Tp, typename ...Args> inline void read(_Tp& x, Args& ...args) { read(x), read(args...); }
    template <typename _Tp, typename ...Args> inline void write(_Tp x, Args ...args) { write(x), pc(' '), write(args...); }
    template <typename _Tp> inline void writesp(_Tp x) { write(x), pc(' '); }
    template <typename _Tp> inline void writeln(_Tp x) { write(x), pc('\n'); }
    inline void flush() { fwrite(obuf, oT - obuf, 1, stdout); } 
}
using namespace FastIO;

指令集

#pragma GCC target("abm,avx,mmx,popcnt,sse,sse2,sse3,sse4,ssse3,tune=native")

缺省源

#include <bits/stdc++.h>
#define gc() (iS == iT && (iT = (iS = buf) + fread(buf, 1, 1 << 20, stdin), iS == iT) ? EOF : *iS++)
char buf[1 << 20], *iS = buf, *iT = buf;
template <typename _Tp> inline void read(_Tp& x) {
    x = 0; char c = gc(); bool f = 0;
    for(; !std::isdigit(c); c = gc()) f |= c == '-';
    for(; std::isdigit(c); c = gc()) x = x * 10 + (c - '0');
}
template <> inline void read(std::string& x) {
    x.clear(); char c = gc();
    for(; !std::isalpha(c); c = gc());
    for(; std::isalpha(c); c = gc()) x += c;
}
template <typename _Tp, typename ...Args>
inline void read(_Tp& x, Args& ...args) {
    read(x), read(args...);
}
typedef long long i64;

signed main() {

    return 0;
}

线筛 \boldsymbol\varphi, \mu

inline void sieve(int n) {
    phi[1] = 1, mu[1] = 1;
    rep(i, 2, n) {
        if(!vis[i]) pr[++tot] = i, phi[i] = i - 1, mu[i] = -1;
        for(int j = 1; j <= tot && 1ll * i * pr[j] <= n; j++) {
            if(i % pr[j]) {
                phi[i * pr[j]] = phi[i] * (pr[j] - 1);
                mu[i] = 0;
            } else {
                phi[i * pr[j]] = phi[i] * pr[j];
                mu[i * pr[j]] = -mu[i];
                break;
            }
        }
    }
}