【2 级置顶】代码模板

OI

快读快写

来自 lnw143

#include <cstdio>
#include <string>
#include <cctype>
namespace IO {
	static const int BUF = 1<<20;
	static char pbuf[BUF],*p=pbuf;
	inline char gc() {
		static char buf[BUF],*p1=buf,*p2=buf;
		return p1==p2&&(p2=(p1=buf)+fread(buf,1,BUF,stdin),p1==p2)?-1:*p1++;
	}
	inline void pc(char c) { *p++=c; if(p-pbuf==BUF) fwrite(pbuf,1,BUF,stdout),p=pbuf; }
	inline void flush() { fwrite(pbuf,1,p-pbuf,stdout); p=pbuf; }
	template<typename T> inline void read(T &x) {
		x=0;
		char c=gc();
		T f=1;
		for(; !isdigit(c); c=gc()) if(c=='-') f=-1;
		for(; isdigit(c); c=gc()) x=(x<<3)+(x<<1)+(c^48);
		x*=f;
	}
	inline void read(char &c) { c=gc(); }
	inline void read(char *p) {
		char c=gc();
		for(; !isgraph(c); c=gc());
		for(; isgraph(c); c=gc()) *p++=c;
		*p='\0';
	}
	inline void read(std::string& s) {
		s.clear();
		char c=gc();
		for(; !isgraph(c); c=gc());
		for(; isgraph(c); c=gc()) s+=c;
	}
	template<typename T,typename ...Args> void read(T &x,Args &...args) { read(x); read(args...); }
	template<typename T> inline void write(const T &y) {
		T x=y;
		if(x<0) {
			pc('-');
			x=-x;
		}
		static char stk[1<<8],*tp;
		tp=stk;
		do *tp++=(x%10)^48; while(x/=10);
		while(tp!=stk) pc(*--tp);
	}
	inline void write(const char &c) { pc(c); }
	inline void write(const std::string &s) { for(auto i : s) pc(i); }
	inline void write(const char *p) { for(; *p; ++p) pc(*p); }
	inline void write(char *p) { write(static_cast<const char*>(p)); }
	template<typename T,typename ...Args> void write(const T &x,const Args &...args) { write(x); write(args...); }
	struct __Flusher { ~__Flusher() { flush(); } } __flusher;
};
using IO::read;
using IO::write;
using IO::flush;

int main() {
	int a,b;
	read(a,b);
	write(a+b,'\n');
	// std::string s,t;
	char s[100],t[100];
	read(s,t);
	write(s,' ',t,'\n');
	return 0;
}

压行后:

namespace IO{static const int BUF=1<<20;static char pbuf[BUF],*p=pbuf;inline char gc(){static char buf[BUF],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,BUF,stdin),p1==p2)?-1:*p1++;}inline void pc(char c){*p++=c;if(p-pbuf==BUF)fwrite(pbuf,1,BUF,stdout),p=pbuf;}inline void flush(){fwrite(pbuf,1,p-pbuf,stdout);p=pbuf;}template<typename T>inline void read(T&x){x=0;char c=gc();T f=1;for(;!isdigit(c);c=gc())if(c=='-')f=-1;for(;isdigit(c);c=gc())x=(x<<3)+(x<<1)+(c^48);x*=f;}inline void read(char&c){c=gc();}inline void read(char*p){char c=gc();for(;!isgraph(c);c=gc());for(;isgraph(c);c=gc())*p++=c;*p='\0';}inline void read(std::string&s){s.clear();char c=gc();for(;!isgraph(c);c=gc());for(;isgraph(c);c=gc())s+=c;}template<typename T,typename...Args>void read(T&x,Args&...args){read(x);read(args...);}template<typename T>inline void write(const T&y){T x=y;if(x<0){pc('-');x=-x;}static char stk[1<<8],*tp;tp=stk;do*tp++=(x%10)^48;while(x/=10);while(tp!=stk)pc(*--tp);}inline void write(const char&c){pc(c);}inline void write(const std::string&s){for(auto i:s)pc(i);}inline void write(const char*p){for(;*p;++p)pc(*p);}inline void write(char*p){write(static_cast<const char*>(p));}template<typename T,typename...Args>void write(const T&x,const Args&...args){write(x);write(args...);}struct __Flusher{~__Flusher(){flush();}}__flusher;};using IO::read;using IO::write;using IO::flush;

多项式

#include <bits/stdc++.h>
using namespace std;
using poly = vector<int>;
const int N = 4e6 + 10, P = 998244353, G = 3;
inline int add(int x, int y) { if (x + y >= P) return x + y - P; return x + y; }
inline int sub(int x, int y) { if (x - y < 0) return x - y + P; return x - y; }
inline int mul(int x, int y) { return 1LL * x * y % P; }
inline void Add(int& x, int y) { x = add(x, y); }
inline void Sub(int& x, int y) { x = sub(x, y); }
inline void Mul(int& x, int y) { x = mul(x, y); }
inline int qpow(int x, int y = P - 2) {
    int z = 1;
    while (y) {
        if (y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}
poly operator+(const poly& a, const poly& b);
poly operator-(const poly& a, const poly& b);
poly operator*(const poly& a, const poly& b);
poly operator/(const poly& a, const poly& b);
poly operator%(const poly& a, const poly& b);
poly operator~(const poly& a);
const int Gi = qpow(G, P - 2);
namespace Poly {
    int f[N], g[N], h[N], lim, l, r[N];
    void ntt(int a[], int type) {
        for (int i = 0; i < lim; i++) if (i < r[i]) swap(a[i], a[r[i]]);
        for (int x = 2; x <= lim; x <<= 1) {
            int wn = qpow(type == 1 ? G : Gi, (P - 1) / x);
            for (int i = 0; i < lim; i += x) {
                int w = 1;
                for (int j = 0; j < x / 2; j++, w = mul(w, wn)) {
                    int u = a[i + j], v = mul(a[i + j + x / 2], w);
                    a[i + j] = add(u, v);
                    a[i + j + x / 2] = sub(u, v);
                }
            }
        }
    }
    void inv(int a[], int b[], int n) {
        if (n == 1) {
            b[0] = qpow(a[0], P - 2);
            return;
        }
        int m = (n + 1) >> 1;
        inv(a, b, m);
        lim = 1, l = 0;
        while (lim < 2 * n) lim <<= 1, l++;
        for (int i = 0; i < lim; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1)), f[i] = 0;
        for (int i = 0; i < n; i++) f[i] = a[i];
        for (int i = m; i < lim; i++) b[i] = 0;
        ntt(f, 1);
        ntt(b, 1);
        for (int i = 0; i < lim; i++) f[i] = sub(mul(b[i], 2), mul(f[i], mul(b[i], b[i])));
        ntt(f, -1);
        int t = qpow(lim, P - 2);
        for (int i = 0; i < n; i++) b[i] = mul(f[i], t);
        for (int i = n; i < lim; i++) b[i] = 0;
    }
    pair<poly, poly> div(const poly &a, const poly &b) {
        int n = a.size() - 1, m = b.size() - 1;
        static poly q, r, f, g;
        f = a, g = b;
        reverse(f.begin(), f.end());
        reverse(g.begin(), g.end());
        f.resize(n - m + 1);
        g.resize(n - m + 1);
        q = f * ~g;
        q.resize(n - m + 1);
        reverse(q.begin(), q.end());
        r = a - q * b;
        r.resize(m);
        return make_pair(q, r);
    }
}
poly operator+(const poly& a, const poly& b) {
    static poly c;
    int sz = max(a.size(), b.size());
    c.resize(sz);
    for (int i = 0; i < sz; i++) c[i] = 0;
    for (int i = 0; i < a.size(); i++) c[i] = add(c[i], a[i]);
    for (int i = 0; i < b.size(); i++) c[i] = add(c[i], b[i]);
    return c;
}
poly operator-(const poly& a, const poly& b) {
    static poly c;
    int sz = max(a.size(), b.size());
    c.resize(sz);
    for (int i = 0; i < sz; i++) c[i] = 0;
    for (int i = 0; i < a.size(); i++) c[i] = add(c[i], a[i]);
    for (int i = 0; i < b.size(); i++) c[i] = sub(c[i], b[i]);
    return c;
}
poly operator*(const poly& a, const poly& b) {
    static poly c;
    int sz = a.size() + b.size() - 1;
    c.resize(sz);
    for (int i = 0; i < sz; i++) c[i] = 0;
    if (a.size() * b.size() <= 500) {
        for (int i = 0; i < a.size(); i++) {
            for (int j = 0; j < b.size(); j++) {
                c[i + j] = add(c[i + j], mul(a[i], b[j]));
            }
        }
        return c;
    }
    using namespace Poly;
    lim = 1, l = 0;
    while (lim <= sz) lim <<= 1, l++;
    for (int i = 0; i < lim; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1)), f[i] = g[i] = 0;
    for (int i = 0; i < a.size(); i++) f[i] = a[i];
    for (int i = 0; i < b.size(); i++) g[i] = b[i];
    ntt(f, 1);
    ntt(g, 1);
    for (int i = 0; i < lim; i++) f[i] = mul(f[i], g[i]);
    ntt(f, -1);
    int t = qpow(lim, P - 2);
    for (int i = 0; i < sz; i++) c[i] = mul(f[i], t);
    return c;
}
poly operator~(const poly &a) {
    static poly c;
    int sz = a.size();
    c.resize(sz);
    using namespace Poly;
    for (int i = 0; i < sz; i++) h[i] = a[i];
    inv(h, g, sz);
    for (int i = 0; i < sz; i++) c[i] = g[i];
    return c;
}
poly operator/(const poly& a, const poly& b) {
    return Poly::div(a, b).first;
}
poly operator%(const poly& a, const poly& b) {
    return Poly::div(a, b).second;
}

计数

inline int add(int x, int y) { if (x + y >= P) return x + y - P; return x + y; }
inline int sub(int x, int y) { if (x - y < 0) return x - y + P; return x - y; }
inline int mul(int x, int y) { return 1LL * x * y % P; }
inline void Add(int& x, int y) { x = add(x, y); }
inline void Sub(int& x, int y) { x = sub(x, y); }
inline void Mul(int& x, int y) { x = mul(x, y); }
inline int qpow(int x, int y = P - 2) {
    int z = 1;
    while (y) {
        if (y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}

本文作者:ZnPdCo

本文链接: https://znpdco.github.io/blog/2024/08/14/template/

本页面的全部内容在 CC BY-SA 4.0SATA 协议之条款下提供,附加条款亦可能应用