QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#92346 | #5. 在线 O(1) 逆元 | Crysfly# | 100 ✓ | 2135ms | 21360kb | C++17 | 3.3kb | 2023-03-30 16:02:50 | 2024-11-05 21:47:57 |
Judging History
answer
/*
不会卡常 /kel
存个 skip2004 的代码在这。。
*/
#include "inv.h"
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define ull unsigned long long
using namespace std;
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define mod 998244353
struct modint{
int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,int b){return a.x==b;}
friend bool operator !=(modint a,int b){return a.x!=b;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 300005
#define inf 0x3f3f3f3f
const int mod2 = 974849;
const int w = sqrt(mod2);
const int limit = 3 * (w + 1) * 1024;
int djq[limit], arr[mod2];
int inv(int x) {
const int p = (ull) x * arr[x >> 10] % mod;
/*
if(p >= limit && p <= mod - limit) {
std::cerr << "debug : " << x << '\n';
}
*/
return (ull) arr[x >> 10] * (p < limit ? djq[p] : mod - djq[mod - p]) % mod;
}
int getinv(int x) {
return x == 1 ? 1 : mod2 - (ull)(mod2 / x) * getinv(mod2 % x) % mod2;
}
void init(int) {
for(int i = w;i >= 1;--i) {
int inv = getinv(i);
for(int j = 1;j <= w;++j) {
arr[(ull) j * inv % mod2] = i;
arr[mod2 - (ull) j * inv % mod2] = mod - i;
}
}
arr[0] = 1;
// 0 <= x * arr[x] - k * mod2 < w, and arr[x] < w, k < w
// 998244353 = mod2 * 1024 - 1023
// 0 <= 1024x * arr[x] - k * 998244353 < w * 1024 + 1023 * k
// 0 <= (1024x + y) * arr[x] - k * 998244353 < w * 1024 + 1023 * k + 1024 * w
djq[1] = 1;
// 等下再优化这个
for(int i = 2;i < limit;++i) {
djq[i] = mod - (ull)(mod / i) * djq[mod % i] % mod;
}
}
详细
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 11ms
memory: 21360kb
Test #2:
score: 20
Accepted
time: 228ms
memory: 20228kb
Test #3:
score: 30
Accepted
time: 1067ms
memory: 19932kb
Test #4:
score: 20
Accepted
time: 1714ms
memory: 20256kb
Test #5:
score: 20
Accepted
time: 2135ms
memory: 20184kb