QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#66080 | #1457. FFT Algorithm | xiaoyaowudi | AC ✓ | 9ms | 3392kb | C++14 | 2.1kb | 2022-12-06 12:10:32 | 2022-12-06 12:10:36 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <chrono>
#include <random>
#include <vector>
using ll=long long;
std::mt19937_64 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());
using L=__int128;
ll fp(ll a,ll b,ll c){ll ans=1,off=a;while(b){if(b&1) ans=(L)ans*off%c;off=(L)off*off%c;b>>=1;}return ans;}
constexpr ll tn[]={2,325,9375,28178,450775,9780504,1795265022};
bool check(ll a,ll p)
{
ll d(p-1),w(fp(a,d,p));
if(w!=1) return 1;
while((d&1)^1)
{
if((w=fp(a,(d>>=1),p))==p-1) return 0;
else if(w!=1) return 1;
}
return 0;
}
bool miller_rabin(ll a)
{
for(ll c:tn) if(a==c) return a==2;
for(ll c:tn)
{
ll b(c%a);
if(b==0) continue;
if(check(b,a)) return false;
}
return true;
}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll pollard_rho(ll x)
{
ll s=0,t=0,c=1LL*rnd()%(x-1)+1,j=0,k=1,tmp=1;
for(k=1;;k<<=1,s=t,tmp=1)
{
for(j=1;j<=k;++j)
{
t=(L(t)*t+c)%x;
tmp=(L)tmp*std::abs(t-s)%x;
if((j%127)==0)
{
ll d=gcd(tmp,x);
if(d>1) return d;
}
}
ll d=gcd(tmp,x);
if(d>1) return d;
}
return 1;
}
void fact(ll n,std::vector<ll> &ps)
{
if(n<2) return;
if(miller_rabin(n)){ps.emplace_back(n);return;}
ll p=n;
while(p>=n) p=pollard_rho(n);
while((n%p)==0) n/=p;
fact(n,ps),fact(p,ps);
}
bool check(ll m,const std::vector<ll> &ps)
{
if(m%2) return ps.size()==1;
else return ((m/2)%2)==1 && ps.size()==2;
}
int main()
{
ll m;int k;
std::cin>>m>>k;std::vector<ll> ps;fact(m,ps);
ll phi(m);std::sort(ps.begin(),ps.end());ps.resize(std::unique(ps.begin(),ps.end())-ps.begin());
for(ll v:ps) phi=phi/v*(v-1);
if(phi%(1<<k))
{
std::cout<<"-1\n";
return 0;
}
if(phi==(1<<k))
{
if(!check(m,ps))
{
std::cout<<"-1\n";
return 0;
}
}
ps.clear();fact(phi,ps);
ll t(phi),q(1);while((t%2)==0) t/=2,q*=2;
ll t0(t*(1<<(k-1)));
ll w(1);
// std::cout<<phi<<" "<<t<<std::endl;
while(fp(w,t0,m)==1 || gcd(w,m)!=1) ++w;
// std::cout<<w<<" "<<fp(w,t0,m)<<"\n";
w=fp(w,t,m);
// std::cout<<fp(w,q,m)<<std::endl;
while(fp(w,(1<<k),m)!=1) w=L(w)*w%m;
std::cout<<w<<std::endl;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3244kb
input:
998244353 23
output:
15311432
result:
ok OK valid solution
Test #2:
score: 0
Accepted
time: 2ms
memory: 3320kb
input:
1048576 15
output:
6561
result:
ok OK valid solution
Test #3:
score: 0
Accepted
time: 0ms
memory: 3300kb
input:
3 23
output:
-1
result:
ok OK no solution
Test #4:
score: 0
Accepted
time: 0ms
memory: 3388kb
input:
997261313 15
output:
365254143
result:
ok OK valid solution
Test #5:
score: 0
Accepted
time: 2ms
memory: 3288kb
input:
971604167 15
output:
693649921
result:
ok OK valid solution
Test #6:
score: 0
Accepted
time: 2ms
memory: 3220kb
input:
561972416 15
output:
-1
result:
ok OK no solution
Test #7:
score: 0
Accepted
time: 3ms
memory: 3216kb
input:
998572034 15
output:
550870509
result:
ok OK valid solution
Test #8:
score: 0
Accepted
time: 2ms
memory: 3280kb
input:
127941919 15
output:
-1
result:
ok OK no solution
Test #9:
score: 0
Accepted
time: 0ms
memory: 3232kb
input:
945553409 15
output:
-1
result:
ok OK no solution
Test #10:
score: 0
Accepted
time: 2ms
memory: 3228kb
input:
998244353 23
output:
15311432
result:
ok OK valid solution
Test #11:
score: 0
Accepted
time: 2ms
memory: 3252kb
input:
596468390 23
output:
-1
result:
ok OK no solution
Test #12:
score: 0
Accepted
time: 2ms
memory: 3240kb
input:
813335261 23
output:
-1
result:
ok OK no solution
Test #13:
score: 0
Accepted
time: 2ms
memory: 3296kb
input:
293185311 23
output:
-1
result:
ok OK no solution
Test #14:
score: 0
Accepted
time: 2ms
memory: 3292kb
input:
431142105 23
output:
-1
result:
ok OK no solution
Test #15:
score: 0
Accepted
time: 2ms
memory: 3392kb
input:
545259521 23
output:
-1
result:
ok OK no solution
Test #16:
score: 0
Accepted
time: 2ms
memory: 3256kb
input:
999999998361601 15
output:
724329739575680
result:
ok OK valid solution
Test #17:
score: 0
Accepted
time: 0ms
memory: 3252kb
input:
999956339174017 15
output:
115295294176587
result:
ok OK valid solution
Test #18:
score: 0
Accepted
time: 3ms
memory: 3216kb
input:
999999934158041 15
output:
85250316165705
result:
ok OK valid solution
Test #19:
score: 0
Accepted
time: 0ms
memory: 3240kb
input:
999870494845507 15
output:
824076331860218
result:
ok OK valid solution
Test #20:
score: 0
Accepted
time: 3ms
memory: 3248kb
input:
987500017287169 15
output:
728983403990856
result:
ok OK valid solution
Test #21:
score: 0
Accepted
time: 3ms
memory: 3364kb
input:
999869489774593 15
output:
-1
result:
ok OK no solution
Test #22:
score: 0
Accepted
time: 2ms
memory: 3276kb
input:
999999643058177 23
output:
375603868068691
result:
ok OK valid solution
Test #23:
score: 0
Accepted
time: 2ms
memory: 3296kb
input:
999029131455133 23
output:
531641924642800
result:
ok OK valid solution
Test #24:
score: 0
Accepted
time: 0ms
memory: 3284kb
input:
980573759207039 23
output:
-1
result:
ok OK no solution
Test #25:
score: 0
Accepted
time: 0ms
memory: 3260kb
input:
997380719651467 23
output:
801535115907997
result:
ok OK valid solution
Test #26:
score: 0
Accepted
time: 2ms
memory: 3232kb
input:
673383102728611 23
output:
-1
result:
ok OK no solution
Test #27:
score: 0
Accepted
time: 3ms
memory: 3300kb
input:
997055929516033 23
output:
-1
result:
ok OK no solution
Test #28:
score: 0
Accepted
time: 2ms
memory: 3320kb
input:
3999999999999705089 15
output:
1058625960373983012
result:
ok OK valid solution
Test #29:
score: 0
Accepted
time: 3ms
memory: 3224kb
input:
3999993153100322407 15
output:
802866453965245724
result:
ok OK valid solution
Test #30:
score: 0
Accepted
time: 2ms
memory: 3368kb
input:
3999999979069450717 15
output:
1951361416389507515
result:
ok OK valid solution
Test #31:
score: 0
Accepted
time: 0ms
memory: 3296kb
input:
3999991341703927187 15
output:
3809043281050504927
result:
ok OK valid solution
Test #32:
score: 0
Accepted
time: 2ms
memory: 3240kb
input:
1641566238888296449 15
output:
391907170255173830
result:
ok OK valid solution
Test #33:
score: 0
Accepted
time: 7ms
memory: 3368kb
input:
3999987196009414657 15
output:
-1
result:
ok OK no solution
Test #34:
score: 0
Accepted
time: 2ms
memory: 3392kb
input:
3999999999713738753 23
output:
2541755635409224047
result:
ok OK valid solution
Test #35:
score: 0
Accepted
time: 2ms
memory: 3300kb
input:
3999977144498948549 23
output:
858148666025926742
result:
ok OK valid solution
Test #36:
score: 0
Accepted
time: 6ms
memory: 3372kb
input:
3999999983264930527 23
output:
3012279943069811843
result:
ok OK valid solution
Test #37:
score: 0
Accepted
time: 2ms
memory: 3324kb
input:
3999867163212679501 23
output:
1272286299560227459
result:
ok OK valid solution
Test #38:
score: 0
Accepted
time: 9ms
memory: 3220kb
input:
2928465661120217089 23
output:
603245591959997402
result:
ok OK valid solution
Test #39:
score: 0
Accepted
time: 8ms
memory: 3288kb
input:
3999790589313810433 23
output:
-1
result:
ok OK no solution
Test #40:
score: 0
Accepted
time: 0ms
memory: 3296kb
input:
65536 15
output:
-1
result:
ok OK no solution
Test #41:
score: 0
Accepted
time: 3ms
memory: 3236kb
input:
131072 15
output:
3
result:
ok OK valid solution
Test #42:
score: 0
Accepted
time: 3ms
memory: 3364kb
input:
2305843009213693952 15
output:
1049127606944792577
result:
ok OK valid solution
Test #43:
score: 0
Accepted
time: 2ms
memory: 3300kb
input:
16777216 23
output:
-1
result:
ok OK no solution
Test #44:
score: 0
Accepted
time: 2ms
memory: 3392kb
input:
33554432 23
output:
3
result:
ok OK valid solution
Test #45:
score: 0
Accepted
time: 2ms
memory: 3216kb
input:
2305843009213693952 23
output:
661623700310720513
result:
ok OK valid solution