【存檔】缺省源

主要內容(經常使用)

如今經常使用的幾個缺省源,包括:c++

  • 快讀
  • 最大最小值
  • 帶模加法乘法(函數打包)
  • 線性篩 + \(\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]);}

主要內容(其餘)

不經常使用的:git

  • 分數加運算,乘運算
  • 分數判斷大小的邏輯運算

分數運算

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);}
}
相關文章
相關標籤/搜索