QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#48000#3799. It's Surely Complexbulijiojiodibuliduo#AC ✓4368ms203060kbC++6.0kb2022-09-11 02:55:082022-09-11 02:55:11

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-11 02:55:11]
  • 评测
  • 测评结果:AC
  • 用时:4368ms
  • 内存:203060kb
  • [2022-09-11 02:55:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

void gao(int p) {
	for (int x=0;x<p;x++) {
		ll a=1,b=0;
		for (int y=0;y<p;y++) {
			if (x==0&&y==0) continue;
			ll c=(a*x-b*y)%p,d=(a*y+b*x)%p;
			if (c<0) c+=p;
			a=c; b=d;
			//printf("cccc %d %d %lld %lld\n",x,y,a,b);
		}
		printf("cnm %d %d: ",x,p);
		printf("%lld %lld\n",a,b);
	}
}


typedef pair<ll,ll> cd;
ll p,n;
cd ans(1,0);
cd operator *(cd a,cd b) {
	cd c=mp((a.fi*b.fi-a.se*b.se)%p,(a.fi*b.se+a.se*b.fi)%p);
	if (c.fi<0) c.fi+=p;
	return c;
}
cd powmod(cd a,ll b) {
	cd res(1,0);
	for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}
	return res;
}

cd gao1() {
	return cd{(p%4==1)?0:(p-1),0};
}

cd gao2(ll x) {
	if (x==0) {
		if (p%4==1) return cd{p-1,0};
		else return cd{1,0};
	}
	if (p%4==1) {
		return cd{0,0};
	} else {
		return cd{2*x%p,0};
	}
}
cd gao3(ll y) {
	if (y==0) return cd{p-1,0};
	if (p%4==1) {
		return cd{0,0};
	} else {
		return cd{0,(2*p-2*y)%p};
	}
}


// FFT_MAXN = 2^k
// fft_init() to precalc FFT_MAXN-th roots

typedef double db;
const int FFT_MAXN=1<<21,N=FFT_MAXN+10;
const db pi=acos(-1.);
struct cp{
	db a,b;
	cp operator+(const cp&y)const{return (cp){a+y.a,b+y.b};}
	cp operator-(const cp&y)const{return (cp){a-y.a,b-y.b};}
	cp operator*(const cp&y)const{return (cp){a*y.a-b*y.b,a*y.b+b*y.a};}
	cp operator!()const{return (cp){a,-b};};
}nw[FFT_MAXN+1];int bitrev[FFT_MAXN];
void dft(cp*a,int n,int flag=1){
	int d=0;while((1<<d)*n!=FFT_MAXN)d++;
	rep(i,0,n)if(i<(bitrev[i]>>d))swap(a[i],a[bitrev[i]>>d]);
	for (int l=2;l<=n;l<<=1){
		int del=FFT_MAXN/l*flag;
		for (int i=0;i<n;i+=l){
			cp *le=a+i,*ri=a+i+(l>>1),*w=flag==1?nw:nw+FFT_MAXN;
			rep(k,0,l>>1){
				cp ne=*ri**w;
				*ri=*le-ne,*le=*le+ne;
				le++,ri++,w+=del;
			}
		}
	}
	if(flag!=1)rep(i,0,n)a[i].a/=n,a[i].b/=n;
}
void fft_init(){
	int L=0;while((1<<L)!=FFT_MAXN)L++;
	bitrev[0]=0;rep(i,1,FFT_MAXN)bitrev[i]=bitrev[i>>1]>>1|((i&1)<<(L-1));
	nw[0]=nw[FFT_MAXN]=(cp){1,0};
	rep(i,0,FFT_MAXN+1)nw[i]=(cp){cos(2*pi/FFT_MAXN*i),sin(2*pi/FFT_MAXN*i)};	//very slow
}

void convo(db*a,int n,db*b,int m,db*c){
	static cp f[FFT_MAXN>>1],g[FFT_MAXN>>1],t[FFT_MAXN>>1];
	int N=2;while(N<=n+m)N<<=1;
	rep(i,0,N)
		if(i&1){
			f[i>>1].b=(i<=n)?a[i]:0.0;
			g[i>>1].b=(i<=m)?b[i]:0.0;
		}else{
			f[i>>1].a=(i<=n)?a[i]:0.0;
			g[i>>1].a=(i<=m)?b[i]:0.0;
		}
	dft(f,N>>1);dft(g,N>>1);
	int del=FFT_MAXN/(N>>1);
	cp qua=(cp){0,0.25},one=(cp){1,0},four=(cp){4,0},*w=nw;
	rep(i,0,N>>1){
		int j=i?(N>>1)-i:0;
		t[i]=(four*!(f[j]*g[j])-(!f[j]-f[i])*(!g[j]-g[i])*(one+*w))*qua;
		w+=del;
	}
	dft(t,N>>1,-1);
	rep(i,0,n+m+1)c[i]=(i&1)?t[i>>1].a:t[i>>1].b;
}


int D=10,D2=(1<<D)-1;
VI mul(int *a,int *b,int n,int m){// n<=N, 0<=a[i],b[i]<mo
	static cp f[N],g[N],t[N],r[N];
	int nn=2;while(nn<=n+m)nn<<=1;
	int sz=n+m+1;
	rep(i,0,nn){
		f[i]=(i<=n)?(cp){(db)(a[i]>>D),(db)(a[i]&D2)}:(cp){0,0};
		g[i]=(i<=m)?(cp){(db)(b[i]>>D),(db)(b[i]&D2)}:(cp){0,0};
	}
	swap(n,nn);
	dft(f,n,1);dft(g,n,1);
	rep(i,0,n){
		int j=i?n-i:0;
		t[i]=( (f[i]+!f[j])*(!g[j]-g[i]) + (!f[j]-f[i])*(g[i]+!g[j]) )*(cp){0,0.25};
		r[i]=(!f[j]-f[i])*(!g[j]-g[i])*(cp){-0.25,0} + (cp){0,0.25}*(f[i]+!f[j])*(g[i]+!g[j]);
	}
	dft(t,n,-1); dft(r,n,-1);
	VI tmp(n,0);
	rep(i,0,n)tmp[i]=( (ll(t[i].a+0.5)%p<<D) + ll(r[i].a+0.5) + (ll(r[i].b+0.5)%p<<(2*D)) )%p;
	tmp.resize(sz);
	return tmp;
}

VI mul(VI a,VI b) {
	if (min(SZ(a),SZ(b))<=128) {
		static ll tmp[N];
		rep(i,0,SZ(a)+SZ(b)) tmp[i]=0;
		rep(i,0,SZ(a)) rep(j,0,SZ(b)) tmp[i+j]+=(ll)a[i]*b[j];
		VI c(SZ(a)+SZ(b)-1,0);
		rep(i,0,SZ(c)) c[i]=tmp[i]%p;
	}
	return mul(a.data(),b.data(),SZ(a)-1,SZ(b)-1);
}

VI solve(int l,int r) {
	if (l==r) return VI{l,1};
	else {
		int md=(l+r)>>1;
		return mul(solve(l,md),solve(md+1,r));
	}
}

VI ddqz(VI c) {
	c.resize(p);
	int g=-1;
	for (int gg=2;gg<p;gg++) {
		ll d=1;
		bool isg=1;
		for (int j=1;j<p-1;j++) {
			d=d*gg%p;
			//printf("cnm %d %d %d\n",gg,j,d);
			if (d==1) isg=0;
		}
		if (isg) { g=gg; break; }
	}
	assert(g!=-1);
	VI ff(2*p,0),gg(p,0);
	for (int i=0;i<2*p;i++) {
		ff[i]=powmod(g,(ll)i*(i-1)/2,p);
	}
	rep(i,0,p) {
		gg[i]=c[i]*powmod(g,p-1-(ll)i*(i-1)/2%(p-1),p)%p;
	}
	reverse(all(gg));
	VI rr2=mul(ff,gg);
	VI rr(p,0);
	for (int i=0;i<p-1;i++) {
		rr[powmod(g,i,p)]=rr2[i+p-1]*powmod(g,p-1-(ll)i*(i-1)/2%(p-1),p)%p;
	}
	rr[0]=c[0];
	//printf("C : "); for (auto p:c) printf("%d ",p); puts("");
	//printf("r : "); for (auto p:rr) printf("%d ",p); puts("");
	return rr;
}

int main() {
	fft_init();
	scanf("%lld%lld",&p,&n);
	++n;
	if (p==2) {
		if (n==1) puts("1 0");
		else if (n==2||n==3) puts("1 1");
		else puts("0 0");
		return 0;
	}
	ans=ans*powmod(powmod(gao1(),n/p),n/p);
	cd tmp(1,0);
	for (int i=0;i<n%p;i++) {
		tmp=tmp*gao2(i)*gao3(i);
	}
	ans=ans*powmod(tmp,n/p);
	int m=n%p;
	/*printf("%d\n",m);
	for (int i=0;i<m;i++) for (int j=0;j<m;j++) {
		if (i==0&&j==0) continue;
		ans=ans*cd(i,j);
	}*/
	//puts("cnm");
	if (m>0) {
		VI a=solve(0,m-1);
		/*for (auto x:a) {
			printf("%d ",x);
		}
		puts("");*/
		VI ansr(m+1,0),ansi(m+1,0);
		rep(i,0,m+1) {
			if (i%4==0) ansr[i]=a[i];
			else if (i%4==1) ansi[i]=a[i];
			else if (i%4==2) ansr[i]=(p-a[i])%p;
			else ansi[i]=(p-a[i])%p;
		}
		auto f1=ddqz(ansr),f2=ddqz(ansi);
		rep(i,1,m) ans=ans*cd{f1[i],f2[i]};
		rep(i,1,m) ans=ans*cd{i,0};
	}
	printf("%lld %lld\n",ans.fi,ans.se);
}

詳細信息

Test #1:

score: 100
Accepted
time: 77ms
memory: 44972kb

input:

2 1

output:

1 1

result:

ok single line: '1 1'

Test #2:

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

input:

2 2

output:

1 1

result:

ok single line: '1 1'

Test #3:

score: 0
Accepted
time: 66ms
memory: 45712kb

input:

2 3

output:

0 0

result:

ok single line: '0 0'

Test #4:

score: 0
Accepted
time: 61ms
memory: 45080kb

input:

2 4

output:

0 0

result:

ok single line: '0 0'

Test #5:

score: 0
Accepted
time: 68ms
memory: 47036kb

input:

3 1

output:

2 1

result:

ok single line: '2 1'

Test #6:

score: 0
Accepted
time: 67ms
memory: 46648kb

input:

3 2

output:

2 0

result:

ok single line: '2 0'

Test #7:

score: 0
Accepted
time: 71ms
memory: 45520kb

input:

3 3

output:

1 0

result:

ok single line: '1 0'

Test #8:

score: 0
Accepted
time: 68ms
memory: 45152kb

input:

3 4

output:

1 1

result:

ok single line: '1 1'

Test #9:

score: 0
Accepted
time: 58ms
memory: 46320kb

input:

3 5

output:

1 0

result:

ok single line: '1 0'

Test #10:

score: 0
Accepted
time: 67ms
memory: 45332kb

input:

3 6

output:

1 0

result:

ok single line: '1 0'

Test #11:

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

input:

5 1

output:

4 1

result:

ok single line: '4 1'

Test #12:

score: 0
Accepted
time: 55ms
memory: 45872kb

input:

5 2

output:

0 0

result:

ok single line: '0 0'

Test #13:

score: 0
Accepted
time: 66ms
memory: 45604kb

input:

5 3

output:

0 0

result:

ok single line: '0 0'

Test #14:

score: 0
Accepted
time: 72ms
memory: 46692kb

input:

5 4

output:

0 0

result:

ok single line: '0 0'

Test #15:

score: 0
Accepted
time: 60ms
memory: 47032kb

input:

5 5

output:

0 0

result:

ok single line: '0 0'

Test #16:

score: 0
Accepted
time: 71ms
memory: 46156kb

input:

5 6

output:

0 0

result:

ok single line: '0 0'

Test #17:

score: 0
Accepted
time: 67ms
memory: 46072kb

input:

5 7

output:

0 0

result:

ok single line: '0 0'

Test #18:

score: 0
Accepted
time: 55ms
memory: 46372kb

input:

5 8

output:

0 0

result:

ok single line: '0 0'

Test #19:

score: 0
Accepted
time: 93ms
memory: 46468kb

input:

5 9

output:

0 0

result:

ok single line: '0 0'

Test #20:

score: 0
Accepted
time: 65ms
memory: 45664kb

input:

5 10

output:

0 0

result:

ok single line: '0 0'

Test #21:

score: 0
Accepted
time: 72ms
memory: 46544kb

input:

7 1

output:

6 1

result:

ok single line: '6 1'

Test #22:

score: 0
Accepted
time: 55ms
memory: 45132kb

input:

7 2

output:

3 0

result:

ok single line: '3 0'

Test #23:

score: 0
Accepted
time: 66ms
memory: 46956kb

input:

7 3

output:

2 5

result:

ok single line: '2 5'

Test #24:

score: 0
Accepted
time: 61ms
memory: 45520kb

input:

7 4

output:

1 0

result:

ok single line: '1 0'

Test #25:

score: 0
Accepted
time: 76ms
memory: 45384kb

input:

7 5

output:

5 2

result:

ok single line: '5 2'

Test #26:

score: 0
Accepted
time: 64ms
memory: 46056kb

input:

7 6

output:

6 0

result:

ok single line: '6 0'

Test #27:

score: 0
Accepted
time: 58ms
memory: 46280kb

input:

7 7

output:

1 0

result:

ok single line: '1 0'

Test #28:

score: 0
Accepted
time: 59ms
memory: 45524kb

input:

7 8

output:

4 4

result:

ok single line: '4 4'

Test #29:

score: 0
Accepted
time: 61ms
memory: 46024kb

input:

7 9

output:

4 0

result:

ok single line: '4 0'

Test #30:

score: 0
Accepted
time: 70ms
memory: 45248kb

input:

7 10

output:

2 2

result:

ok single line: '2 2'

Test #31:

score: 0
Accepted
time: 101ms
memory: 46612kb

input:

7 11

output:

1 0

result:

ok single line: '1 0'

Test #32:

score: 0
Accepted
time: 101ms
memory: 45524kb

input:

7 12

output:

4 4

result:

ok single line: '4 4'

Test #33:

score: 0
Accepted
time: 60ms
memory: 45740kb

input:

7 13

output:

1 0

result:

ok single line: '1 0'

Test #34:

score: 0
Accepted
time: 66ms
memory: 46820kb

input:

7 14

output:

1 0

result:

ok single line: '1 0'

Test #35:

score: 0
Accepted
time: 63ms
memory: 45028kb

input:

2 1000000000000000000

output:

0 0

result:

ok single line: '0 0'

Test #36:

score: 0
Accepted
time: 68ms
memory: 46892kb

input:

3 1000000000000000000

output:

1 1

result:

ok single line: '1 1'

Test #37:

score: 0
Accepted
time: 4043ms
memory: 201040kb

input:

499979 1000000000000000000

output:

486292 0

result:

ok single line: '486292 0'

Test #38:

score: 0
Accepted
time: 4368ms
memory: 203060kb

input:

499973 1000000000000000000

output:

0 0

result:

ok single line: '0 0'

Test #39:

score: 0
Accepted
time: 63ms
memory: 44980kb

input:

2 576460752303423488

output:

0 0

result:

ok single line: '0 0'

Test #40:

score: 0
Accepted
time: 77ms
memory: 46600kb

input:

3 864691128455135232

output:

1 0

result:

ok single line: '1 0'

Test #41:

score: 0
Accepted
time: 53ms
memory: 45516kb

input:

43 41

output:

32 11

result:

ok single line: '32 11'

Test #42:

score: 0
Accepted
time: 75ms
memory: 45980kb

input:

89 646243632056227082

output:

0 0

result:

ok single line: '0 0'

Test #43:

score: 0
Accepted
time: 52ms
memory: 45344kb

input:

79 3818048575756175

output:

61 18

result:

ok single line: '61 18'

Test #44:

score: 0
Accepted
time: 65ms
memory: 46528kb

input:

43 417918461

output:

1 0

result:

ok single line: '1 0'

Test #45:

score: 0
Accepted
time: 65ms
memory: 45828kb

input:

67 9225777529942049

output:

26 0

result:

ok single line: '26 0'

Test #46:

score: 0
Accepted
time: 69ms
memory: 45668kb

input:

29 1894616718601

output:

0 0

result:

ok single line: '0 0'

Test #47:

score: 0
Accepted
time: 63ms
memory: 46160kb

input:

73 24891833259

output:

0 0

result:

ok single line: '0 0'

Test #48:

score: 0
Accepted
time: 76ms
memory: 45796kb

input:

751 45

output:

36 715

result:

ok single line: '36 715'

Test #49:

score: 0
Accepted
time: 58ms
memory: 46460kb

input:

631 870852734504724166

output:

101 0

result:

ok single line: '101 0'

Test #50:

score: 0
Accepted
time: 64ms
memory: 45504kb

input:

479 939006

output:

52 0

result:

ok single line: '52 0'

Test #51:

score: 0
Accepted
time: 65ms
memory: 46788kb

input:

503 223239772447

output:

381 0

result:

ok single line: '381 0'

Test #52:

score: 0
Accepted
time: 57ms
memory: 46628kb

input:

317 73782933513241136

output:

0 0

result:

ok single line: '0 0'

Test #53:

score: 0
Accepted
time: 76ms
memory: 45712kb

input:

577 4897864747011

output:

0 0

result:

ok single line: '0 0'

Test #54:

score: 0
Accepted
time: 73ms
memory: 46272kb

input:

571 7326187013

output:

400 171

result:

ok single line: '400 171'

Test #55:

score: 0
Accepted
time: 113ms
memory: 48692kb

input:

9787 39

output:

953 8834

result:

ok single line: '953 8834'

Test #56:

score: 0
Accepted
time: 69ms
memory: 47192kb

input:

4177 296229723194145403

output:

0 0

result:

ok single line: '0 0'

Test #57:

score: 0
Accepted
time: 77ms
memory: 46308kb

input:

5039 71501150263015946

output:

4425 4425

result:

ok single line: '4425 4425'

Test #58:

score: 0
Accepted
time: 99ms
memory: 47876kb

input:

7027 44142

output:

6075 0

result:

ok single line: '6075 0'

Test #59:

score: 0
Accepted
time: 65ms
memory: 45992kb

input:

1877 5605079

output:

0 0

result:

ok single line: '0 0'

Test #60:

score: 0
Accepted
time: 81ms
memory: 46264kb

input:

2251 187

output:

1847 404

result:

ok single line: '1847 404'

Test #61:

score: 0
Accepted
time: 81ms
memory: 46856kb

input:

3863 699

output:

3850 13

result:

ok single line: '3850 13'

Test #62:

score: 0
Accepted
time: 733ms
memory: 82336kb

input:

92557 64

output:

28440 0

result:

ok single line: '28440 0'

Test #63:

score: 0
Accepted
time: 455ms
memory: 64252kb

input:

62047 410196757795686372

output:

11479 11479

result:

ok single line: '11479 11479'

Test #64:

score: 0
Accepted
time: 593ms
memory: 66200kb

input:

67129 2833304630

output:

0 0

result:

ok single line: '0 0'

Test #65:

score: 0
Accepted
time: 1034ms
memory: 83804kb

input:

90793 25188225487855

output:

0 0

result:

ok single line: '0 0'

Test #66:

score: 0
Accepted
time: 422ms
memory: 63928kb

input:

55313 111467

output:

0 0

result:

ok single line: '0 0'

Test #67:

score: 0
Accepted
time: 545ms
memory: 66296kb

input:

69151 718198020401

output:

9621 59530

result:

ok single line: '9621 59530'

Test #68:

score: 0
Accepted
time: 408ms
memory: 65912kb

input:

48571 56301

output:

2628 0

result:

ok single line: '2628 0'

Test #69:

score: 0
Accepted
time: 2092ms
memory: 126024kb

input:

326983 51

output:

39793 287190

result:

ok single line: '39793 287190'

Test #70:

score: 0
Accepted
time: 4248ms
memory: 202156kb

input:

406183 933021611983655873

output:

238788 167395

result:

ok single line: '238788 167395'

Test #71:

score: 0
Accepted
time: 1110ms
memory: 85984kb

input:

152729 7971425537345

output:

0 0

result:

ok single line: '0 0'

Test #72:

score: 0
Accepted
time: 1508ms
memory: 120292kb

input:

183047 6977

output:

141264 41783

result:

ok single line: '141264 41783'

Test #73:

score: 0
Accepted
time: 1602ms
memory: 122772kb

input:

207973 3240

output:

0 0

result:

ok single line: '0 0'

Test #74:

score: 0
Accepted
time: 935ms
memory: 85224kb

input:

141907 10497585978

output:

141777 141777

result:

ok single line: '141777 141777'

Test #75:

score: 0
Accepted
time: 1875ms
memory: 124344kb

input:

279317 6562

output:

0 0

result:

ok single line: '0 0'