QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#119701 | #6179. 最大平方问题 | serene_analysis | 100 ✓ | 347ms | 24220kb | C++17 | 3.4kb | 2023-07-05 15:07:18 | 2023-07-05 15:07:21 |
Judging History
answer
#include<algorithm>
#include<chrono>
#include<cstdio>
#include<random>
#include<vector>
#include<cmath>
#include<ctime>
#include<map>
std::mt19937_64 tian(std::chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
const int maxn=4e5+5;
const int amod=998244353;
int pri[maxn],pcnt;
bool isnt[maxn];
void sift(int lim){
for(int i=2;i<=lim;i++){
if(!isnt[i])pri[++pcnt]=i;
for(int j=1;j<=pcnt&&1ll*i*pri[j]<=lim;j++){
isnt[i*pri[j]]=true;
if(i%pri[j]==0)break;
}
}
return;
}
ll mul(ll now,ll oth,ll mod){return (__int128)(now)*oth%mod;}
ll qpow(ll a,ll b,ll mod){
ll ans=1;
while(b){
if(b&1)ans=mul(ans,a,mod);
a=mul(a,a,mod);
b>>=1;
}
return ans;
}
namespace cipolla{
struct cpx{ll rea,ima;};
cpx cmul(cpx now,cpx oth,ll coe,ll mod){
return (cpx){(mul(now.rea,oth.rea,mod)+mul(coe,mul(now.ima,oth.ima,mod),mod))%mod,
(mul(now.rea,oth.ima,mod)+mul(now.ima,oth.rea,mod))%mod};}
ll leg(ll x,ll mod){return qpow(x,(mod-1)/2,mod);}
void sqrest(ll x,ll mod,ll &fi,ll &se){
if(x==0){
fi=0,se=0;
return;
}
if(leg(x,mod)==mod-1){
fi=-1,se=-1;
return;
}
while(true){
ll gue=tian()%mod,now=(mul(gue,gue,mod)-x+mod)%mod;
// printf("mod=%lld,gue=%lld,now=%lld,leg=%lld\n",mod,gue,now,leg(now,mod));
if(leg(now,mod)==mod-1){
cpx ret=(cpx){1,0},step=(cpx){gue,1};
ll up=(mod+1)/2;
while(up){
if(up&1)ret=cmul(ret,step,now,mod);
step=cmul(step,step,now,mod);
up>>=1;
}
fi=ret.rea,se=mod-ret.rea;
return;
}
}
return;
}
};
std::map<ll,int>apr;
std::map<ll,bool>vis;
void divide(ll &x,ll p,int coe){
// printf("divide:%lld,%lld,%d\n",x,p,coe);
while(x%p==0)x/=p,apr[p]+=coe;
return;
}
int a,b,c,n;
ll f[maxn],g[maxn];
void go(ll p){
if(p==2||a%p==0)for(int i=1;i<=n;i++)/*printf("i=%d\n",i),*/divide(f[i],p,1);
else{
ll fi=-1,se=-1,rv=mul((mul(b,b,p)-mul(4*a,c,p)+p)%p,qpow(mul(4*a,a,p),p-2,p),p);
// printf("rv=%lld\n",rv);
cipolla::sqrest(rv,p,fi,se);
// printf("ret,fi=%lld,se=%lld\n",fi,se);
if(fi!=-1){
ll dec=mul(b,qpow(2*a,p-2,p),p);
fi=(fi-dec+p)%p,se=(se-dec+p)%p;
if(fi==0)fi=p;
if(se==0)se=p;
for(ll i=fi;i<=n;i+=p)/*printf("i=%lld\n",i),*/divide(f[i],p,1);
for(ll i=se;i<=n;i+=p)/*printf("i=%lld\n",i),*/divide(f[i],p,1);
}
}
return;
}
signed main(){
scanf("%d%d%d%d",&a,&b,&c,&n);
for(int i=1;i<=n;i++)f[i]=1ll*a*i*i+1ll*b*i+c;
for(int i=1;i<=2*n;i++)g[i]=1ll*a*i+b;
int lim=std::max({n,(int)(std::pow(f[n],1.0/3)),(int)(std::sqrt(g[2*n]))});
// printf("lim=%d\n",lim);
sift(lim);
for(int i=1;i<=pcnt;i++){
int p=pri[i];
// printf("i=%d,p=%d\n",i,p);
go(p);
if(a%p==0||b%p==0){
// printf("a,b mod p=0\n");
for(int j=1;j<=2*n;j++)divide(g[j],p,0);
}
else{
// printf("a=%d,b=%d,p=%d\n",a,b,p);
int st=1ll*(p-b%p)*qpow(a,p-2,p)%p;
// printf("st=%d\n",st);
for(int j=st;j<=2*n;j+=p)divide(g[j],p,0);
}
}
vis[1]=true;
for(int i=1;i<=2*n;i++)if(!vis[g[i]])/*printf("g[i]=%lld\n",g[i]),*/vis[g[i]]=true,go(g[i]);
for(int i=1;i<=n;i++)if(f[i]!=1){
// printf("f[i]=%lld\n",f[i]);
int sq=std::sqrt(f[i])+0.1;
if(1ll*sq*sq==f[i])apr[sq]+=2;
}
int ans=1;
for(auto now:apr)ans=1ll*ans*qpow(now.first,(now.second/2)*2,amod)%amod;
printf("%d",ans);
return 0;
}
/*
17 3 17 14
123798013
*/
/*
1 18 0 6
2073600
*/
//namespace burningContract
詳細信息
Test #1:
score: 5
Accepted
time: 1ms
memory: 7396kb
input:
17 3 17 14
output:
123798013
result:
ok 1 number(s): "123798013"
Test #2:
score: 5
Accepted
time: 2ms
memory: 7468kb
input:
1 18 0 6
output:
2073600
result:
ok 1 number(s): "2073600"
Test #3:
score: 5
Accepted
time: 0ms
memory: 7464kb
input:
20 6 20 14
output:
315851899
result:
ok 1 number(s): "315851899"
Test #4:
score: 5
Accepted
time: 2ms
memory: 7424kb
input:
81 190 40 125
output:
924770602
result:
ok 1 number(s): "924770602"
Test #5:
score: 5
Accepted
time: 2ms
memory: 7472kb
input:
108 31 110 122
output:
319532761
result:
ok 1 number(s): "319532761"
Test #6:
score: 5
Accepted
time: 3ms
memory: 7628kb
input:
988 194 1412 934
output:
871618879
result:
ok 1 number(s): "871618879"
Test #7:
score: 5
Accepted
time: 3ms
memory: 7384kb
input:
1956 1305 92 1061
output:
681879967
result:
ok 1 number(s): "681879967"
Test #8:
score: 5
Accepted
time: 12ms
memory: 8264kb
input:
75955 165029 149078 5607
output:
739160804
result:
ok 1 number(s): "739160804"
Test #9:
score: 5
Accepted
time: 4ms
memory: 7756kb
input:
3223 4143 3383 4499
output:
139873119
result:
ok 1 number(s): "139873119"
Test #10:
score: 5
Accepted
time: 15ms
memory: 8584kb
input:
9257 3632 1736 9497
output:
158679485
result:
ok 1 number(s): "158679485"
Test #11:
score: 5
Accepted
time: 35ms
memory: 9372kb
input:
13657 8517 9780 16829
output:
499148359
result:
ok 1 number(s): "499148359"
Test #12:
score: 5
Accepted
time: 17ms
memory: 8416kb
input:
152794 105986 145639 8507
output:
432311896
result:
ok 1 number(s): "432311896"
Test #13:
score: 5
Accepted
time: 2ms
memory: 7532kb
input:
2209 13866 42748 227
output:
576767941
result:
ok 1 number(s): "576767941"
Test #14:
score: 5
Accepted
time: 3ms
memory: 7520kb
input:
14279 7290 25915 2265
output:
173729704
result:
ok 1 number(s): "173729704"
Test #15:
score: 5
Accepted
time: 53ms
memory: 10580kb
input:
24703 6569 26356 26534
output:
108700579
result:
ok 1 number(s): "108700579"
Test #16:
score: 5
Accepted
time: 33ms
memory: 9244kb
input:
187348 185285 76358 13718
output:
425402711
result:
ok 1 number(s): "425402711"
Test #17:
score: 5
Accepted
time: 33ms
memory: 9148kb
input:
189507 133399 143282 13930
output:
608644351
result:
ok 1 number(s): "608644351"
Test #18:
score: 5
Accepted
time: 347ms
memory: 24220kb
input:
102698 162019 98595 137546
output:
388084925
result:
ok 1 number(s): "388084925"
Test #19:
score: 5
Accepted
time: 34ms
memory: 9336kb
input:
152381 90362 151048 15578
output:
413943175
result:
ok 1 number(s): "413943175"
Test #20:
score: 5
Accepted
time: 144ms
memory: 14832kb
input:
34402 49259 74598 64198
output:
733372582
result:
ok 1 number(s): "733372582"