一些敲可爱的数论板子
exgcd:
pair<ll, ll> exgcd(ll a, ll b)
{
if (!b) return make_pair(1, 0); pair<ll, ll> x = exgcd(b, a % b);
return make_pair(x.second, x.first - a / b * x.second);
}
快速乘(感谢 lzy 大佬,真的可以放心使用)。
ll mul(ll x, ll y, ll P)
{
ll z = x * y - (ll)((long double)x / P * y + 0.5L) * P; return z < P ? z : z + P;
}
线性筛:
int pr[maxn], tt, mu[maxn]; bool isp[maxn];
inline void prework()
{
mu[1] = 1; for (int i = 2; i < maxn; i++)
{
if (!isp[i]) pr[++tt] = i, mu[i] = -1;
for (int j = 1, t; j <= tt && ((t = i * pr[j]) < maxn); j++)
{
isp[t] = true; if (i % pr[j] == 0) continue; mu[t] = -mu[i];
}
}
}
杜教筛(模板题):
inline int QSmu(int n)
{
if (Smu[n]) return Smu[n]; if (n < maxn) return mu[n]; int rs = 0;
for (unsigned int l = 2, r; l <= n; l = r + 1)
{
r = n / (n / l); rs -= QSmu(n / l) * (r - l + 1);
}
return Smu[n] = 1 + rs;
}
inline ll QSph(int n)
{
ll ans = 0; for (unsigned int l = 1, r; l <= n; l = r + 1)
{
r = n / (n / l), ans += 1ll * (QSmu(r) - QSmu(l - 1)) * (n / l) * (n / l);
}
return ans;
}
exCRT:
inline ll exCRT()
{
ll x = 0, M = 1; mx = 0; for (int i = 1; i <= n; i++)
{
mx = max(mx, (ll)ceil(h[i] * 1.0 / b[i]));
ll A = ..., B = ..., C = ...;
pair<ll, ll> t = exgcd(A, B); ll bg = gcd(A, B); t.first = (t.first % B + B) % B;
if (C % bg) return -1; x += mul(mul(C / bg, t.first, B / bg), M, M * (B / bg)); M *= B / bg; x %= M;
}
if (x < mx) x += ceil(mx * 1.0 / M) * M; return x;
}
as 0.4123