UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#198454#2973. 排列计数Eaoci10062803ms72400kbC++113.0kb2023-11-26 11:27:542023-11-26 12:04:08

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;

namespace Conv{

typedef long long ll;
const int mod=998244353,N=1050000,g=3,invg=(mod+1)/3;
int wk[N+5],ta[N+5],tb[N+5];
int Power(int x,int y){
	int r=1;
	while(y){
		if(y&1)r=1ll*r*x%mod;
		x=1ll*x*x%mod,y>>=1;
	}
	return r;
}
void DFT(int *a,int n){
	for(int i=n>>1;i;i>>=1){
		int w=Power(g,(mod-1)/(i<<1));
		wk[0]=1;
		for(int j=1;j<i;j++)wk[j]=1ll*wk[j-1]*w%mod;
		for(int j=0;j<n;j+=(i<<1)){
			for(int k=0;k<i;k++){
				int x=a[j+k],y=a[i+j+k],z=x;
				x+=y,(x>=mod&&(x-=mod)),a[j+k]=x;
				z-=y,(z<0&&(z+=mod)),a[i+j+k]=1ll*z*wk[k]%mod;
			}
		}
	}
}
void IDFT(int *a,int n){
	for(int i=1;i<n;i<<=1){
		int w=Power(invg,(mod-1)/(i<<1));
		wk[0]=1;
		for(int j=1;j<i;j++)wk[j]=1ll*wk[j-1]*w%mod;
		for(int j=0;j<n;j+=(i<<1)){
			for(int k=0;k<i;k++){
				int x=a[j+k],y=1ll*a[i+j+k]*wk[k]%mod,z=x;
				x+=y,(x>=mod&&(x-=mod)),a[j+k]=x;
				z-=y,(z<0&&(z+=mod)),a[i+j+k]=z;
			}
		}
	}
	for(int i=0,inv=Power(n,mod-2);i<n;i++)a[i]=1ll*a[i]*inv%mod;
}
vector<int> conv(vector<int> A,vector<int> B){
	int sa=A.size(),sb=B.size();
	vector<int> ret(sa+sb-1);
	int len=1;
	while(len<ret.size())len<<=1;
	for(int i=0;i<len;i++)ta[i]=tb[i]=0;
	for(int i=0;i<sa;i++)ta[i]=A[i];
	for(int i=0;i<sb;i++)tb[i]=B[i];
	DFT(ta,len),DFT(tb,len);
	for(int i=0;i<len;i++)ta[i]=1ll*ta[i]*tb[i]%mod;
	IDFT(ta,len);
	for(int i=0;i<ret.size();i++)ret[i]=ta[i];
	return ret;
}
}
int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
const int N=1000010,mod=998244353;
int n,m,c[N],d[N],jc[N],iv[N],inv[N],h[N],a[N];
vector<int>f,g;
int ksm(int p,int k){
	int x=1;
	while(k){
		if(k&1)x=x*p%mod;
		p=p*p%mod;
		k>>=1;
	}
	return x;
}
int C(int n,int m){
	if(n<m)return 0;
	return jc[n]*inv[m]%mod*inv[n-m]%mod;
} 
signed main(){
	//ʹÓ÷½·¨£ºConv::conv(A[],B[]) ·µ»Ø A*B£¬ÆäÖÐ A[]£¬B[]£¬·µ»ØÖµµÄϵÊý¾ù´ÓµÍλµ½¸ßλÅÅÁÐ
	//ÒªÇó A ºÍ B µÄ´ÎÊýÖ®ºÍ²»³¬¹ý 1040000£¬ÇÒ A[i],B[i] ÊôÓÚ [0,998244352]
	n=read(),jc[0]=iv[0]=1;
	for(int i=1;i<=n;i++){
		c[read()]++,h[i]=1;
	}
	for(int i=1;i<=n;i++){
		d[c[i]]++;
		jc[i]=jc[i-1]*i%mod;
	} 
	inv[n]=ksm(jc[n],mod-2);
	for(int i=n;i;i--){
		inv[i-1]=inv[i]*i%mod;
		iv[i]=jc[i-1]*inv[i]%mod;
	}
	for(int i=1;i<=n;i++){
		if(d[i]){
			for(int j=1;j<=n;j++){
				h[j]=h[j]*ksm(C(j,i),d[i])%mod;
			}
		}
	}
	int fl=1;
	for(int i=0;i<=n;i++){
		f.push_back(h[i]*inv[i]%mod);
		g.push_back(fl*inv[i]%mod);
		fl=mod-fl;
	}
	f=Conv::conv(f,g);
	fl=1;
	for(int i=0;i<=n;i++){
		a[i]=f[i]*jc[i]%mod*jc[n-i]%mod*fl%mod;
		fl=mod-fl;
	}
	f.clear(),g.clear();
	for(int i=0;i<=n;i++){
		f.push_back(a[i]);
		g.push_back(inv[i]);
	}
	f=Conv::conv(f,g);
	fl=1;
	for(int i=0;i<=n;i++){
		a[i]=f[i]*inv[n-i]%mod*fl%mod;
		fl=mod-fl;
	}
	for(int i=n;i;i--){
		cout<<a[i]<<" ";
	}
}

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 3ms
memory: 1248kb

input:

1
1

output:

1 

result:

ok 1 number(s): "1"

Test #2:

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

input:

2
2 2

output:

1 0 

result:

ok 2 number(s): "1 0"

Test #3:

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

input:

2
1 2

output:

1 1 

result:

ok 2 number(s): "1 1"

Test #4:

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

input:

2
1 1

output:

1 0 

result:

ok 2 number(s): "1 0"

Test #5:

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

input:

3
2 3 2

output:

1 2 0 

result:

ok 3 number(s): "1 2 0"

Test #6:

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

input:

10
10 5 8 4 3 3 8 2 3 1

output:

1 373 10411 65375 126695 82259 16557 729 0 0 

result:

ok 10 numbers

Test #7:

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

input:

10
4 4 2 8 6 8 7 2 1 8

output:

1 277 6607 36755 63785 36989 6543 243 0 0 

result:

ok 10 numbers

Subtask #2:

score: 5
Accepted

Test #8:

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

input:

100
16 41 2 14 92 72 81 99 16 17 48 57 18 51 7 5 81 12 100 65 42 53 69 71 27 87 30 3 64 61 86 45 14 ...

output:

1 493656088 330049578 519168467 491719437 924286979 683492517 369393853 738859200 499576428 37907333...

result:

ok 100 numbers

Test #9:

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

input:

100
27 46 46 65 14 46 16 16 16 7 14 7 82 14 46 37 89 7 82 46 7 82 65 7 7 44 46 58 37 7 46 87 16 14 2...

output:

1 700352636 847591972 75103663 390926195 226855132 210546576 28157717 435965818 548785536 975908354 ...

result:

ok 100 numbers

Test #10:

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

input:

100
39 51 4 14 60 54 65 71 68 63 51 32 51 71 14 51 50 85 14 51 14 68 60 32 51 56 78 44 14 50 85 44 7...

output:

1 420258136 685651705 23179696 118080778 43512276 63703851 881349561 219761296 235958861 892078766 2...

result:

ok 100 numbers

Subtask #3:

score: 25
Accepted

Test #11:

score: 25
Accepted
time: 3ms
memory: 2044kb

input:

5000
1803 1803 1803 1803 1803 1803 1803 1803 1803 1803 1803 1803 1803 1803 1803 1803 1803 1803 1803 ...

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 5000 numbers

Test #12:

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

input:

5000
530 4016 4795 2246 663 2360 1026 3880 1852 4010 1961 1591 4168 1759 4610 997 1658 2122 4094 413...

output:

1 856959048 97689916 558976748 108202271 770788588 201836920 259465721 535131549 60179743 724257554 ...

result:

ok 5000 numbers

Test #13:

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

input:

5000
3950 1579 4116 3950 1580 2349 3588 1116 1203 1337 3381 14 2665 704 4209 4282 1378 1240 3861 123...

output:

1 769937747 288794947 838367984 809894896 8920175 563786519 786092538 316440917 886388958 454968445 ...

result:

ok 5000 numbers

Test #14:

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

input:

5000
1037 2379 4 3905 4465 4006 215 3159 1345 817 4152 570 2576 1331 3543 4175 3172 1713 1900 2341 3...

output:

1 362471484 580353531 214199131 387749397 153112251 65108787 418620525 477340316 818854310 388403704...

result:

ok 5000 numbers

Test #15:

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

input:

5000
4781 2496 4420 229 3573 883 4872 425 831 2509 2158 4260 3307 1957 2684 1989 4715 491 2192 3925 ...

output:

1 808823718 260069678 153783709 837023368 261523069 759000673 123260563 971195264 145849539 11028489...

result:

ok 5000 numbers

Subtask #4:

score: 25
Accepted

Test #16:

score: 25
Accepted
time: 97ms
memory: 16196kb

input:

100000
86560 16927 30583 83801 57591 24132 13102 39651 13929 15385 27654 35116 40036 11849 88109 110...

output:

1 810657976 660682135 479889731 661485379 881944304 731701740 689269979 544652213 880321662 40473518...

result:

ok 100000 numbers

Test #17:

score: 0
Accepted
time: 88ms
memory: 16196kb

input:

100000
74036 20305 91457 81064 90725 45468 47457 39678 12792 6159 51461 43033 15356 51115 27187 8518...

output:

1 538161387 960365002 327356420 561995082 518945460 898311365 186234308 955007476 587537983 42987804...

result:

ok 100000 numbers

Test #18:

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

input:

100000
93922 93922 93922 93922 93922 93922 93922 93922 93922 93922 93922 93922 93922 93922 93922 939...

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 100000 numbers

Test #19:

score: 0
Accepted
time: 145ms
memory: 15788kb

input:

100000
13786 42969 26284 59831 58948 42862 94580 4501 62990 72608 23684 63892 57032 23283 42969 5625...

output:

1 791861246 393148724 363533605 253354476 609323099 257284417 939279685 71264555 407757799 148545399...

result:

ok 100000 numbers

Test #20:

score: 0
Accepted
time: 501ms
memory: 16204kb

input:

100000
95100 85244 70057 858 50262 42340 80346 17888 36650 44663 62479 67644 16250 12029 94760 9762 ...

output:

1 124745106 219330979 429611336 583501074 125460209 453711261 687532518 663554135 836191792 53507958...

result:

ok 100000 numbers

Test #21:

score: 0
Accepted
time: 493ms
memory: 16196kb

input:

100000
79637 72299 35218 99712 53361 74692 45382 6591 94338 64346 93867 54441 94667 11413 78019 7904...

output:

1 182027277 452213211 990661442 275474404 553799540 661696562 344792951 885078451 908719534 48686820...

result:

ok 100000 numbers

Test #22:

score: 0
Accepted
time: 659ms
memory: 16200kb

input:

100000
40257 92255 38596 87039 93572 9288 1781 87580 7456 80790 68694 30281 58013 86491 26286 31050 ...

output:

1 292495574 683976498 467220974 228706619 857030238 192827335 695523707 400987767 12733082 840128901...

result:

ok 100000 numbers

Test #23:

score: 0
Accepted
time: 321ms
memory: 16200kb

input:

100000
28642 47626 81912 37197 2161 6655 25416 78482 75307 59534 14615 52278 50663 82716 29808 92522...

output:

1 175112175 105094989 435975484 341178868 369773500 987880839 28509120 946710427 184431948 775986107...

result:

ok 100000 numbers

Test #24:

score: 0
Accepted
time: 502ms
memory: 16128kb

input:

100000
27639 50 50 25969 25969 25969 62428 62428 62428 62428 96003 96003 96003 96003 96003 72270 722...

output:

1 627682396 723928424 245408764 969955647 180148619 498153987 14891693 977818238 109580121 207940574...

result:

ok 100000 numbers

Test #25:

score: 0
Accepted
time: 532ms
memory: 16164kb

input:

100000
7661 92732 97876 97876 67453 67453 7783 7783 7783 12813 12813 12813 74383 74383 74383 74383 6...

output:

1 617136501 156311359 488256263 524804034 29799009 262014552 338584050 86701707 81094987 608283013 3...

result:

ok 100000 numbers

Test #26:

score: 0
Accepted
time: 635ms
memory: 16188kb

input:

100000
30459 89816 19030 84923 84923 79923 79923 58979 58979 3531 3531 3531 37379 37379 37379 51468 ...

output:

1 641354320 537466259 489771119 967093011 117996510 675523481 698138016 957223461 369638244 77739253...

result:

ok 100000 numbers

Test #27:

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

input:

100000
54447 88151 23869 40183 71572 71572 2433 2433 42522 42522 6335 6335 87309 87309 87309 435 435...

output:

1 140526484 801108363 882500364 452227683 765062330 563179233 849790224 742107883 647429821 38712607...

result:

ok 100000 numbers

Test #28:

score: 0
Accepted
time: 539ms
memory: 16200kb

input:

100000
76179 45541 85638 99711 23550 28222 85944 90235 40850 85517 90600 71454 21289 3776 15379 9972...

output:

1 56457424 182686799 92934644 562426023 827701255 277685516 12226134 502758437 664379992 31568371 26...

result:

ok 100000 numbers

Test #29:

score: 0
Accepted
time: 520ms
memory: 16200kb

input:

100000
56749 25676 38772 48925 2757 55739 41298 85829 4941 60295 50600 46764 64902 9162 54510 12017 ...

output:

1 553894197 493699056 613427609 894037067 229393510 366167038 939987552 957368889 405515733 38968785...

result:

ok 100000 numbers

Subtask #5:

score: 10
Accepted

Test #30:

score: 10
Accepted
time: 306ms
memory: 68500kb

input:

500000
2 1 2 2 1 2 2 2 1 2 1 2 1 2 1 2 2 1 1 2 2 1 2 2 1 2 2 1 1 1 1 1 2 2 2 1 1 2 2 2 1 2 2 2 2 2 1...

output:

1 608799038 817513309 868760797 580987707 768212657 638828508 793489247 640713265 948353945 30727575...

result:

ok 500000 numbers

Test #31:

score: 0
Accepted
time: 296ms
memory: 68496kb

input:

500000
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2...

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 500000 numbers

Subtask #6:

score: 10
Accepted

Test #32:

score: 10
Accepted
time: 378ms
memory: 72396kb

input:

500000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

output:

1 194610091 219500315 55663251 16170074 672530946 447510445 851953477 319350373 593281340 673717938 ...

result:

ok 500000 numbers

Test #33:

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

input:

1
1

output:

1 

result:

ok 1 number(s): "1"

Test #34:

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

input:

2
1 2

output:

1 1 

result:

ok 2 number(s): "1 1"

Test #35:

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

input:

3
1 2 3

output:

1 4 1 

result:

ok 3 number(s): "1 4 1"

Subtask #7:

score: 10
Accepted

Test #36:

score: 10
Accepted
time: 372ms
memory: 70448kb

input:

500000
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 ...

output:

1 898793417 678135587 377054826 659950997 721112191 671331268 715580251 335806132 87188453 82502484 ...

result:

ok 500000 numbers

Test #37:

score: 0
Accepted
time: 364ms
memory: 70448kb

input:

499999
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 ...

output:

1 931777063 312717307 671142207 66514075 900807256 485804013 235170076 494546023 928341075 208355865...

result:

ok 499999 numbers

Test #38:

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

input:

1
1

output:

1 

result:

ok 1 number(s): "1"

Test #39:

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

input:

2
1 1

output:

1 0 

result:

ok 2 number(s): "1 0"

Test #40:

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

input:

3
1 1 2

output:

1 2 0 

result:

ok 3 number(s): "1 2 0"

Subtask #8:

score: 10
Accepted

Test #41:

score: 10
Accepted
time: 804ms
memory: 72400kb

input:

500000
42716 148212 28923 251527 20607 298615 171688 373403 263351 29269 123682 453906 293728 396943...

output:

1 433268411 257991313 118403173 391249294 581462066 108945621 165527440 598956183 78612748 809506391...

result:

ok 500000 numbers

Test #42:

score: 0
Accepted
time: 381ms
memory: 72400kb

input:

500000
346710 31305 488758 314953 368517 415142 207989 267950 131693 280286 355712 393818 419539 277...

output:

1 194610091 219500315 55663251 16170074 672530946 447510445 851953477 319350373 593281340 673717938 ...

result:

ok 500000 numbers

Test #43:

score: 0
Accepted
time: 322ms
memory: 68496kb

input:

500000
296403 296403 296403 296403 296403 296403 296403 296403 296403 296403 296403 296403 296403 29...

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 500000 numbers

Test #44:

score: 0
Accepted
time: 1755ms
memory: 69920kb

input:

500000
421312 48524 323818 491864 323221 178195 302492 29483 416277 253330 181006 237214 29483 37343...

output:

1 461979725 711652994 704272370 810875222 13456003 106275089 3056266 309304362 715678649 961799049 8...

result:

ok 500000 numbers

Test #45:

score: 0
Accepted
time: 5166ms
memory: 72372kb

input:

500000
98008 453524 115669 422 237424 279862 159107 32934 125465 446382 228013 420789 415065 346855 ...

output:

1 617365033 684392464 635015784 375807697 719590916 637573628 587509025 410357746 880057728 51103096...

result:

ok 500000 numbers

Test #46:

score: 0
Accepted
time: 5212ms
memory: 72364kb

input:

500000
324991 353351 219439 428238 97477 15055 235784 272042 195163 475992 54963 117336 272042 36050...

output:

1 4533855 242627984 55282132 54888268 754449428 902101408 92124873 441515878 896778143 18431053 6752...

result:

ok 500000 numbers

Test #47:

score: 0
Accepted
time: 5446ms
memory: 72396kb

input:

500000
361437 319349 392035 155858 262195 159630 53117 106181 250305 442125 28927 461914 284434 3905...

output:

1 803581693 517443757 587289764 10781668 947352089 943156509 160393537 828891910 218137409 604095640...

result:

ok 500000 numbers

Test #48:

score: 0
Accepted
time: 3426ms
memory: 72400kb

input:

500000
267769 470627 380223 371144 288025 372890 148012 453125 243673 485731 438005 286773 219273 33...

output:

1 259945542 887727691 422954074 827280412 912719262 443154458 244032579 892063992 55050907 238110554...

result:

ok 500000 numbers

Test #49:

score: 0
Accepted
time: 6010ms
memory: 71012kb

input:

500000
366957 117806 117806 418706 418706 418706 294158 294158 294158 294158 142191 142191 142191 14...

output:

1 630919615 716352345 496790718 666759002 983106625 677694164 175144732 817463904 466452242 24895727...

result:

ok 500000 numbers

Test #50:

score: 0
Accepted
time: 5460ms
memory: 71544kb

input:

500000
31342 224118 331887 331887 327403 327403 331480 331480 331480 316845 316845 316845 356128 356...

output:

1 424906347 767299998 495461130 855742771 692671444 431331015 617267095 183762199 776650441 24445500...

result:

ok 500000 numbers

Test #51:

score: 0
Accepted
time: 4786ms
memory: 71764kb

input:

500000
331372 237064 17451 353983 353983 153079 153079 1068 1068 366689 366689 366689 92132 92132 92...

output:

1 626222981 474512525 599069037 121550032 4896682 699018092 872009855 304614255 260341646 580730397 ...

result:

ok 500000 numbers

Test #52:

score: 0
Accepted
time: 5021ms
memory: 71904kb

input:

500000
10504 395634 479306 32314 69571 69571 47472 47472 499601 499601 353658 353658 448061 448061 4...

output:

1 738324123 772892468 923391277 472192860 32671501 800551746 176545004 604819243 739257614 269027041...

result:

ok 500000 numbers

Test #53:

score: 0
Accepted
time: 5945ms
memory: 72384kb

input:

500000
101795 486491 271254 206067 22183 263155 77757 494049 90060 69661 68105 22877 98816 76594 191...

output:

1 793913116 698361619 327856051 620668404 316060500 325486978 163554962 360719058 658606060 76663166...

result:

ok 500000 numbers

Test #54:

score: 0
Accepted
time: 5767ms
memory: 72396kb

input:

500000
323070 211716 227812 304022 161853 224139 186536 127455 216130 321351 100809 244656 328928 38...

output:

1 748929603 796307550 814289482 617936456 943436242 636923164 267827225 132172813 842735484 43357706...

result:

ok 500000 numbers

Extra Test:

score: 0
Extra Test Passed