主要内容(常用)
现在常用的几个缺省源,包括:
- 快读
- 最大最小值
- 带模加法乘法(函数打包)
- 线性筛 + (varphi) 函数
- 普通 (gcd)(循环)、扩展 (gcd)(递归)
- 龟速乘、快速幂
- 乘法逆元
- 组合数
快读 + 最大最小值
#include <bits/stdc++.h>
#define LL long long
const int Maxn = /**/;
const LL Mod = /**/;
namespace Basic {
template <typename Temp>
inline void read(Temp & res) {
Temp fh = 1; res = 0; char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') fh = -1;
for(; isdigit(ch); ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ '0');
res = res * fh;
}
template <typename Temp> inline void Checkmax(Temp & num, Temp comp) {if(comp > num) num = comp;}
template <typename Temp> inline void Checkmin(Temp & num, Temp comp) {if(comp < num) num = comp;}
}
四则运算
inline LL add(LL A, LL B) {return A + B > Mod ? A + B - Mod : A + B;}
inline LL mul(LL A, LL B) {return A * B % Mod;}
inline LL slowmul(LL A, LL B) {LL res = 0; for(; B; B >>= 1, A = add(A, A)) if(B & 1) res = add(res, A); return res;}
inline LL qpow(LL A, LL B) {LL res = 1; for(; B; B >>= 1, A = mul(A, A)) if(B & 1) res = mul(res, A); return res;}
inline LL Inv(LL A) {return qpow(A, Mod - 2);}
扩展欧几里得
inline LL gcd_(LL A, LL B) {LL C; while(B) C = B, B = A % B, A = C; return A;}
LL gcd(LL A, LL B, LL & X, LL & Y) {
if(B == 0) {X = 1, Y = 0; return A;}
LL res = gcd(B, A % B, X, Y);
LL temp = Y; Y = X - A / B * Y; X = temp;
return res;
}
线性筛 + (varphi) 函数
bool isprime[MAXN];
int prime[MAXN], cnt_prime = 0;
LL phi[MAXN];
inline void Euler(int N) {
phi[1] = 1;
for(register int i = 2; i <= N; ++i) {
if(!isprime[i]) prime[++cnt_prime] = i, phi[i] = i - 1;
for(register int j = 1; (j <= cnt_prime) && (i * prime[j] <= N); ++j) {
isprime[i * prime[j]] = 1;
phi[i * prime[j]] = phi[i] * (prime[j] - (i % prime[j] != 0));
}
}
}
逆元和组合数
LL inv[MAXN];
LL invf[MAXN];
LL func[MAXN];
inline void init_inv(int N) {
func[0] = 1;
for(register int i = 1; i <= N; ++i) func[i] = mul(func[i - 1], (LL)i);
invf[N] = Inv(func[N]);
for(register int i = N; i >= 0; --i) {
if(i ^ N) invf[i] = mul(invf[i + 1], (LL)(i + 1));
if(i) inv[i] = mul(invf[i], func[i - 1]);
}
}
inline LL choose(int N, int M) {if(N < M) return 0ll; return mul(mul(func[N], invf[M]), invf[N - M]);}
主要内容(其他)
不常用的:
- 分数加运算,乘运算
- 分数判断大小的逻辑运算
分数运算
struct fraction {
LL Numerator, Denominator;
};
namespace fraction_calculation {
fraction operator + (fraction A, fraction B) {
fraction C;
C.Denominator = lcm(A.Denominator, B.Denominator);
C.Numerator = A.Numerator * (C.Denominator / A.Denominator) + B.Numerator * (C.Denominator / B.Denominator);
LL F = gcd(C.Numerator, C.Denominator);
C.Denominator /= F; C.Numerator /= F;
return C;
}
fraction operator * (fraction A, fraction B) {LL F1 = gcd(A.Denominator, B.Numerator), F2 = gcd(A.Numerator, B.Denominator); return (fraction){A.Numerator / F2 * B.Numerator / F1, A.Denominator / F1 * B.Denominator / F2};}
bool operator < (fraction A, fraction B) {LL F = lcm(A.Denominator, B.Denominator); return A.Numerator * F / A.Denominator < B.Numerator * F / B.Denominator;}
bool operator == (fraction A, fraction B) {return (A.Denominator == B.Denominator) && (A.Numerator == B.Numerator);}
bool operator <= (fraction A, fraction B) {return (A < B) || (A == B);}
}