UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#203381#3560. 小院闲窗春色深sjc0610311001583ms3888kbC++113.8kb2024-02-24 10:36:512024-02-24 13:04:00

answer

#include <bits/stdc++.h>
#define MOD 998244353
using namespace std;

int n,m,p[410],id[410],num[410],a[410][410],b[410][410],f[410][410],c[410][410],dp[410],pw1[100010],pw2[100010];
vector<int> vec[410];

inline int find_set(int x)
{
	return p[x]==x?x:p[x]=find_set(p[x]);
}

inline void add1(int &x,int y)
{
	x+=y;
	if(x>=MOD) x-=MOD;
}

inline void add2(int &x,int y)
{
	x+=y;
	if(x<0) x+=MOD; 
}

inline int my_pow(int x,int y)
{
	if(y==0) return 1;
	if(y==1) return x;
	int res=my_pow(x,y/2);
	if(y%2==0) return 1ll*res*res%MOD;
	else return 1ll*res*res%MOD*x%MOD; 
}

signed main()
{
	ios::sync_with_stdio(false);cin.tie(0);
	int t;cin>>t;
	for(int i=0;i<=400;i++) c[i][0]=1;
	for(int i=1;i<=400;i++){
		for(int j=1;j<=400;j++){
			c[i][j]=c[i-1][j];
			add1(c[i][j],c[i-1][j-1]);
		}
	}
	while(t--){
		cin>>n>>m;
		for(int i=1;i<=n;i++) p[i]=i,vec[i].clear();
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				cin>>a[i][j];
				if(a[i][j]==0){
					int rootx=find_set(i),rooty=find_set(j);
					if(rootx!=rooty) p[rooty]=rootx;
				}
			}
		}
		for(int i=1;i<=n;i++) vec[find_set(i)].push_back(i);
		bool flag=true;
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++) if(a[i][j]!=a[j][i]) flag=false;
		}
		int tot=0;
		for(int i=1;i<=n;i++) if(find_set(i)==i){
			tot++;num[tot]=0;
			for(int j=0;j<(int)vec[i].size();j++) id[vec[i][j]]=tot,num[tot]++;
			for(int j=0;j<(int)vec[i].size();j++){
				for(int k=j+1;k<(int)vec[i].size();k++){
					if(a[vec[i][j]][vec[i][k]]!=0) flag=false;
				}
			}
		}
		for(int i=1;i<=tot;i++) for(int j=1;j<=tot;j++) b[i][j]=0; 
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				if(b[id[i]][id[j]]==0) b[id[i]][id[j]]=b[id[j]][id[i]]=a[i][j];
				else{
					if(a[i][j]!=b[id[i]][id[j]]) flag=false;
				}
			}
		}
		if(!flag){
			cout<<0<<'\n';
			continue;
		}
		for(int i=1;i<=tot;i++){
			for(int j=1;j<=tot;j++){
				f[i][j]=b[i][j];
			}
		}
		for(int k=1;k<=tot;k++){
			for(int i=1;i<=tot;i++){
				for(int j=1;j<=tot;j++){
					f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
				}
			}
		}
		int ans=0,res=1;
		dp[1]=1;
		for(int i=2;i<=n;i++){
			dp[i]=my_pow(m+1,i*(i-1)/2);
			for(int j=0;j<i-1;j++){
				add2(dp[i],-1ll*dp[j+1]*c[i-1][j]%MOD*my_pow(m,(j+1)*(i-j-1))%MOD*my_pow(m+1,(i-j-1)*(i-j-2)/2)%MOD);
			}
		}
		for(int i=1;i<=tot;i++) res=1ll*res*dp[num[i]]%MOD;
		for(int i=1;i<=tot;i++){
			for(int j=i+1;j<=tot;j++){
				if(f[i][j]<b[i][j]){
					res=0;continue;
				}
				bool ok=false;
				for(int k=1;k<=tot;k++) if(k!=i&&k!=j){
					if(f[i][k]+f[k][j]==b[i][j]){
						ok=true;break;
					}
				}
				if(m<b[i][j]){
					res=0;continue;
				}
				if(ok){
					res=1ll*res*my_pow(m-b[i][j]+1,num[i]*num[j])%MOD;
				}
				else{
					int cur=my_pow(m-b[i][j]+1,num[i]*num[j]);
					add2(cur,-my_pow(m-b[i][j],num[i]*num[j]));
					res=1ll*res*cur%MOD;
				}
			}
		}
		add1(ans,res);
		if(m>0){
			m--;res=1;
			dp[1]=1;
			for(int i=2;i<=n;i++){
				dp[i]=my_pow(m+1,i*(i-1)/2);
				for(int j=0;j<i-1;j++){
					add2(dp[i],-1ll*dp[j+1]*c[i-1][j]%MOD*my_pow(m,(j+1)*(i-j-1))%MOD*my_pow(m+1,(i-j-1)*(i-j-2)/2)%MOD);
				}
			}
			for(int i=1;i<=tot;i++) res=1ll*res*dp[num[i]]%MOD;
			for(int i=1;i<=tot;i++){
				for(int j=i+1;j<=tot;j++){
					if(f[i][j]<b[i][j]){
						res=0;continue;
					}
					bool ok=false;
					for(int k=1;k<=tot;k++) if(k!=i&&k!=j){
						if(f[i][k]+f[k][j]==b[i][j]){
							ok=true;break;
						}
					}
					if(m<b[i][j]){
						res=0;continue;
					}
					if(ok){
						res=1ll*res*my_pow(m-b[i][j]+1,num[i]*num[j])%MOD;
					}
					else{
						int cur=my_pow(m-b[i][j]+1,num[i]*num[j]);
						add2(cur,-my_pow(m-b[i][j],num[i]*num[j]));
						res=1ll*res*cur%MOD;
					}
				}
			}
			add2(ans,-res);
		}
		cout<<ans<<'\n';
	}
	return 0;
}

Details

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

Test #1:

score: 10
Accepted
time: 0ms
memory: 1964kb

input:

204
3 3
0 2 1
2 0 3
1 3 0
3 5
0 3 4
3 0 1
4 1 0
4 5
0 1 1 3
1 0 2 4
1 2 0 4
3 4 4 0
4 5
0 2 3 3
2 0 ...

output:

1
1
13
1
4
4
0
0
18
7
0
1
1
0
2
0
13
0
4
4
0
18
7
0
0
1
0
1
1
3
0
0
1
0
1
1
1
1
1
1
1
1
0
1
1
0
0
1
...

result:

ok 204 numbers

Test #2:

score: 10
Accepted
time: 5ms
memory: 2136kb

input:

20
36 744373192
0 9312648 15440508 12836339 12090822 11814833 6490999 13847739 7862103 20584369 8561...

output:

661143199
508590528
837677780
135769368
10300994
164629851
853271557
642677104
785643498
837491821
9...

result:

ok 20 numbers

Test #3:

score: 5
Accepted
time: 264ms
memory: 3884kb

input:

2
400 1000000000
0 13808551 19328904 17405838 21149586 15404044 7866869 11422830 12942958 21193652 1...

output:

626881850
344046232

result:

ok 2 number(s): "626881850 344046232"

Test #4:

score: 5
Accepted
time: 256ms
memory: 3884kb

input:

2
400 1000000000
0 14770595 11960705 14167217 16875028 4062400 16938709 10974424 14779359 10201441 1...

output:

831783370
613181630

result:

ok 2 number(s): "831783370 613181630"

Test #5:

score: 5
Accepted
time: 264ms
memory: 3884kb

input:

2
400 1000000000
0 19670766 11305909 13777445 13208147 10981829 14581204 14947677 23051829 11830974 ...

output:

459514299
868236763

result:

ok 2 number(s): "459514299 868236763"

Test #6:

score: 5
Accepted
time: 266ms
memory: 3888kb

input:

2
400 1000000000
0 17723139 18222708 16934888 13023334 20268472 16395805 16596042 18027449 18601832 ...

output:

36755219
682293912

result:

ok 2 number(s): "36755219 682293912"

Test #7:

score: 20
Accepted
time: 82ms
memory: 2604kb

input:

2
398 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 ...

output:

269676500
218314154

result:

ok 2 number(s): "269676500 218314154"

Test #8:

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

input:

20
31 606668000
0 63776809 28098165 18927177 45964474 59245929 45964474 63776809 59245929 32851078 4...

output:

723771478
894028931
807934521
338045047
445442568
155627530
884657725
979980638
214630018
56377231
8...

result:

ok 20 numbers

Test #9:

score: 5
Accepted
time: 107ms
memory: 3268kb

input:

2
400 1000000000
0 9493556 3729536 15672132 6459545 6567649 4203273 2659943 8338120 3764957 5839726 ...

output:

23513587
596758462

result:

ok 2 number(s): "23513587 596758462"

Test #10:

score: 5
Accepted
time: 114ms
memory: 3304kb

input:

2
400 1000000000
0 4548100 5592201 6703474 1939013 2274200 1382749 2099318 2755020 2618761 3211869 6...

output:

257976477
541722834

result:

ok 2 number(s): "257976477 541722834"

Test #11:

score: 5
Accepted
time: 116ms
memory: 3300kb

input:

2
400 1000000000
0 14681334 8111719 7636981 10525492 5749614 10040863 8111719 9502166 7228179 811171...

output:

362571923
270871151

result:

ok 2 number(s): "362571923 270871151"

Test #12:

score: 5
Accepted
time: 105ms
memory: 3232kb

input:

2
400 1000000000
0 1294054 4015981 4550486 1232581 2472119 5603920 3007990 7944606 1294054 5346303 3...

output:

364141400
649756260

result:

ok 2 number(s): "364141400 649756260"

Extra Test:

score: 0
Extra Test Passed