O(1) 查询三则
随笔。
- 光速幂
假设我们要多次查询
选定基数
可能的实现一则:
struct {
vector<vmi> pw;
void init(mint q, int n, int b) {
pw.resize(b);
for (auto& v : pw) {
v.resize(n);
mint s = exchange(q, 1);
for (int i = 0; i < n; ++i) v[i] = exchange(q, q * s);
}
}
mint operator()(ll x) const {
mint ret = 1;
for (auto& v : pw) {
auto [q, r] = div(x, v.size());
x = q, ret *= v[r];
}
return ret;
}
};
其中 mint 是取模整数,vmi 是 vector<mint>。这个东西以
- 快速 gcd
假设我们要多次查询
这个算法基于以下的事实:对于任意正整数
对于归纳的正确性证明,不难发现只有
于是我们跑一遍欧拉筛就能把
可能的实现一则:
constexpr int N = 1e6 + 9, S = 1e3 + 9;
array<int, 3> fct[N];
int prm[N >> 3], pre[S][S];
bool isnp[N];
inline void sieve() {
fct[1] = {1, 1, 1};
for (int i = 2; i < N; ++i) {
if (!isnp[i]) prm[++prm[0]] = i, fct[i] = {1, 1, i};
for (int j = 1; j <= prm[0]; ++j) {
int p = prm[j], v = i * p;
if (v >= N) break;
isnp[v] = true, fct[v] = fct[i];
*min_element(fct[v].begin(), fct[v].end()) *= p;
if (!(i % p)) break;
}
}
}
inline void init() {
sieve(), iota(pre[0], pre[0] + S, 0);
for (int i = 1; i < S; ++i) {
pre[i][0] = i;
for (int j = 1; j < i; ++j) pre[i][j] = pre[i - j][j];
for (int j = i; j < S; ++j) pre[i][j] = pre[i][j - i];
}
}
inline int sgcd(int x, int y) { return x >= S ? y % x ? 1 : x : pre[x][y % x]; }
inline int gcd(int x, int y) {
int ret0 = sgcd(fct[x][0], y), ret1 = sgcd(fct[x][1], y /= ret0),
ret2 = sgcd(fct[x][2], y /= ret1);
return ret0 * ret1 * ret2;
}
- 在线逆元
众所周知对
预设值
现在最后的问题就是前驱后继怎么求。这个事情比较简单,考虑到上面我们那个差还有下界,也就是说
可能的实现一则(洛谷乘法逆元二,注意这个题有点卡常):
#include <bits/stdc++.h>
using namespace std;
constexpr int P = 1e9, B = 1000, M = B * B, W = P / B, S = 1 << 23;
char buf[S], *p1, *p2;
inline char nc() {
if (p1 == p2) {
p2 = (p1 = buf) + cin.read(buf, S).gcount();
if (p1 == p2) return EOF;
}
return *p1++;
}
inline int rd() {
char ch;
while (!isdigit(ch = nc()))
;
int x = ch & 0xf;
while (isdigit(ch = nc())) x = x * 10 + (ch & 0xf);
return x;
}
int n, k, ans, p, iv[W], pl[M], pr[M], vx[M], vy[M];
struct modder {
typedef unsigned long long ull;
typedef __uint128_t L;
ull b, m;
modder() {}
modder(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull operator()(ull a) const {
ull q = (ull)((L(m) * a) >> 64), r = a - q * b;
return r >= b ? r - b : r;
}
ull div(ull a) const {
ull q = (ull)((L(m) * a) >> 64);
return q + (a - q * b >= b);
}
} mod;
inline int calc(int w, int v) {
int y = vy[v], d = 1ll * w * y - 1ll * vx[v] * p;
if (d < 0) d += p;
if (d < W) return mod(1ll * iv[d] * y);
if (d > p - W) return p - mod(1ll * iv[p - d] * y);
return 0;
}
inline int ginv(int x) {
if (x < W) return iv[x];
if (x > p - W) return p - iv[p - x];
int v = mod.div(1ll * x * B * B);
if (int q = calc(x, pl[v])) return q;
return calc(x, pr[v]);
}
inline void init() {
iv[1] = 1;
for (int i = 2; i < W; ++i) iv[i] = mod(1ll * (p - p / i) * iv[p % i]);
for (int i = 2; i <= B; ++i)
for (int j = 1; j < i; ++j)
if (int v = 1ll * j * B * B / i; !vy[v]) vx[v] = j, vy[v] = i;
for (int i = M - 1, p = M; i; pr[i--] = p) vy[i] && (p = i);
for (int i = 1, p = 0; i < M; pl[i++] = p) vy[i] && (p = i);
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
n = rd(), mod = modder(p = rd()), k = rd(), init();
for (int i = 0, a, pw = 1; i < n; ++i) {
a = rd(), pw = mod(1ll * pw * k);
ans = mod(ans + 1ll * pw * ginv(a));
}
return cout << ans << endl, 0;
}
以上。