QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#119498 | #6179. 最大平方问题 | wangyian2022 | 15 | 7ms | 7120kb | C++14 | 3.9kb | 2023-07-05 11:50:17 | 2023-07-05 11:50:29 |
Judging History
answer
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<random>
#include<unordered_map>
const int Q=300005;
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){
// printf("upd:%d\n",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 1ll*x*y%p;};
const auto Mul=[&](rg&x,cint y){x=1ll*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){
// printf("powc:%d %d|%d %d\n",x.x,x.y,y,w);
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 -1;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));
// printf("wrbwev:%d %d %d %d %d\n",x,y,(1ll*y*y-x+p)%p,w,pow(w,(p-1)>>1));
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;
// printf("!&*!($F)#!N$_FJ_)!#K$F!@$FVI!U#B$)FH!_#VC!@#V$)ask:%d %d %d\n",dt,sqt,mul(sqt,sqt));
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(){
// puts("_____________");
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_;int 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)/*printf("wefwefb e:%d %d\n",x.first,x.second),*/res=1ll*res*
powt(x.first,x.second-(x.second&1))%mod;write(res,'\n');
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
17 3 17 14
output:
result:
Test #2:
score: 0
Time Limit Exceeded
input:
1 18 0 6
output:
result:
Test #3:
score: 0
Time Limit Exceeded
input:
20 6 20 14
output:
result:
Test #4:
score: 0
Time Limit Exceeded
input:
81 190 40 125
output:
result:
Test #5:
score: 5
Accepted
time: 0ms
memory: 5164kb
input:
108 31 110 122
output:
319532761
result:
ok 1 number(s): "319532761"
Test #6:
score: 5
Accepted
time: 1ms
memory: 7120kb
input:
988 194 1412 934
output:
871618879
result:
ok 1 number(s): "871618879"
Test #7:
score: 0
Time Limit Exceeded
input:
1956 1305 92 1061
output:
result:
Test #8:
score: 0
Time Limit Exceeded
input:
75955 165029 149078 5607
output:
result:
Test #9:
score: 5
Accepted
time: 7ms
memory: 5384kb
input:
3223 4143 3383 4499
output:
139873119
result:
ok 1 number(s): "139873119"
Test #10:
score: 0
Time Limit Exceeded
input:
9257 3632 1736 9497
output:
result:
Test #11:
score: 0
Time Limit Exceeded
input:
13657 8517 9780 16829
output:
result:
Test #12:
score: 0
Time Limit Exceeded
input:
152794 105986 145639 8507
output:
result:
Test #13:
score: 0
Time Limit Exceeded
input:
2209 13866 42748 227
output:
result:
Test #14:
score: 0
Time Limit Exceeded
input:
14279 7290 25915 2265
output:
result:
Test #15:
score: 0
Time Limit Exceeded
input:
24703 6569 26356 26534
output:
result:
Test #16:
score: 0
Time Limit Exceeded
input:
187348 185285 76358 13718
output:
result:
Test #17:
score: 0
Time Limit Exceeded
input:
189507 133399 143282 13930
output:
result:
Test #18:
score: 0
Time Limit Exceeded
input:
102698 162019 98595 137546
output:
result:
Test #19:
score: 0
Time Limit Exceeded
input:
152381 90362 151048 15578
output:
result:
Test #20:
score: 0
Time Limit Exceeded
input:
34402 49259 74598 64198