QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#534360 | #5. 在线 O(1) 逆元 | surgutti | 100 ✓ | 4065ms | 14404kb | C++14 | 971b | 2024-08-27 04:47:19 | 2024-08-27 04:47:20 |
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;
}
Details
Pretests
Final Tests
Test #1:
score: 30
Accepted
time: 14ms
memory: 13076kb
Test #2:
score: 40
Accepted
time: 409ms
memory: 14308kb
Test #3:
score: 30
Accepted
time: 4065ms
memory: 14404kb