UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212632#3836. 排列计数drdilyor100675ms26736kbC++1.9kb2024-10-19 16:29:592024-10-19 18:36:39

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
void ts(){cout<<"IAKIOI\n";}
inline int read(){
	int n=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		n=n*10+ch-'0';
		ch=getchar();
	}
	return n*f;
}
int n;
int a[605];
int f[605];
const int mod=1e9+7;
bool chk(int x){
	int l=1,r=(int)1e9;
	int res=-1;
	while(l<=r){
		int mid=(l+r)/2;
		if(mid*mid<=x){
			res=mid,l=mid+1;
		}
		else r=mid-1;
	}
	return res*res==x;
}
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
int l;
int b[605];
int dp[605][605];
int fac[605];
int C[2005][2005];
//前 i 组考虑下来, 有 j 个相邻的相同的.
void solve(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read(),f[i]=i;
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			int m=a[i]*a[j];
			if(chk(m)){
				f[find(i)]=find(j);
			}
		}
	}
	l=0;
	vector<int> cnt(n+1,0);
	for(int i=1;i<=n;i++)cnt[find(i)]++;
	for(int i=1;i<=n;i++){
		if(cnt[i])b[++l]=cnt[i];
	}
	sort(b+1,b+l+1);
	int coef=1;
	for(int i=1;i<=l;i++)coef*=fac[b[i]],coef%=mod;
	//for(int i=1;i<=l;i++)cout<<b[i]<<" ";
	//cout<<"\n";
	memset(dp,0,sizeof(dp));
	dp[1][b[1]-1]=1;
	int pre=b[1];
	for(int i=2;i<=l;i++){
		for(int j=0;j<=n;j++){
			if(!dp[i-1][j])continue;
			//j 个相邻的是相同的.
			//去掉旧的 x 个相同的.
			//分成 k 组.
			for(int k=1;k<=min(b[i],pre+1);k++){
				for(int x=0;x<=min(j,k);x++){
					//考虑分成
					dp[i][j-x+b[i]-k]+=C[b[i]-1][k-1]*C[j][x]%mod*C[pre+1-j][k-x]%mod*dp[i-1][j]%mod;
					dp[i][j-x+b[i]-k]%=mod;
				}
			}
		}
		pre+=b[i];
	}
	cout<<dp[l][0]*coef%mod<<"\n";
}
signed main(){
	fac[0]=1;
	for(int i=1;i<605;i++)fac[i]=fac[i-1]*i%mod;
	C[0][0]=1;
	for(int i=1;i<=2000;i++){
		C[i][0]=C[i][i]=1;
		for(int j=1;j<i;j++)C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
	}
	int t=read();
	while(t--)solve();
	return 0;
}
//look at my code
//my code is amazing

Details

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

Test #1:

score: 4
Accepted
time: 13ms
memory: 26724kb

input:

3
9
3 3 12 2 5 9 6 9 12
9
3 12 3 1 3 4 3 4 1
9
4 7 3 4 2 1 3 1 12

output:

37440
2880
26784

result:

ok 3 number(s): "37440 2880 26784"

Test #2:

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

input:

3
9
2 2 3 2 3 2 1 3 3
9
3 12 3 1 8 4 8 4 1
9
4 3 1 2 2 4 1 4 12

output:

13824
22752
2880

result:

ok 3 number(s): "13824 22752 2880"

Test #3:

score: 4
Accepted
time: 13ms
memory: 26728kb

input:

3
9
142688 8128512 48834968 8128512 32845824 15095808 32845824 106256304 26295752
9
75427268 2192000...

output:

37584
2880
13824

result:

ok 3 number(s): "37584 2880 13824"

Test #4:

score: 4
Accepted
time: 16ms
memory: 26724kb

input:

3
9
88062123 88062123 48720400 8065600 97440800 76837548 85020800 76837548 14578572
9
2860000 163216...

output:

2880
22752
2880

result:

ok 3 number(s): "2880 22752 2880"

Test #5:

score: 4
Accepted
time: 7ms
memory: 26728kb

input:

3
14
3178431 152649728 117099999 5125428 49768252 363660928 39427850 62943232 3178431 4426506 131833...

output:

437283798
262822393
437283798

result:

ok 3 number(s): "437283798 262822393 437283798"

Test #6:

score: 4
Accepted
time: 4ms
memory: 26728kb

input:

3
16
1545282 112628708 2908125 1445625 136589332 36142429 269503828 36142429 2908125 67898372 788839...

output:

14414094
72255083
981412223

result:

ok 3 number(s): "14414094 72255083 981412223"

Test #7:

score: 4
Accepted
time: 9ms
memory: 26720kb

input:

3
18
171916800 71250 249023808 47278088 307436800 88125 19722516 88281600 31052472 109190400 4727808...

output:

623077510
693147110
858749358

result:

ok 3 number(s): "623077510 693147110 858749358"

Test #8:

score: 4
Accepted
time: 7ms
memory: 26732kb

input:

3
18
201436200 145303200 4826682 11714560 2336727 145303200 248706800 10600448 10600448 344786300 44...

output:

665836280
876677155
850239044

result:

ok 3 number(s): "665836280 876677155 850239044"

Test #9:

score: 4
Accepted
time: 18ms
memory: 26732kb

input:

3
25
10145931 124099600 10145931 84640000 23374549 53138050 10145931 524800 620021061 321521805 1051...

output:

564677035
729332655
822285077

result:

ok 3 number(s): "564677035 729332655 822285077"

Test #10:

score: 4
Accepted
time: 13ms
memory: 26728kb

input:

3
36
858246213 165765632 41295872 274942272 194212096 59846752 67831696 754216369 67831696 67589728 ...

output:

487365905
47131199
263897884

result:

ok 3 number(s): "487365905 47131199 263897884"

Test #11:

score: 4
Accepted
time: 4ms
memory: 26732kb

input:

3
43
180948736 16070400 48926016 12443301 48926016 23653442 29451600 10020348 26696704 9883458 93085...

output:

346337755
945698304
897816656

result:

ok 3 number(s): "346337755 945698304 897816656"

Test #12:

score: 4
Accepted
time: 13ms
memory: 26732kb

input:

3
48
303275700 317146752 773592768 357038291 180342936 357038291 250880 24122774 191498 90166680 383...

output:

900111223
419473721
734260333

result:

ok 3 number(s): "900111223 419473721 734260333"

Test #13:

score: 4
Accepted
time: 5ms
memory: 26732kb

input:

3
50
54200832 256721322 267810625 6566833 237272737 21390625 744874800 296006000 721291875 7480225 1...

output:

934519143
942945314
208625415

result:

ok 3 number(s): "934519143 942945314 208625415"

Test #14:

score: 4
Accepted
time: 15ms
memory: 26720kb

input:

3
120
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 3...

output:

52147493
52147493
52147493

result:

ok 3 number(s): "52147493 52147493 52147493"

Test #15:

score: 4
Accepted
time: 4ms
memory: 26716kb

input:

3
138
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 3...

output:

612891723
612891723
612891723

result:

ok 3 number(s): "612891723 612891723 612891723"

Test #16:

score: 4
Accepted
time: 14ms
memory: 26720kb

input:

3
162
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 3...

output:

434843275
434843275
434843275

result:

ok 3 number(s): "434843275 434843275 434843275"

Test #17:

score: 4
Accepted
time: 10ms
memory: 26720kb

input:

3
185
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 3...

output:

331926949
331926949
331926949

result:

ok 3 number(s): "331926949 331926949 331926949"

Test #18:

score: 4
Accepted
time: 8ms
memory: 26720kb

input:

3
199
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 3...

output:

480864616
480864616
480864616

result:

ok 3 number(s): "480864616 480864616 480864616"

Test #19:

score: 4
Accepted
time: 17ms
memory: 26728kb

input:

3
140
454825431 218748672 521859924 214978676 377814488 78843600 18330048 255453264 25664841 6614150...

output:

599724215
116436571
614515367

result:

ok 3 number(s): "599724215 116436571 614515367"

Test #20:

score: 4
Accepted
time: 14ms
memory: 26728kb

input:

3
170
62799680 49290795 59062216 259200000 9772992 149713920 204546351 26689864 115110400 49290795 2...

output:

517171519
533298251
159070420

result:

ok 3 number(s): "517171519 533298251 159070420"

Test #21:

score: 4
Accepted
time: 21ms
memory: 26732kb

input:

3
188
23894112 87116724 182801234 46345552 25719300 182801234 105908077 235599925 262109925 54348048...

output:

354714011
121364949
670554540

result:

ok 3 number(s): "354714011 121364949 670554540"

Test #22:

score: 4
Accepted
time: 20ms
memory: 26728kb

input:

3
198
24553280 17298000 740763184 96610680 411361280 38720 187876030 285738796 55660 38720 86050908 ...

output:

289048382
60157290
399700373

result:

ok 3 number(s): "289048382 60157290 399700373"

Test #23:

score: 4
Accepted
time: 151ms
memory: 26736kb

input:

3
590
38449622 115600000 376521627 1510956 1444069 435573936 297048204 34413408 54189573 462250 1222...

output:

854594268
131460639
157559240

result:

ok 3 number(s): "854594268 131460639 157559240"

Test #24:

score: 4
Accepted
time: 128ms
memory: 26736kb

input:

3
590
907000000 55918784 892000000 5241600 103421313 28336896 8510400 34848600 5225472 30666768 1779...

output:

314483534
945347739
747567388

result:

ok 3 number(s): "314483534 945347739 747567388"

Test #25:

score: 4
Accepted
time: 151ms
memory: 26736kb

input:

3
599
21638400 21638400 345780750 430564352 25818296 4954880 77772992 6387084 11434863 402793472 311...

output:

780906791
962756707
423759220

result:

ok 3 number(s): "780906791 962756707 423759220"

Extra Test:

score: 0
Extra Test Passed