QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#134755 | #5. 在线 O(1) 逆元 | sserxhs | 100 ✓ | 2611ms | 24812kb | C++20 | 971b | 2023-08-04 20:45:14 | 2024-11-05 21:51:12 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
namespace online_inv
{
typedef unsigned int ui;
typedef unsigned long long ll;
const ui p=998244353;
const ui n=1010,m=n*n,N=m+2;
int l[N],r[N];
ui y[N];
bool s[N];
ui _inv[N*2],i,j,k;
void init_inv()
{
assert(n*n*n>p);
_inv[1]=1;
for (i=2; i<m*2; i++)
{
j=p/i;
_inv[i]=(ll)(p-j)*_inv[p-i*j]%p;
}
s[0]=y[0]=1;
for (i=1; i<n; i++) for (j=i; j<n; j++) if (!s[k=i*m/j])
{
y[k]=j;
s[k]=1;
}
l[0]=1;
for (i=1; i<=m; i++) l[i]=s[i]?y[i]:l[i-1];
r[m]=1;
for (i=m-1; ~i; i--) r[i]=s[i]?y[i]:r[i+1];
for (i=0; i<=m; i++) y[i]=min(l[i],r[i]);
}
inline ui inv(const ui &x)
{
assert(x&&x<p);
if (x<m*2) return _inv[x];
k=(ll)x*m/p;
j=(ll)y[k]*x%p;
return (j<m*2?_inv[j]:p-_inv[p-j])*(ll)y[k]%p;
}
}
using online_inv::init_inv,online_inv::p;
void init(int p)
{
init_inv();
}
int inv(int x)
{
return online_inv::inv(x);
}
Details
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 21ms
memory: 24812kb
Test #2:
score: 20
Accepted
time: 274ms
memory: 24704kb
Test #3:
score: 30
Accepted
time: 1337ms
memory: 24700kb
Test #4:
score: 20
Accepted
time: 2088ms
memory: 24616kb
Test #5:
score: 20
Accepted
time: 2611ms
memory: 24812kb