QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#526522 | #5. 在线 O(1) 逆元 | December456 | 100 ✓ | 5931ms | 19508kb | C++14 | 617b | 2024-08-21 17:01:32 | 2024-08-21 17:01:32 |
Judging History
answer
#include "inv.h"
constexpr int INIT_BOUND = 4000000;
constexpr int P = 998244353;
int _inv[INIT_BOUND];
void init(int p) {
_inv[1] = 1;
for (int i = 2; i < INIT_BOUND; i ++) {
_inv[i] = (long long){p - p / i} * _inv[p % i] % p;
}
}
int inv(int x) {
int ret = 1;
while (x >= INIT_BOUND) {
int y = P % x;
if (y << 1 <= x) {
ret = (long long){P - P / x} * ret % P;
x = y;
} else {
ret = (P / x + 1ll) * ret % P;
x = x - y;
}
}
return (long long){ret} * _inv[x] % P;
}
详细
Pretests
Final Tests
Test #1:
score: 30
Accepted
time: 25ms
memory: 19508kb
Test #2:
score: 40
Accepted
time: 599ms
memory: 19316kb
Test #3:
score: 30
Accepted
time: 5931ms
memory: 19412kb