QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#479132 | #5. 在线 O(1) 逆元 | A_programmer# | 100 ✓ | 3495ms | 25280kb | C++17 | 1.0kb | 2024-07-15 15:17:13 | 2024-07-15 15:17:14 |
Judging History
answer
#include "inv.h"
#include <bits/stdc++.h>
using namespace std;
const int B = 1 << 10, B2 = 1 << 20;
int Inv[B2 << 2], mod;
int U[B2 | 10], vl[B2 | 10];
int* iv;
void init(int P)
{
iv = Inv + B2;
iv[1] = 1; mod = P;
for (int i = 2; i <= (B2 << 1); i++) iv[i] = 1ll * (P - P / i) * iv[P % i] % P;
for (int i = 1; i <= B2; i++) iv[-i] = mod - iv[i];
U[0] = 1;
for (int u = 1; u <= B; u++)
{
int uB = u << 10, sum = 0;
for (int k = 1; k <= B2; k++)
{
sum = ((sum += uB) >= P ? sum - P : sum);
if (sum <= B2)
{
if (!U[k]) U[k] = u, vl[k] = sum;
}
else if (sum >= P - B2)
{
if (!U[k]) U[k] = u, vl[k] = sum - P;
}
else
{
int nxt = (P - B2 - sum - 1) / uB;
k += nxt, sum += nxt * uB;
}
}
}
}
int inv(int x) { return 1ll * iv[vl[x >> 10] + (x & 1023) * U[x >> 10]] * U[x >> 10] % mod; }
Details
Test #1:
score: 30
Accepted
time: 23ms
memory: 24988kb
Test #2:
score: 40
Accepted
time: 276ms
memory: 24720kb
Test #3:
score: 30
Accepted
time: 3495ms
memory: 25280kb