QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#119674 | #6179. 最大平方问题 | wangyian2022 | 100 ✓ | 242ms | 10272kb | C++14 | 3.7kb | 2023-07-05 14:21:27 | 2023-07-05 14:21:29 |
Judging History
answer
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<random>
#include<unordered_map>
#define int long long
const int Q=500005;
const int INF=(1<<30);
const int mod=998244353;
typedef long long ll;
#define rg register int
#define cint const register int
//char ibuf[1<<21],*IP1=ibuf,*IP2=ibuf;
//#define gc() (__builtin_expect(IP1==IP2,0)&&(IP2=(IP1=ibuf)+fread(ibuf,1,1<<21,stdin),__builtin_expect(IP1==IP2,0))?EOF:*IP1++)
#define gc getchar
#define pc putchar
inline bool ig(const char c){return c>=48&&c<=57;}
inline void read(rg&oi){char c;rg f=1,res=0;while(c=gc(),(!ig(c))&&c^'-');c^'-'?res=(c^48):f=-1;while(c=gc(),ig(c))res=res*10+(c^48);oi=f*res;}
inline void print(rg oi){char io[23];rg l=0;if(oi<0)pc('-'),oi=~oi+1;do io[++l]=(oi%10+48);while(oi/=10);for(;l;--l)pc(io[l]);}
inline void write(cint oi,const char c){print(oi);pc(c);}char _ST_;
inline int max(cint x,cint y){return x>y?x:y;}
inline int powt(rg x,rg y){rg res=1;for(;y;y>>=1,x=1ll*x*x%mod)(y&1)&&(res=1ll*res*x%mod);return res;}
int A,B,C,n,m;ll f[Q],g[Q],dlt;
std::unordered_map<ll,int>cnt;bool divg;
inline void Fdiv(cint o,cint p){if(o>=1&&o<=n){for(ll&x=f[o];x%p==0;x/=p,++cnt[p]);}}
inline void Gdiv(cint o,cint p){if(o>=1&&o<=m){for(ll&x=g[o];x%p==0;x/=p);}}
struct cpl{int x,y;cpl()=default;cpl(cint X,cint Y):x(X),y(Y){}};
std::random_device sd;std::mt19937 rnd(sd());
inline void upd(cint p){
if(p<=1)return;if(p==2||std::__gcd(p,A)!=1){
for(rg i=1;i<=n;++i)Fdiv(i,p);for(rg i=1;i<=m;++i)Gdiv(i,p);return;}
const auto inc=[&](cint x,cint y){return x+y<p?x+y:x+y-p;};
const auto dec=[&](cint x,cint y){return x>=y?x-y:x-y+p;};
const auto mul=[&](cint x,cint y){return (__int128)x*y%p;};
const auto Mul=[&](rg&x,cint y){x=(__int128)x*y%p;};
const auto pow=[&](rg x,rg y){rg res=1;for(;y;y>>=1,Mul(x,x))(y&1)&&(Mul(res,x),1);return res;};
const auto Inv=[&](cint x){return pow(x,p-2);};
rg w=0;const auto mg=[&](cpl x,cpl y){
return cpl(inc(mul(x.x,y.x),mul(w,mul(x.y,y.y))),inc(mul(x.x,y.y),mul(x.y,y.x)));};
std::uniform_int_distribution<int>rng(0,p-1);
const auto powc=[&](cpl x,rg y){
cpl res=cpl(1,0);for(;y;y>>=1,x=mg(x,x))(y&1)&&(res=mg(res,x),1);return res;};
const auto Gsqrt=[&](cint x){
if(pow(x,(p-1)>>1)==p-1)return -1ll;if(!x)return 0ll;rg y=rng(rnd);w=dec(mul(y,y),x);
for(;pow(w,(p-1)>>1)!=p-1;y=rng(rnd),w=dec(mul(y,y),x));return powc(cpl(y,1),(p+1)>>1).x;
};
cint iv=Inv(2ll*A%p),nB=dec(0,B%p);
if(divg){rg x=mul(nB,Inv(A%p));x=x?x:p;for(rg i=0;i<=m;i+=p)Gdiv(i+x,p);}
cint dt=(dlt%p+p)%p,sqt=Gsqrt(dt);if(!~sqt)return;
rg x0=mul(iv,inc(nB,sqt)),x1=mul(iv,dec(nB,sqt));x0=x0?x0:p;x1=x1?x1:p;
if(x0^x1){for(rg i=0;i<=n;i+=p)Fdiv(i+x0,p),Fdiv(i+x1,p);}
else{for(rg i=0;i<=n;i+=p)Fdiv(i+x0,p);}
}
int pr[Q],tot;bool isp[Q];
inline void solve1(){
dlt=1ll*B*B-4ll*A*C;
cint N=max(n,max(sqrt(g[m]),pow(f[n],1.0/3)))+15;
for(rg i=2;i<=N;++i){
if(!isp[i])pr[++tot]=i;
for(rg j=1;j<=tot&&pr[j]*i<=N;++j){
isp[pr[j]*i]=1;if(!(i%pr[j]))break;
}
}
divg=1;for(rg i=1;i<=tot;++i)upd(pr[i]);divg=0;
}
inline void solve2(){
std::sort(g+1,g+1+m);m=std::unique(g+1,g+1+m)-g-1;
for(rg i=1;i<=m;++i)upd(g[i]);
}
inline void solve3(){
for(rg i=1;i<=n;++i)if(f[i]>1){
const ll x=sqrtl(f[i]);if(x*x==f[i])cnt[x]+=2;}
}
char _ED_;signed main(){
fprintf(stderr,"memory:%llu MB\n",(&_ST_-&_ED_)>>20);
read(A);read(B);read(C);read(n);m=n<<1;
for(rg i=1;i<=n;++i)f[i]=1ll*A*i*i+1ll*B*i+C;
for(rg i=1;i<=m;++i)g[i]=1ll*A*i+B;
solve1();//small prime
solve2();//prime left from g[i]
solve3();//p^2 left from f[i]
rg res=1;for(auto x:cnt)res=1ll*res*
powt(x.first,x.second-(x.second&1))%mod;write(res,'\n');
return 0;
}
/*
114514 191981 192608 200000
*/
詳細信息
Test #1:
score: 5
Accepted
time: 0ms
memory: 7404kb
input:
17 3 17 14
output:
123798013
result:
ok 1 number(s): "123798013"
Test #2:
score: 5
Accepted
time: 2ms
memory: 7404kb
input:
1 18 0 6
output:
2073600
result:
ok 1 number(s): "2073600"
Test #3:
score: 5
Accepted
time: 2ms
memory: 7284kb
input:
20 6 20 14
output:
315851899
result:
ok 1 number(s): "315851899"
Test #4:
score: 5
Accepted
time: 0ms
memory: 7168kb
input:
81 190 40 125
output:
924770602
result:
ok 1 number(s): "924770602"
Test #5:
score: 5
Accepted
time: 0ms
memory: 7280kb
input:
108 31 110 122
output:
319532761
result:
ok 1 number(s): "319532761"
Test #6:
score: 5
Accepted
time: 3ms
memory: 7312kb
input:
988 194 1412 934
output:
871618879
result:
ok 1 number(s): "871618879"
Test #7:
score: 5
Accepted
time: 1ms
memory: 7252kb
input:
1956 1305 92 1061
output:
681879967
result:
ok 1 number(s): "681879967"
Test #8:
score: 5
Accepted
time: 8ms
memory: 7352kb
input:
75955 165029 149078 5607
output:
739160804
result:
ok 1 number(s): "739160804"
Test #9:
score: 5
Accepted
time: 5ms
memory: 7284kb
input:
3223 4143 3383 4499
output:
139873119
result:
ok 1 number(s): "139873119"
Test #10:
score: 5
Accepted
time: 15ms
memory: 7500kb
input:
9257 3632 1736 9497
output:
158679485
result:
ok 1 number(s): "158679485"
Test #11:
score: 5
Accepted
time: 23ms
memory: 7428kb
input:
13657 8517 9780 16829
output:
499148359
result:
ok 1 number(s): "499148359"
Test #12:
score: 5
Accepted
time: 18ms
memory: 7608kb
input:
152794 105986 145639 8507
output:
432311896
result:
ok 1 number(s): "432311896"
Test #13:
score: 5
Accepted
time: 2ms
memory: 7304kb
input:
2209 13866 42748 227
output:
576767941
result:
ok 1 number(s): "576767941"
Test #14:
score: 5
Accepted
time: 5ms
memory: 7328kb
input:
14279 7290 25915 2265
output:
173729704
result:
ok 1 number(s): "173729704"
Test #15:
score: 5
Accepted
time: 45ms
memory: 9488kb
input:
24703 6569 26356 26534
output:
108700579
result:
ok 1 number(s): "108700579"
Test #16:
score: 5
Accepted
time: 30ms
memory: 7592kb
input:
187348 185285 76358 13718
output:
425402711
result:
ok 1 number(s): "425402711"
Test #17:
score: 5
Accepted
time: 29ms
memory: 7520kb
input:
189507 133399 143282 13930
output:
608644351
result:
ok 1 number(s): "608644351"
Test #18:
score: 5
Accepted
time: 242ms
memory: 10272kb
input:
102698 162019 98595 137546
output:
388084925
result:
ok 1 number(s): "388084925"
Test #19:
score: 5
Accepted
time: 27ms
memory: 7684kb
input:
152381 90362 151048 15578
output:
413943175
result:
ok 1 number(s): "413943175"
Test #20:
score: 5
Accepted
time: 106ms
memory: 7952kb
input:
34402 49259 74598 64198
output:
733372582
result:
ok 1 number(s): "733372582"