[工具] 常用代碼模板


Quick Start

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cassert>
using namespace std;

#define LOG(f...) fprintf(stderr, f)
#define DBG(f...) printf("%3d: ", __LINE__), printf(f)
// #define DBG(f...) void()
#define all(cont) begin(cont), end(cont)
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

using ll = long long;

template <class T> void read(T &x) {
  char ch; x = 0;
  int f = 1;
  while (isspace(ch = getchar()));
  if (ch == '-') ch = getchar(), f = -1;
  do x = x * 10 + (ch - '0'); while(isdigit(ch = getchar()));
  x *= f;
}
template <class T, class ...A> void read(T &x, A&... args) { read(x); read(args...); }

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  return 0;
}

快速 IO (手寫 buffer)

UPD: 修復了讀入接近上界的整數時可能導致的溢出問題。

需要 <cstdio>,<cctype> 頭文件。

namespace io {
  const int SIZE = (1 << 21) + 1;
  char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1;
  char getc () { return (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++); }
  void flush () { fwrite (obuf, 1, oS - obuf, stdout); oS = obuf; }
  void putc (char x) { *oS ++ = x; if (oS == oT) flush (); }
  template <class T> void read(T &x) {
    char ch; int f = 1;
    x = 0;
    while (isspace(ch = getc()));
    if (ch == '-') ch = getc(), f = -1;
    do x = x * 10 + (ch - '0'); while(isdigit(ch = getc()));
    x *= f;
  }
  template <class T, class ...A> void read(T &x, A&... args) { read(x); read(args...); }
  template <class T> void write(T x) {
    static char stk[128];
    int top = 0;
    if (x == 0) { putc('0'); return; }
    if (x < 0) putc('-'), x = -x;
    while (x) stk[top++] = x % 10, x /= 10;
    while (top) putc(stk[--top] + '0');
  }
  template <class T, class ...A> void write(T x, A... args) { write(x); putc(' '); write(args...) ; }
  void space() { putc(' '); }
  void endl() { putc('\n'); }
  struct _f {~_f() { flush(); }} __f;
}
using io::read; using io::write; using io::flush; using io::space; using io::endl; using io::getc; using io::putc;

modmath.cpp 基礎模域運算

using ll = long long;

const int M = 998244353;

void reduceR(int &x) { x += M & (x >> 31); }
int reduceN(int x) { return x + (M & (x >> 31)); }
int add(int x, int y) { return reduceN(x + y - M); }
int sub(int x, int y) { return reduceN(x - y); }
void inc(int &x, int y) { reduceR(x += y - M); }
void dec(int &x, int y) { reduceR(x -= y); }
int power(int x, int y) {
  int p = 1;
  for (; y; y >>= 1, x = (ll)x * x % M)
    if (y & 1) p = (ll)p * x % M;
  return p;
}
int inv(int x) { return power(x, M - 2); }

最小化靜態內存分配單向鏈表實現 (默認 buffer 256 MB,支持 range based for 訪問)

char pool[256 << 20], *alloc = pool;
// minimal forward_list implementation with static memory allocation
template <class T>
struct flist {
  struct node {
    T val;
    node *nxt;
  };
  node *head;
  // no initalization at global space
  // flist() : head(nullptr) {}
  struct iterator {
    node *ptr;
    iterator() = default;
    iterator(node *p) : ptr(p) {}
    operator T*() { return &ptr->val; }
    bool operator==(const iterator &rhs) const { return ptr == rhs.ptr; }
    bool operator!=(const iterator &rhs) const { return ptr != rhs.ptr; }
    iterator operator++() { ptr = ptr->nxt; return iterator(ptr); }
  };
  iterator begin() { return iterator(head); }
  constexpr iterator end() { return iterator(nullptr); }

  void push(const T &x) {
    node *p = (node *)alloc; alloc += sizeof(node);
    p->val = x; p->nxt = head; head = p;
  }
};

靜態內存分配器(可以在舊版 GCC 上使用,包含一些在 C++17 中 deprecated 的特性)

char _pool[32 << 20], *alloc = _pool;
template <class T>
struct static_allocator {
  using value_type = T;
  using pointer = T*;
  using const_pointer = const T*;
  using reference = T&;
  using const_reference = const T&;
  using void_pointer = void *;
  using const_void_pointer = const void *;
  using size_type = size_t;
  using difference_type = ptrdiff_t;
  template <class U>
  struct rebind {
    using other = static_allocator<U>;
  };
  T *allocate(int n) { T *ret = (T*)alloc; alloc += sizeof(T) * n; return ret; }
  void deallocate(pointer p, int n) {}
  template< class U, class... Args >
  void construct(U* p, Args&&... args) {
    ::new((void *)p) U(std::forward<Args>(args)...);
  }
  void destroy(pointer p) { p->~T(); }
  constexpr bool operator==(const static_allocator &rhs) const { return true; }
};

不定模數快速取模 FastMod

typedef unsigned long long ull;
typedef __uint128_t L;
struct FastMod {
  ull b, m;
  FastMod() {}
  FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
  ull reduce(ull a) {
    ull q = (ull)((L(m) * a) >> 64);
    ull r = a - q * b; // can be proven that 0 <= r < 2*b
    return r >= b ? r - b : r;
  }
};

Codeforces 比賽模板

#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <iostream>
#include <string>
#include <array>
#include <random>
#include <chrono>
#include <vector>
using namespace std;
#ifndef ONLINE_JUDGE
#define File(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#else
#define File(s)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define mkp make_pair
#define pb push_back
#define SP putchar(' ')
#define EL putchar('\n')

mt19937 rnd(chrono :: steady_clock :: now().time_since_epoch().count());
mt19937_64 rnd64(chrono :: steady_clock :: now().time_since_epoch().count()) ;

template<class T> inline void gi(T &x){
  char ch; x = 0;int f = 1;
  while(isspace(ch = getchar()));
  if(ch == '-') ch = getchar(), f = -1;
  do x = x * 10 + ch - '0'; while(isdigit(ch = getchar()));
  x *= f;
}
template<class T> inline void wi(T x){
  static char stk[128]; int top = 0;
  if(x == 0) putchar('0');
  if(x < 0) putchar('-'), x = -x;
  while(x) stk[++top] = x % 10, x /= 10;
  while(top) putchar(stk[top--] + '0');
}
template<class T, class ...Args>
inline void gi(T &x, Args&... args) {gi(x); gi(args...);}
template<class T, class ...Args>
inline void wi(T x, Args... args) {wi(x); SP; wi(args...);}
template<class T> void upmax(T &x, T y) {x = x > y ? x : y;}
template<class T> void upmin(T &x, T y) {x = x < y ? x : y;}

int main(){
  
  return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM