QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#480152 | #5. 在线 O(1) 逆元 | Williamxzh# | 70 | 1209ms | 199064kb | C++20 | 978b | 2024-07-16 08:54:10 | 2024-07-16 08:54:10 |
Judging History
answer
#include <bits/stdc++.h>
//#include "inv.h"
#define il inline
#define B __int128
using namespace std;
typedef long long ll;
const int mod=998244353;
il ll qp(ll a,ll b){
ll ans=1ll;
while(b){
if(b&1) ans=(ans*a)%mod;
a=(a*a)%mod,b>>=1;
}return ans;
}
const ll N=5e7+5;
int n,iv[N],b[31];
void init(int MOD){
int x=1;n=N-5;for(int i=1;i<=n;++i) x=(1ll*x*i)%mod;
iv[n]=qp(x,mod-2ll);for(int i=n-1;i>=0;--i) iv[i]=(iv[i+1]*(i+1ll))%mod;
x=1;for(int i=1;i<=n;++i) iv[i]=(1ll*x*iv[i])%mod,x=(1ll*x*i)%mod;
for(int i=0;i<=29;++i) b[i]=qp(1ll<<i,mod-2ll);
}
int inv(int x){
if(x<=n) return iv[x];
if(x&1){
int y=mod/x;
return 1ll*(mod-y)*inv(mod-x*y)%mod;
}
int y=__builtin_ctz(x);
return 1ll*inv(x>>y)*b[y]%mod;
}
/*int main(){
init(mod);
mt19937 rnd(time(0));
for(int i=1;i<=100;++i){
int x=rnd()%(mod-1)+1;
if(inv(x)!=qp(x,mod-2ll)) printf("%d %d %d\n",x,inv(x),qp(x,mod-2ll));
//else puts("*");
}
return 0;
}*/
Details
Test #1:
score: 30
Accepted
time: 648ms
memory: 199064kb
Test #2:
score: 40
Accepted
time: 1209ms
memory: 198968kb
Test #3:
score: 0
Time Limit Exceeded