UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#203369#3560. 小院闲窗春色深yllcm1001303ms3060kbC++3.3kb2024-02-24 09:27:102024-02-24 13:00:51

answer

#include<bits/stdc++.h>
#define ll long long    
#define ull unsigned long long
#define db double
#define ldb long double
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define FR first
#define SE second
using namespace std;
inline int read() {
  int x = 0; bool op = 0;
  char c = getchar();
  while(!isdigit(c))op |= (c == '-'), c = getchar();
  while(isdigit(c))x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return op ? -x : x;
}
const int N = 410;
const int P = 998244353;
void add(int &a, int b) {a += b; a >= P ? a -= P : 0;}
void sub(int &a, int b) {a -= b; a < 0 ? a += P : 0;}
int ksm(int x, int k) {
  int res = 1;
  for(int pw = x; k; (k & 1) ? res = 1ll * res * pw % P : 0, pw = 1ll * pw * pw % P, k >>= 1);
  return res;
}
int n, m;
int a[N][N], f[N], sz[N];
int find(int x) {return f[x] == x ? x : f[x] = find(f[x]);}
bool work() {
  for(int i = 1; i <= n; i++)if(a[i][i])return false;
  for(int i = 1; i <= n; i++)
    for(int j = i + 1; j <= n; j++)
      if(a[i][j] != a[j][i])return false;
  for(int i = 1; i <= n; i++)f[i] = i, sz[i] = 1;
  for(int i = 1; i <= n; i++)
    for(int j = 1; j <= n; j++)
      if(a[i][j] == 0) {
        int x = find(i), y = find(j);
        if(x != y)f[x] = y, sz[y] += sz[x];
      }
  for(int i = 1; i <= n; i++)
    for(int j = 1; j <= n; j++) {
      int x = find(i), y = find(j);
      if(a[i][j] != a[x][y])return false;
    }
  return true;
}
int val1[N], val2[N * N], val3[N * N], fac[N], ifac[N];
void init() {
  fac[0] = ifac[0] = 1;
  for(int i = 1; i < N; i++)fac[i] = 1ll * fac[i - 1] * i % P;
  ifac[N - 1] = ksm(fac[N - 1], P - 2);
  for(int i = N - 2; i; i--)ifac[i] = 1ll * ifac[i + 1] * (i + 1) % P;
  return ;
}
int com(int n, int m) {return n < m || m < 0 ? 0 : 1ll * fac[n] * ifac[m] % P * ifac[n - m] % P;}
int calc(int m) {
  for(int i = 1; i <= n; i++)
    for(int j = 1; j <= n; j++)
      if(a[i][j] > m)return 0;
  val2[0] = val3[0] = 1;
  for(int i = 1; i <= n * n; i++) {
    val2[i] = 1ll * val2[i - 1] * m % P;
    val3[i] = 1ll * val3[i - 1] * (m + 1) % P;
  }
  for(int i = 1; i <= n; i++) {
    val1[i] = val3[i * (i - 1) / 2];
    for(int j = 1; j < i; j++) {
      int c1 = val1[j];
      int c2 = val2[j * (i - j)];
      int c3 = val3[(i - j) * (i - j - 1) / 2];
      int res = 1ll * c1 * c2 % P * c3 % P;
      sub(val1[i], 1ll * com(i - 1, j - 1) * res % P);
    }
  }
  int ans = 1;
  for(int i = 1; i <= n; i++)
    if(find(i) == i)ans = 1ll * ans * val1[sz[i]] % P;
  for(int i = 1; i <= n; i++) {
    for(int j = i + 1; j <= n; j++) {
      if(find(i) != i || find(j) != j)continue;
      int mn = m;
      for(int k = 1; k <= n; k++)
        if(a[i][k] && a[k][j])mn = min(mn, a[i][k] + a[k][j]);
      if(mn < a[i][j])return 0;
      int res = ksm(m - a[i][j] + 1, sz[i] * sz[j]);
      if(mn > a[i][j])sub(res, ksm(m - a[i][j], sz[i] * sz[j]));
      ans = 1ll * ans * res % P;
    }
  }
  return ans;
}
void solve() {
  n = read(); m = read();
  for(int i = 1; i <= n; i++)
    for(int j = 1; j <= n; j++)
      a[i][j] = read();
  if(!work())return puts("0"), void();
  int ans = (calc(m) - calc(m - 1) + P) % P;
  printf("%d\n", ans);
  return ;
}
int main() { 
  init();
  int test = read();
  while(test--)solve();
  return 0;
}

详细

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

Test #1:

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

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: 4ms
memory: 1240kb

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: 230ms
memory: 3060kb

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: 230ms
memory: 3056kb

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: 226ms
memory: 3060kb

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: 226ms
memory: 3060kb

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: 17ms
memory: 3052kb

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: 0ms
memory: 1244kb

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: 90ms
memory: 3060kb

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: 91ms
memory: 3056kb

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: 101ms
memory: 3060kb

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: 88ms
memory: 3056kb

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