QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#65724#3799. It's Surely Complexdlg#AC ✓3446ms19140kbC++174.5kb2022-12-03 12:41:152022-12-03 12:41:18

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-03 12:41:18]
  • 评测
  • 测评结果:AC
  • 用时:3446ms
  • 内存:19140kb
  • [2022-12-03 12:41:15]
  • 提交

answer

#if not _GLIBCXX_DEBUG
#define NDEBUG 1
#endif
#include<bits/stdc++.h>

namespace FFT{
int constexpr MOD=998244353;
struct M{
	int x;
	void operator+=(M o){ x+=o.x; if(x>=MOD) x-=MOD; }
	void operator-=(M o){ x-=o.x; if(x<0) x+=MOD; }
	void operator*=(M o){ x=int(int64_t(x)*o.x%MOD); }
	friend M operator+(M a,M b){ a+=b; return a; }
	friend M operator-(M a,M b){ a-=b; return a; }
	friend M operator*(M a,M b){ a*=b; return a; }
	M pow(int e)const{
		auto b=*this;
		M re{1};
		while(e){
			if(e&1) re*=b;
			e>>=1;
			b*=b;
		}
		return re;
	}
	M inv()const{
		return pow(MOD-2);
	}
};

void fft(std::vector<M>& a,bool invert){
	M const root{3};


	int n=int(a.size());
	assert((n&(n-1))==0);
	int lg=__builtin_ctz(n);
	for(int i=0;i<n;i++){
		int j=0;
		for(int k=0;k<lg;k++) if(i>>k&1) j|=1<<(lg-k-1);
		if(i<j) std::swap(a[i],a[j]);
	}
	for(int len=2;len<=n;len*=2){
		M wlen=root.pow((MOD-1)/len);
		if(invert) wlen=wlen.inv();
		for(int i=0;i<n;i+=len){
			M w{1};
			for(int j=0;j<len/2;j++){
				auto u=a[i+j];
				auto v=a[i+j+len/2]*w;
				a[i+j]=u+v;
				a[i+j+len/2]=u-v;
				w*=wlen;
			}
		}
	}
	if(invert){
		assert(n<MOD);
		auto mul=M{n}.inv();
		for(auto& x:a) x*=mul;
	}
}

[[nodiscard]] std::vector<M> mul(std::vector<M> a,std::vector<M> b){
	auto rlen=int(a.size()+b.size()-1);
	auto len=1; while(len<rlen) len<<=1;
	a.resize(len); b.resize(len);
	fft(a,false);
	fft(b,false);
	for(int i=0;i<len;i++) a[i]*=b[i];
	fft(a,true);
	return a;
}
}

int p;
struct M{
	int x;
	void operator+=(M o){ x+=o.x; if(x>=p) x-=p; }
	void operator-=(M o){ x-=o.x; if(x<0) x+=p; }
	void operator*=(M o){ x=int(int64_t(x)*o.x%p); }
	friend M operator+(M a,M b){ a+=b; return a; }
	friend M operator-(M a,M b){ a-=b; return a; }
	friend M operator*(M a,M b){ a*=b; return a; }
	M pow(int e)const{
		auto b=*this;
		M re{1};
		while(e){
			if(e&1) re*=b;
			e>>=1;
			b*=b;
		}
		return re;
	}
	M inv()const{
		return pow(p-2);
	}
};

/*
struct C{
	M a,b;
	void operator+(C x){
};
*/
using C=std::complex<M>;

C powC(C b, __int128 e){
	C re{M{1}};
	while(e){
		if(e&1) re*=b;
		e>>=1;
		b*=b;
	}
	return re;
}

int main(){
#if 0
	std::vector<M> a{{1},{2},{3}};
	std::vector<M> b{{4},{5},{6}};
	a=mul(a,b);
	assert(0);
#endif

	std::ios::sync_with_stdio(0);std::cin.tie(0);
	int64_t n;std::cin>>p>>n;
	auto q=(n+1)/p; auto r=int((n+1)%p);

	std::vector<int> p_1_prime_factors;
	{
		int tmp=p-1;
		for(int i=2;tmp!=1;i++) if(tmp%i==0){
			p_1_prime_factors.push_back(i);
			do tmp/=i; while(tmp%i==0);
		}
	}

	M g{1};
	while(std::any_of(begin(p_1_prime_factors),end(p_1_prime_factors),[&](int x){ return g.pow((p-1)/x).x==1; })){
		// then g is not a generator of multiplicative group of Z_p
		g.x+=1;
	}
	assert(g.x<p);


	std::vector<int> lg(p,-1);
	std::vector<M> exp(p-1);
	{
		M g_pow_i{1};
		for(int i=0;i<p-1;i++,g_pow_i*=g){
			lg[g_pow_i.x]=i;
			exp[i]=g_pow_i;
		}
		assert(g_pow_i.x==1);
	}
	lg[0]=0;
	for(auto x:lg) assert(x!=-1);



	auto const solve_=[&](int u,int v)->C{
		assert(0<=u and u<=p);
		assert(0<=v and v<=p);
		C re{M{1},M{}};
		if(u==0 or v==0) return re;

		for(int a=1;a<u;a++) re*=C{M{a},M{}};
		for(int b=1;b<v;b++) re*=C{M{},M{b}};
		if(u<=1 or v<=1) return re;

		M tmp{1};
		for(int a=1;a<u;a++) tmp*=M{a};
		tmp=tmp.pow(v-1);

		re*=tmp;

		std::vector<FFT::M> a_pos(p-1), b_pos(p-1);
		for(int a=1;a<u;a++) a_pos[(p-1-lg[a])%(p-1)].x++;
		for(int b=1;b<v;b++) b_pos[lg[b]].x++;
		auto re_pos=FFT::mul(std::move(a_pos),std::move(b_pos));
		while(int(re_pos.size())>p-1){
			re_pos.end()[-p] += re_pos.back();
			re_pos.pop_back();
		}

		for(int i=0;i<int(re_pos.size());i++)
			re*=powC(C{M{1},M{exp[i]}}, re_pos[i].x);

		return re;
	};
	auto const solve=[&](int u,int v)->C{
		auto re=solve_(u,v);
#if _GLIBCXX_DEBUG
		C re1{M{1}};
		for(int a=0;a<u;a++)
		for(int b=0;b<v;b++)
			if(a%p!=0 or b%p!=0)
				re1*=C{M{a%p}, M{b%p}};
		assert(re.real().x==re1.real().x);
		assert(re.imag().x==re1.imag().x);
#endif
		return re;
	};


	C re{M{1}};
	//if(q%(p-1)){
		re*=powC(solve(p,p), __int128(q)*q) * powC( solve(p,r) * solve(r,p) , q);
	//}
	re*=solve(r,r);

#if _GLIBCXX_DEBUG
	C re1{M{1}};
	for(int a=0;a<=n;a++)
	for(int b=0;b<=n;b++)
		if(a%p!=0 or b%p!=0)
			re1*=C{M{a%p}, M{b%p}};
	assert(re.real().x==re1.real().x);
	assert(re.imag().x==re1.imag().x);
#endif


	std::cout<<
		re.real().x
		<<' '<<
		re.imag().x
		<<'\n';
}

詳細信息

Test #1:

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

input:

2 1

output:

1 1

result:

ok single line: '1 1'

Test #2:

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

input:

2 2

output:

1 1

result:

ok single line: '1 1'

Test #3:

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

input:

2 3

output:

0 0

result:

ok single line: '0 0'

Test #4:

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

input:

2 4

output:

0 0

result:

ok single line: '0 0'

Test #5:

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

input:

3 1

output:

2 1

result:

ok single line: '2 1'

Test #6:

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

input:

3 2

output:

2 0

result:

ok single line: '2 0'

Test #7:

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

input:

3 3

output:

1 0

result:

ok single line: '1 0'

Test #8:

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

input:

3 4

output:

1 1

result:

ok single line: '1 1'

Test #9:

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

input:

3 5

output:

1 0

result:

ok single line: '1 0'

Test #10:

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

input:

3 6

output:

1 0

result:

ok single line: '1 0'

Test #11:

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

input:

5 1

output:

4 1

result:

ok single line: '4 1'

Test #12:

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

input:

5 2

output:

0 0

result:

ok single line: '0 0'

Test #13:

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

input:

5 3

output:

0 0

result:

ok single line: '0 0'

Test #14:

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

input:

5 4

output:

0 0

result:

ok single line: '0 0'

Test #15:

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

input:

5 5

output:

0 0

result:

ok single line: '0 0'

Test #16:

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

input:

5 6

output:

0 0

result:

ok single line: '0 0'

Test #17:

score: 0
Accepted
time: 7ms
memory: 3440kb

input:

5 7

output:

0 0

result:

ok single line: '0 0'

Test #18:

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

input:

5 8

output:

0 0

result:

ok single line: '0 0'

Test #19:

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

input:

5 9

output:

0 0

result:

ok single line: '0 0'

Test #20:

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

input:

5 10

output:

0 0

result:

ok single line: '0 0'

Test #21:

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

input:

7 1

output:

6 1

result:

ok single line: '6 1'

Test #22:

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

input:

7 2

output:

3 0

result:

ok single line: '3 0'

Test #23:

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

input:

7 3

output:

2 5

result:

ok single line: '2 5'

Test #24:

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

input:

7 4

output:

1 0

result:

ok single line: '1 0'

Test #25:

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

input:

7 5

output:

5 2

result:

ok single line: '5 2'

Test #26:

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

input:

7 6

output:

6 0

result:

ok single line: '6 0'

Test #27:

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

input:

7 7

output:

1 0

result:

ok single line: '1 0'

Test #28:

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

input:

7 8

output:

4 4

result:

ok single line: '4 4'

Test #29:

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

input:

7 9

output:

4 0

result:

ok single line: '4 0'

Test #30:

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

input:

7 10

output:

2 2

result:

ok single line: '2 2'

Test #31:

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

input:

7 11

output:

1 0

result:

ok single line: '1 0'

Test #32:

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

input:

7 12

output:

4 4

result:

ok single line: '4 4'

Test #33:

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

input:

7 13

output:

1 0

result:

ok single line: '1 0'

Test #34:

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

input:

7 14

output:

1 0

result:

ok single line: '1 0'

Test #35:

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

input:

2 1000000000000000000

output:

0 0

result:

ok single line: '0 0'

Test #36:

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

input:

3 1000000000000000000

output:

1 1

result:

ok single line: '1 1'

Test #37:

score: 0
Accepted
time: 3437ms
memory: 18992kb

input:

499979 1000000000000000000

output:

486292 0

result:

ok single line: '486292 0'

Test #38:

score: 0
Accepted
time: 3446ms
memory: 19140kb

input:

499973 1000000000000000000

output:

0 0

result:

ok single line: '0 0'

Test #39:

score: 0
Accepted
time: 5ms
memory: 3208kb

input:

2 576460752303423488

output:

0 0

result:

ok single line: '0 0'

Test #40:

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

input:

3 864691128455135232

output:

1 0

result:

ok single line: '1 0'

Test #41:

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

input:

43 41

output:

32 11

result:

ok single line: '32 11'

Test #42:

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

input:

89 646243632056227082

output:

0 0

result:

ok single line: '0 0'

Test #43:

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

input:

79 3818048575756175

output:

61 18

result:

ok single line: '61 18'

Test #44:

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

input:

43 417918461

output:

1 0

result:

ok single line: '1 0'

Test #45:

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

input:

67 9225777529942049

output:

26 0

result:

ok single line: '26 0'

Test #46:

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

input:

29 1894616718601

output:

0 0

result:

ok single line: '0 0'

Test #47:

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

input:

73 24891833259

output:

0 0

result:

ok single line: '0 0'

Test #48:

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

input:

751 45

output:

36 715

result:

ok single line: '36 715'

Test #49:

score: 0
Accepted
time: 7ms
memory: 3412kb

input:

631 870852734504724166

output:

101 0

result:

ok single line: '101 0'

Test #50:

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

input:

479 939006

output:

52 0

result:

ok single line: '52 0'

Test #51:

score: 0
Accepted
time: 5ms
memory: 3432kb

input:

503 223239772447

output:

381 0

result:

ok single line: '381 0'

Test #52:

score: 0
Accepted
time: 5ms
memory: 3432kb

input:

317 73782933513241136

output:

0 0

result:

ok single line: '0 0'

Test #53:

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

input:

577 4897864747011

output:

0 0

result:

ok single line: '0 0'

Test #54:

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

input:

571 7326187013

output:

400 171

result:

ok single line: '400 171'

Test #55:

score: 0
Accepted
time: 54ms
memory: 3548kb

input:

9787 39

output:

953 8834

result:

ok single line: '953 8834'

Test #56:

score: 0
Accepted
time: 28ms
memory: 3340kb

input:

4177 296229723194145403

output:

0 0

result:

ok single line: '0 0'

Test #57:

score: 0
Accepted
time: 31ms
memory: 3332kb

input:

5039 71501150263015946

output:

4425 4425

result:

ok single line: '4425 4425'

Test #58:

score: 0
Accepted
time: 49ms
memory: 3440kb

input:

7027 44142

output:

6075 0

result:

ok single line: '6075 0'

Test #59:

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

input:

1877 5605079

output:

0 0

result:

ok single line: '0 0'

Test #60:

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

input:

2251 187

output:

1847 404

result:

ok single line: '1847 404'

Test #61:

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

input:

3863 699

output:

3850 13

result:

ok single line: '3850 13'

Test #62:

score: 0
Accepted
time: 506ms
memory: 6664kb

input:

92557 64

output:

28440 0

result:

ok single line: '28440 0'

Test #63:

score: 0
Accepted
time: 387ms
memory: 5028kb

input:

62047 410196757795686372

output:

11479 11479

result:

ok single line: '11479 11479'

Test #64:

score: 0
Accepted
time: 586ms
memory: 6200kb

input:

67129 2833304630

output:

0 0

result:

ok single line: '0 0'

Test #65:

score: 0
Accepted
time: 684ms
memory: 6404kb

input:

90793 25188225487855

output:

0 0

result:

ok single line: '0 0'

Test #66:

score: 0
Accepted
time: 282ms
memory: 5028kb

input:

55313 111467

output:

0 0

result:

ok single line: '0 0'

Test #67:

score: 0
Accepted
time: 576ms
memory: 6132kb

input:

69151 718198020401

output:

9621 59530

result:

ok single line: '9621 59530'

Test #68:

score: 0
Accepted
time: 306ms
memory: 4788kb

input:

48571 56301

output:

2628 0

result:

ok single line: '2628 0'

Test #69:

score: 0
Accepted
time: 2213ms
memory: 16324kb

input:

326983 51

output:

39793 287190

result:

ok single line: '39793 287190'

Test #70:

score: 0
Accepted
time: 3209ms
memory: 17460kb

input:

406183 933021611983655873

output:

238788 167395

result:

ok single line: '238788 167395'

Test #71:

score: 0
Accepted
time: 1237ms
memory: 9572kb

input:

152729 7971425537345

output:

0 0

result:

ok single line: '0 0'

Test #72:

score: 0
Accepted
time: 1265ms
memory: 9980kb

input:

183047 6977

output:

141264 41783

result:

ok single line: '141264 41783'

Test #73:

score: 0
Accepted
time: 1251ms
memory: 10468kb

input:

207973 3240

output:

0 0

result:

ok single line: '0 0'

Test #74:

score: 0
Accepted
time: 1175ms
memory: 9348kb

input:

141907 10497585978

output:

141777 141777

result:

ok single line: '141777 141777'

Test #75:

score: 0
Accepted
time: 2309ms
memory: 15704kb

input:

279317 6562

output:

0 0

result:

ok single line: '0 0'