QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#480164#5. 在线 O(1) 逆元Williamxzh#100 5429ms160032kbC++201016b2024-07-16 09:02:072024-07-16 09:02:07

Judging History

你现在查看的是测评时间为 2024-07-16 09:02:07 的历史记录

  • [2024-11-05 22:01:27]
  • 管理员手动重测本题所有提交记录
  • 测评结果:80
  • 用时:5150ms
  • 内存:160148kb
  • [2024-07-16 09:02:07]
  • 评测
  • 测评结果:100
  • 用时:5429ms
  • 内存:160032kb
  • [2024-07-16 09:02:07]
  • 提交

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=2e7+5;
int n,fac[N],iv[N],b[31];
void init(int MOD){
	n=N-5,fac[0]=1;for(int i=1;i<=n;++i) fac[i]=(1ll*fac[i-1]*i)%mod;
	iv[n]=qp(fac[n],mod-2ll);for(int i=n-1;i>=0;--i) iv[i]=(iv[i+1]*(i+1ll))%mod;
	for(int i=1;i<=n;++i) iv[i]=(1ll*fac[i-1]*iv[i])%mod;
	for(int i=1;i<=29;++i) b[i]=qp(1ll<<i,mod-2ll);
}
il int INV(int x){
	if(x<=n) return iv[x];
	int y=mod/x;return 1ll*(mod-y)*INV(mod-x*y)%mod;
}
int inv(int x){
	if(x&1) return INV(x);
	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: 194ms
memory: 160032kb

Test #2:

score: 40
Accepted
time: 808ms
memory: 159944kb

Test #3:

score: 30
Accepted
time: 5429ms
memory: 159952kb