QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#66082#1457. FFT AlgorithmeyiigjknAC ✓30ms3420kbC++141.7kb2022-12-06 12:25:562022-12-06 12:25:58

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-06 12:25:58]
  • 评测
  • 测评结果:AC
  • 用时:30ms
  • 内存:3420kb
  • [2022-12-06 12:25:56]
  • 提交

answer

# include <bits/stdc++.h>
using namespace std;
using ll=long long;
using lll=__int128;
constexpr int prime[8]={2,3,5,7,11,13,61,24251};
mt19937_64 rnd;
template<typename T>
inline void clear(T &x){T().swap(x);}
ll power(ll a,ll b,ll c)
{
	ll ans=1;
	for(;b;b>>=1,a=(lll)a*a%c)
		if(b&1) ans=(lll)ans*a%c;
	return ans;
}
bool MR(ll a,ll n)
{
	if(a%n==0 || power(a,n-1,n)!=1) return true;
	ll t=n-1,m=0;
	while(!(t&1)) t>>=1,m++;
	ll x=power(a,t,n);
	for(int i=0;i<m;i++)
	{
		ll y=(lll)x*x%n;
		if(y==1 && x!=1 && x!=n-1) return true;
		x=y;
	}
	return x!=1;
}
bool ispri(ll n)
{
	if(n==1) return false;
	for(int i=0;i<8;i++)
		if(n==prime[i]) return true;
		else if(MR(prime[i],n)) return false;
	return true;
}
ll PR(ll n)
{
	ll x=rnd()%(n-1)+1,c=rnd()%(n-1)+1,y=x,s=1;
	for(ll i=1,j=2,g;;i++)
	{
		x=((lll)x*x+c)%n;s=(lll)s*abs(x-y)%n;
		if(!s) return n;
		if(i==j)
		{
			if((g=__gcd(s,n))>1) return g;
			y=x;j<<=1;
		}
	}
}
void slv(ll n,vector<ll> &vec)
{
	if(n==1) return;
	if(ispri(n)) return vec.push_back(n),void();
	ll x;
	while((x=PR(n))>=n);
	slv(x,vec);slv(n/x,vec);
}
int main()
{
	int K;
	ll n,w=1;
	vector<ll> d;
	cin>>n>>K;
	slv(n,d);
	sort(d.begin(),d.end());
	d.resize(unique(d.begin(),d.end())-d.begin());
	ll phi=n;
	for(ll i:d) phi=phi/i*(i-1);
	if(phi%(1<<K)) return puts("-1"),0;
	clear(d);slv(phi,d);
	auto ord=[&](ll w)
	{
		ll ans=phi;
		for(ll i:d)
			while(ans%i==0 && power(w,ans/i,n)==1)
				ans/=i;
		return ans;
	};
	ll m=1<<K-1;
	for(ll i:d) if(i>2) m*=i;
	auto chk=[&](ll w){return __gcd(w,m)==1 && power(w,m,n)>1;};
	for(int c=0;!chk(w) && c<100000;c++) w++;
	if(!chk(w)) return puts("-1"),0;
	cout<<power(w,ord(w)>>K,n)<<endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3164kb

input:

998244353 23

output:

15311432

result:

ok OK valid solution

Test #2:

score: 0
Accepted
time: 0ms
memory: 3360kb

input:

1048576 15

output:

6561

result:

ok OK valid solution

Test #3:

score: 0
Accepted
time: 2ms
memory: 3224kb

input:

3 23

output:

-1

result:

ok OK no solution

Test #4:

score: 0
Accepted
time: 1ms
memory: 3236kb

input:

997261313 15

output:

365254143

result:

ok OK valid solution

Test #5:

score: 0
Accepted
time: 2ms
memory: 3252kb

input:

971604167 15

output:

323647233

result:

ok OK valid solution

Test #6:

score: 0
Accepted
time: 2ms
memory: 3416kb

input:

561972416 15

output:

-1

result:

ok OK no solution

Test #7:

score: 0
Accepted
time: 2ms
memory: 3240kb

input:

998572034 15

output:

739803799

result:

ok OK valid solution

Test #8:

score: 0
Accepted
time: 2ms
memory: 3220kb

input:

127941919 15

output:

-1

result:

ok OK no solution

Test #9:

score: 0
Accepted
time: 2ms
memory: 3292kb

input:

945553409 15

output:

-1

result:

ok OK no solution

Test #10:

score: 0
Accepted
time: 2ms
memory: 3216kb

input:

998244353 23

output:

15311432

result:

ok OK valid solution

Test #11:

score: 0
Accepted
time: 0ms
memory: 3360kb

input:

596468390 23

output:

-1

result:

ok OK no solution

Test #12:

score: 0
Accepted
time: 2ms
memory: 3224kb

input:

813335261 23

output:

-1

result:

ok OK no solution

Test #13:

score: 0
Accepted
time: 0ms
memory: 3216kb

input:

293185311 23

output:

-1

result:

ok OK no solution

Test #14:

score: 0
Accepted
time: 2ms
memory: 3416kb

input:

431142105 23

output:

-1

result:

ok OK no solution

Test #15:

score: 0
Accepted
time: 2ms
memory: 3228kb

input:

545259521 23

output:

-1

result:

ok OK no solution

Test #16:

score: 0
Accepted
time: 0ms
memory: 3240kb

input:

999999998361601 15

output:

596739826094319

result:

ok OK valid solution

Test #17:

score: 0
Accepted
time: 3ms
memory: 3388kb

input:

999956339174017 15

output:

148138122890580

result:

ok OK valid solution

Test #18:

score: 0
Accepted
time: 3ms
memory: 3360kb

input:

999999934158041 15

output:

478047879184688

result:

ok OK valid solution

Test #19:

score: 0
Accepted
time: 2ms
memory: 3360kb

input:

999870494845507 15

output:

201253600221856

result:

ok OK valid solution

Test #20:

score: 0
Accepted
time: 2ms
memory: 3388kb

input:

987500017287169 15

output:

728983403990856

result:

ok OK valid solution

Test #21:

score: 0
Accepted
time: 1ms
memory: 3220kb

input:

999869489774593 15

output:

-1

result:

ok OK no solution

Test #22:

score: 0
Accepted
time: 2ms
memory: 3356kb

input:

999999643058177 23

output:

375603868068691

result:

ok OK valid solution

Test #23:

score: 0
Accepted
time: 2ms
memory: 3196kb

input:

999029131455133 23

output:

597546885368399

result:

ok OK valid solution

Test #24:

score: 0
Accepted
time: 2ms
memory: 3192kb

input:

980573759207039 23

output:

-1

result:

ok OK no solution

Test #25:

score: 0
Accepted
time: 2ms
memory: 3244kb

input:

997380719651467 23

output:

70289329287912

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: 3236kb

input:

997055929516033 23

output:

-1

result:

ok OK no solution

Test #28:

score: 0
Accepted
time: 2ms
memory: 3216kb

input:

3999999999999705089 15

output:

1058625960373983012

result:

ok OK valid solution

Test #29:

score: 0
Accepted
time: 3ms
memory: 3240kb

input:

3999993153100322407 15

output:

3841483656333778778

result:

ok OK valid solution

Test #30:

score: 0
Accepted
time: 16ms
memory: 3256kb

input:

3999999979069450717 15

output:

3418373025234552474

result:

ok OK valid solution

Test #31:

score: 0
Accepted
time: 3ms
memory: 3228kb

input:

3999991341703927187 15

output:

2042406726365818435

result:

ok OK valid solution

Test #32:

score: 0
Accepted
time: 1ms
memory: 3164kb

input:

1641566238888296449 15

output:

30564347160431184

result:

ok OK valid solution

Test #33:

score: 0
Accepted
time: 6ms
memory: 3372kb

input:

3999987196009414657 15

output:

-1

result:

ok OK no solution

Test #34:

score: 0
Accepted
time: 2ms
memory: 3196kb

input:

3999999999713738753 23

output:

2541755635409224047

result:

ok OK valid solution

Test #35:

score: 0
Accepted
time: 0ms
memory: 3228kb

input:

3999977144498948549 23

output:

858148666025926742

result:

ok OK valid solution

Test #36:

score: 0
Accepted
time: 4ms
memory: 3160kb

input:

3999999983264930527 23

output:

672962409053550820

result:

ok OK valid solution

Test #37:

score: 0
Accepted
time: 1ms
memory: 3408kb

input:

3999867163212679501 23

output:

1036476330667375565

result:

ok OK valid solution

Test #38:

score: 0
Accepted
time: 15ms
memory: 3320kb

input:

2928465661120217089 23

output:

928110756729021249

result:

ok OK valid solution

Test #39:

score: 0
Accepted
time: 3ms
memory: 3252kb

input:

3999790589313810433 23

output:

-1

result:

ok OK no solution

Test #40:

score: 0
Accepted
time: 23ms
memory: 3376kb

input:

65536 15

output:

-1

result:

ok OK no solution

Test #41:

score: 0
Accepted
time: 2ms
memory: 3408kb

input:

131072 15

output:

3

result:

ok OK valid solution

Test #42:

score: 0
Accepted
time: 2ms
memory: 3320kb

input:

2305843009213693952 15

output:

1049127606944792577

result:

ok OK valid solution

Test #43:

score: 0
Accepted
time: 30ms
memory: 3420kb

input:

16777216 23

output:

-1

result:

ok OK no solution

Test #44:

score: 0
Accepted
time: 2ms
memory: 3316kb

input:

33554432 23

output:

3

result:

ok OK valid solution

Test #45:

score: 0
Accepted
time: 0ms
memory: 3408kb

input:

2305843009213693952 23

output:

661623700310720513

result:

ok OK valid solution