QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#534360 | #5. 在线 O(1) 逆元 | surgutti | 100 ✓ | 4719ms | 14752kb | C++14 | 971b | 2024-08-27 04:47:19 | 2024-11-05 22:03:28 |
Judging History
answer
#include "inv.h"
#include <cmath>
#include <cassert>
#include <cstdio>
constexpr int mod = 998244353;
constexpr int n = 1024;
constexpr int n2 = n * n;
constexpr int mod_n = mod / n;
int p[n2 + 1];
int f[n2 + 1];
int inv_[mod_n + 1];
void init(int _p) {
assert(_p == mod);
for (int y = 1; y < n; y++) {
for (int x = 0; x <= y; x++) {
int i = x * n2 / y;
if (!p[i]) {
p[i] = x * n + y;
}
}
}
int f_cnt = 0;
for (int i = 0; i <= n2; i++) {
if (p[i]) {
f[f_cnt++] = p[i];
}
p[i] = f_cnt;
}
inv_[1] = 1;
for (int i = 2; i <= mod_n; i++)
inv_[i] = mod - (long long) (mod / i) * inv_[mod % i] % mod;
}
int inv(int a) {
int i = p[(long long) a * n2 / mod];
int x = f[i] / n;
int y = f[i] % n;
int u = a * y - mod * x;
if (abs(u) > mod_n) {
i--;
x = f[i] / n;
y = f[i] % n;
u = a * y - mod * x;
}
return (long long) y * (u < 0 ? mod - inv_[-u] : inv_[u]) % mod;
}
详细
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 16ms
memory: 14316kb
Test #2:
score: 20
Accepted
time: 485ms
memory: 13796kb
Test #3:
score: 30
Accepted
time: 2274ms
memory: 13040kb
Test #4:
score: 20
Accepted
time: 3880ms
memory: 13560kb
Test #5:
score: 20
Accepted
time: 4719ms
memory: 14752kb