QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#734656#9611. 木桶效应hos_lyric#100 ✓3451ms4336kbC++147.0kb2024-11-11 13:46:232024-11-11 13:46:24

Judging History

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

  • [2024-11-11 13:46:24]
  • 评测
  • 测评结果:100
  • 用时:3451ms
  • 内存:4336kb
  • [2024-11-11 13:46:23]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;


int N, M, Q;
vector<int> X, Y, W;

int M0, N0;
int V[11][11];

int gt[11][55];
Mint bn[55][55];
Mint dp[1 << 10][55];

int main() {
  for (; ~scanf("%d%d%d", &N, &M, &Q); ) {
    X.resize(Q);
    Y.resize(Q);
    W.resize(Q);
    for (int q = 0; q < Q; ++q) {
      scanf("%d%d%d", &X[q], &Y[q], &W[q]);
    }
    
    bool valid = true;
    {
      map<pair<int, int>, int> w;
      for (int q = 0; q < Q; ++q) w[make_pair(X[q], Y[q])] = W[q];
      for (int q = 0; q < Q; ++q) valid = valid && (w[make_pair(X[q], Y[q])] == W[q]);
    }
    Mint ans = 0;
    if (valid) {
      {
        auto xs = X;
        auto ys = Y;
        sort(xs.begin(), xs.end());
        sort(ys.begin(), ys.end());
        xs.erase(unique(xs.begin(), xs.end()), xs.end());
        ys.erase(unique(ys.begin(), ys.end()), ys.end());
        M0 = xs.size();
        N0 = ys.size();
        memset(V, 0, sizeof(V));
        for (int q = 0; q < Q; ++q) {
          X[q] = lower_bound(xs.begin(), xs.end(), X[q]) - xs.begin();
          Y[q] = lower_bound(ys.begin(), ys.end(), Y[q]) - ys.begin();
          V[X[q]][Y[q]] = W[q];
        }
      }
cerr<<"M0 = "<<M0<<", N0 = "<<N0<<", V = "<<endl;for(int x=0;x<M0;++x)pv(V[x],V[x]+N0);
      vector<int> on(M0, 0);
      vector<int> minVs(N0, N);
      memset(gt, 0, sizeof(gt));
      for (int x = 0; x < M0; ++x) for (int a = 1; a <= N; ++a) gt[x][a] = N + 1 - a;
      for (int x = 0; x < M0; ++x) for (int y = 0; y < N0; ++y) if (V[x][y]) {
        on[x] |= 1 << y;
        chmin(minVs[y], V[x][y]);
        for (int a = 1; a <= V[x][y]; ++a) --gt[x][a];
      }
      memset(bn, 0, sizeof(bn));
      for (int n = 0; n <= N; ++n) {
        bn[n][0] = bn[n][n] = 1;
        for (int k = 1; k < n; ++k) bn[n][k] = bn[n - 1][k - 1] + bn[n - 1][k];
      }
      memset(dp, 0, sizeof(dp));
      dp[0][0] = 1;
      for (int a = N; a >= 1; --a) {
        for (int y = 0; y < N0; ++y) if (minVs[y] >= a) {
          for (int h = 0; h < 1 << N0; ++h) if (!(h & 1 << y)) for (int k = 0; k <= N - N0; ++k) {
            Mint t = dp[h][k];
            if (t) {
              for (int x = 0; x < M0; ++x) if (!V[x][y]) {
                t *= (gt[x][a] - (__builtin_popcount(h & ~on[x]) + k));
              }
              t *= Mint((N + 1 - a) - (__builtin_popcount(h) + k)).pow(M - M0);
            }
            dp[h | 1 << y][k] += t;
          }
        }
// if(M<=3&&N<=3){cerr<<"HALF a = "<<a<<endl;for(int h=0;h<1<<N0;++h){cerr<<"  dp["<<h<<"] = ";pv(dp[h],dp[h]+(N-N0+1));}}
        for (int h = 0; h < 1 << N0; ++h) for (int k = N - N0; k >= 0; --k) {
          Mint t = dp[h][k];
          for (int kk = k; kk < N - N0; ) {
            for (int x = 0; x < M0; ++x) {
              t *= (gt[x][a] - (__builtin_popcount(h & ~on[x]) + kk));
            }
            t *= Mint((N + 1 - a) - (__builtin_popcount(h) + kk)).pow(M - M0);
            if (!t) break;
            ++kk;
            dp[h][kk] += t * bn[kk][k];
          }
        }
// if(M<=3&&N<=3){cerr<<"DONE a = "<<a<<endl;for(int h=0;h<1<<N0;++h){cerr<<"  dp["<<h<<"] = ";pv(dp[h],dp[h]+(N-N0+1));}}
      }
      ans = dp[(1 << N0) - 1][N - N0];
    }
    
    printf("%u\n", ans.x);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 4
Accepted

Test #1:

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

input:

5 3 0

output:

21412920

result:

ok single line: '21412920'

Test #2:

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

input:

5 3 2
3 4 4
2 5 3

output:

847674

result:

ok single line: '847674'

Test #3:

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

input:

5 3 3
3 5 5
1 2 5
2 5 3

output:

168780

result:

ok single line: '168780'

Subtask #2:

score: 8
Accepted

Dependency #1:

100%
Accepted

Test #4:

score: 8
Accepted
time: 0ms
memory: 4044kb

input:

7 3 0

output:

160221085

result:

ok single line: '160221085'

Test #5:

score: 8
Accepted
time: 0ms
memory: 4040kb

input:

7 3 2
1 1 5
1 6 2

output:

598007855

result:

ok single line: '598007855'

Test #6:

score: 8
Accepted
time: 0ms
memory: 4332kb

input:

7 3 3
1 1 5
2 6 3
2 1 7

output:

950880222

result:

ok single line: '950880222'

Subtask #3:

score: 8
Accepted

Test #7:

score: 8
Accepted
time: 1ms
memory: 4336kb

input:

50 1 0

output:

263941435

result:

ok single line: '263941435'

Test #8:

score: 8
Accepted
time: 1ms
memory: 4044kb

input:

43 2 0

output:

136378346

result:

ok single line: '136378346'

Test #9:

score: 8
Accepted
time: 1ms
memory: 4336kb

input:

50 2 0

output:

489087596

result:

ok single line: '489087596'

Subtask #4:

score: 12
Accepted

Dependency #3:

100%
Accepted

Test #10:

score: 12
Accepted
time: 3ms
memory: 4332kb

input:

50 292247015 0

output:

226872193

result:

ok single line: '226872193'

Test #11:

score: 12
Accepted
time: 0ms
memory: 4048kb

input:

50 873009728 0

output:

63648791

result:

ok single line: '63648791'

Subtask #5:

score: 16
Accepted

Dependency #2:

100%
Accepted

Dependency #4:

100%
Accepted

Test #12:

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

input:

20 728836785 5
248289571 15 16
439110385 8 15
339467267 12 7
585491339 7 9
605518440 4 1

output:

761275883

result:

ok single line: '761275883'

Test #13:

score: 16
Accepted
time: 2ms
memory: 4044kb

input:

20 288197925 5
31379347 9 1
222153278 1 1
24531748 20 1
106427339 18 1
212338331 17 1

output:

877851586

result:

ok single line: '877851586'

Test #14:

score: 16
Accepted
time: 7ms
memory: 4044kb

input:

20 880439688 5
563540321 17 20
395236367 3 20
300712779 6 20
121406689 18 20
25890496 9 20

output:

186649553

result:

ok single line: '186649553'

Test #15:

score: 16
Accepted
time: 2ms
memory: 4304kb

input:

20 311402369 5
311293636 14 13
306211116 19 19
36858994 5 11
36858994 19 10
306211116 14 18

output:

415461922

result:

ok single line: '415461922'

Test #16:

score: 16
Accepted
time: 3ms
memory: 4304kb

input:

20 98953332 5
1075868 12 5
31161114 8 12
46790018 9 10
39214697 15 7
46790018 8 8

output:

204149614

result:

ok single line: '204149614'

Subtask #6:

score: 12
Accepted

Dependency #5:

100%
Accepted

Test #17:

score: 12
Accepted
time: 76ms
memory: 4056kb

input:

50 915702052 5
541920465 39 16
833447607 49 14
326677362 14 34
412319566 10 36
206682128 46 28

output:

783441394

result:

ok single line: '783441394'

Test #18:

score: 12
Accepted
time: 16ms
memory: 4016kb

input:

50 47879239 5
30666754 20 1
23945845 17 1
27229141 25 1
40551703 9 1
15723198 18 1

output:

546399382

result:

ok single line: '546399382'

Test #19:

score: 12
Accepted
time: 98ms
memory: 4120kb

input:

50 334191703 5
167838076 19 50
95127599 33 50
221030062 4 50
89223523 36 50
40736662 39 50

output:

242104112

result:

ok single line: '242104112'

Test #20:

score: 12
Accepted
time: 16ms
memory: 4044kb

input:

50 33538004 5
1341436 40 26
19132404 4 13
22271562 24 25
19132404 40 18
19132404 24 38

output:

509216038

result:

ok single line: '509216038'

Test #21:

score: 12
Accepted
time: 42ms
memory: 4084kb

input:

50 453197583 5
304895210 17 41
450727109 33 20
129855576 4 24
214262208 12 46
214262208 4 27

output:

929330226

result:

ok single line: '929330226'

Subtask #7:

score: 20
Accepted

Dependency #6:

100%
Accepted

Test #22:

score: 20
Accepted
time: 359ms
memory: 4076kb

input:

50 733592974 7
204969642 20 32
532101473 38 22
428067108 29 19
699479674 23 35
403248947 11 33
523610998 39 50
207894041 37 47

output:

878602112

result:

ok single line: '878602112'

Test #23:

score: 20
Accepted
time: 60ms
memory: 4104kb

input:

50 363989184 7
275675456 32 1
14440487 41 1
84675645 18 1
112753584 7 1
259289962 30 1
244964889 42 1
34667760 27 1

output:

125183718

result:

ok single line: '125183718'

Test #24:

score: 20
Accepted
time: 422ms
memory: 4040kb

input:

50 987882639 7
17913003 22 50
440453368 26 50
849090802 8 50
106136773 13 50
491841612 34 50
131376512 15 50
231961452 39 50

output:

573542486

result:

ok single line: '573542486'

Test #25:

score: 20
Accepted
time: 25ms
memory: 4040kb

input:

50 739368354 7
163377222 20 27
192827369 29 10
688310083 50 13
20398047 27 45
688310083 29 11
192827369 20 5
688310083 27 27

output:

528550901

result:

ok single line: '528550901'

Test #26:

score: 20
Accepted
time: 62ms
memory: 4332kb

input:

50 691852384 7
552436456 27 11
103126750 8 13
122888842 49 34
306609066 7 11
553011271 38 32
553011271 27 47
553011271 49 50

output:

407673475

result:

ok single line: '407673475'

Test #27:

score: 20
Accepted
time: 116ms
memory: 4040kb

input:

50 828246844 7
528561184 35 4
15522388 43 35
679004541 17 20
161133582 6 20
289045078 32 49
705209075 24 38
705209075 17 44

output:

734432988

result:

ok single line: '734432988'

Subtask #8:

score: 12
Accepted

Dependency #7:

100%
Accepted

Test #28:

score: 12
Accepted
time: 1249ms
memory: 4076kb

input:

50 818455715 9
460012830 5 33
118588510 47 47
281020207 23 25
175303600 1 18
234157803 48 32
256460906 19 46
287657461 24 13
81772046 14 22
114821805 44 41

output:

863394340

result:

ok single line: '863394340'

Test #29:

score: 12
Accepted
time: 201ms
memory: 4040kb

input:

50 24969517 9
23565097 13 1
11005666 43 1
21727790 47 1
12763778 6 1
6881701 34 1
2985794 23 1
13971040 15 1
13187518 29 1
1995154 12 1

output:

755357189

result:

ok single line: '755357189'

Test #30:

score: 12
Accepted
time: 1705ms
memory: 4128kb

input:

50 429772980 9
414497820 4 50
141139445 19 50
407732495 7 50
83785413 1 50
164351908 8 50
213988430 26 50
52240611 50 50
352159955 27 50
279326344 29 50

output:

247413078

result:

ok single line: '247413078'

Test #31:

score: 12
Accepted
time: 93ms
memory: 4060kb

input:

50 919810726 9
403069670 48 3
193931638 39 28
821686776 22 11
432351038 8 49
466123383 14 34
254851369 2 48
193931638 48 38
821686776 8 19
254851369 8 20

output:

481094724

result:

ok single line: '481094724'

Test #32:

score: 12
Accepted
time: 109ms
memory: 4048kb

input:

50 619619808 9
140356219 25 31
289966305 18 15
308904151 23 44
530131662 15 2
174186843 35 5
452736627 42 7
12231110 21 2
308904151 18 33
140356219 18 30

output:

194056785

result:

ok single line: '194056785'

Test #33:

score: 12
Accepted
time: 216ms
memory: 4048kb

input:

50 405638475 9
189173757 25 33
313860932 22 8
196599730 26 6
273814431 3 2
52701532 15 14
335951766 19 3
313077524 29 13
349363426 7 15
313860932 19 19

output:

435209021

result:

ok single line: '435209021'

Subtask #9:

score: 8
Accepted

Dependency #8:

100%
Accepted

Test #34:

score: 8
Accepted
time: 1416ms
memory: 4120kb

input:

50 946059042 10
160844820 11 40
246397981 49 28
373026730 1 18
602144414 9 9
284028279 39 44
22155194 16 2
851925494 6 17
862159117 27 32
773018252 10 20
462257810 48 19

output:

680391855

result:

ok single line: '680391855'

Test #35:

score: 8
Accepted
time: 435ms
memory: 4304kb

input:

50 260661857 10
70899239 47 1
248286199 34 1
49459370 23 1
110741847 3 1
13147285 19 1
75609139 10 1
16001621 49 1
258636033 17 1
41404092 36 1
259336018 9 1

output:

621578303

result:

ok single line: '621578303'

Test #36:

score: 8
Accepted
time: 3451ms
memory: 4048kb

input:

50 444766827 10
138606634 2 50
356874907 48 50
186818234 44 50
328471606 8 50
57922803 45 50
442175507 50 50
45412871 41 50
111164719 24 50
278665393 39 50
49548102 15 50

output:

476500313

result:

ok single line: '476500313'

Test #37:

score: 8
Accepted
time: 121ms
memory: 3988kb

input:

50 471185620 10
330350191 32 46
19091103 17 9
383913632 38 25
125638511 4 13
227803510 12 49
140314389 27 34
97824926 22 46
19091103 27 16
140314389 38 4
97824926 12 1

output:

746959227

result:

ok single line: '746959227'

Test #38:

score: 8
Accepted
time: 500ms
memory: 3988kb

input:

50 534235345 10
505481965 11 39
325061903 12 35
443938537 40 20
144968487 29 31
292692014 18 39
423914803 14 36
221863124 46 41
413488879 5 39
413488879 14 5
325061903 11 42

output:

83413094

result:

ok single line: '83413094'

Test #39:

score: 8
Accepted
time: 700ms
memory: 4044kb

input:

50 268783055 10
164040236 5 3
77416439 15 8
215664484 32 34
84807773 8 44
242418459 20 43
132116147 31 13
11369559 50 23
264864429 6 42
153908131 3 27
132116147 20 50

output:

858392519

result:

ok single line: '858392519'

Test #40:

score: 8
Accepted
time: 76ms
memory: 4040kb

input:

50 680178330 10
381317639 8 3
97592656 22 6
66456445 4 9
277935856 2 13
629083753 35 33
86588466 7 25
86588466 8 6
66456445 22 11
97592656 8 23
277935856 7 24

output:

274256402

result:

ok single line: '274256402'