QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#251218#7623. Coloring Tapeucup-team004AC ✓196ms4960kbC++207.5kb2023-11-14 14:04:542023-11-14 14:04:55

Judging History

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

  • [2023-11-14 14:04:55]
  • 评测
  • 测评结果:AC
  • 用时:196ms
  • 内存:4960kb
  • [2023-11-14 14:04:54]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;
template<class T>
constexpr T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}

constexpr i64 mul(i64 a, i64 b, i64 p) {
    i64 res = a * b - i64(1.L * a * b / p) * p;
    res %= p;
    if (res < 0) {
        res += p;
    }
    return res;
}
template<i64 P>
struct MLong {
    i64 x;
    constexpr MLong() : x{} {}
    constexpr MLong(i64 x) : x{norm(x % getMod())} {}
    
    static i64 Mod;
    constexpr static i64 getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(i64 Mod_) {
        Mod = Mod_;
    }
    constexpr i64 norm(i64 x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr i64 val() const {
        return x;
    }
    explicit constexpr operator i64() const {
        return x;
    }
    constexpr MLong operator-() const {
        MLong res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MLong inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MLong &operator*=(MLong rhs) & {
        x = mul(x, rhs.x, getMod());
        return *this;
    }
    constexpr MLong &operator+=(MLong rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MLong &operator-=(MLong rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MLong &operator/=(MLong rhs) & {
        return *this *= rhs.inv();
    }
    friend constexpr MLong operator*(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MLong operator+(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MLong operator-(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MLong operator/(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MLong &a) {
        i64 v;
        is >> v;
        a = MLong(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MLong &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MLong lhs, MLong rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MLong lhs, MLong rhs) {
        return lhs.val() != rhs.val();
    }
};

template<>
i64 MLong<0LL>::Mod = i64(1E18) + 9;

template<int P>
struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(i64 x) : x{norm(x % getMod())} {}
    
    static int Mod;
    constexpr static int getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(int Mod_) {
        Mod = Mod_;
    }
    constexpr int norm(int x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr int val() const {
        return x;
    }
    explicit constexpr operator int() const {
        return x;
    }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MInt &operator*=(MInt rhs) & {
        x = 1LL * x * rhs.x % getMod();
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) & {
        return *this *= rhs.inv();
    }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
};

template<>
int MInt<0>::Mod = 998244353;

template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();

constexpr int P = 998244353;
using Z = MInt<P>;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, m, r;
    std::cin >> n >> m >> r;
    
    std::vector<std::array<int, 3>> trans;
    for (int s = 0; s < (1 << n); s++) {
        auto dfs = [&](auto self, int x, int t, int col, int lst) -> void {
            if (x == 0) {
                if (lst == n) {
                    trans.push_back({s, t, col});
                }
                return;
            }
            int i = __builtin_ctz(x);
            int nx = x ^ (1 << i);
            if (i >= lst) {
                if (i > lst) {
                    self(self, nx, t | (1 << lst), col | (1 << lst), i + 1);
                } else {
                    for (int j = i; j < n; j++) {
                        self(self, nx, t | (1 << j), col | (1 << lst), j + 1);
                    }
                }
            }
        };
        dfs(dfs, s, 0, 0, 0);
    }
    
    std::vector<std::vector<std::array<int, 3>>> rest(m);
    for (int i = 0; i < r; i++) {
        int c, x, y, d;
        std::cin >> c >> x >> y >> d;
        c--, x--, y--;
        rest[c].push_back({x, y, d});
        if (c == 0 && d == 0) {
            std::cout << 0 << "\n";
            return 0;
        }
    }
    
    std::vector<Z> dp(1 << n);
    dp[(1 << n) - 1] = 1;
    
    for (int c = 1; c < m; c++) {
        for (int i = 1; i < (1 << n); i *= 2) {
            for (int j = 0; j < (1 << n); j += 2 * i) {
                for (int k = 0; k < i; k++) {
                    dp[j + k] += dp[i + j + k];
                }
            }
        }
        std::vector<Z> g(1 << n);
        for (auto [s, t, col] : trans) {
            bool ok = true;
            for (auto [x, y, d] : rest[c]) {
                int cx = std::__lg(col & ((1 << (x + 1)) - 1));
                int cy = std::__lg(col & ((1 << (y + 1)) - 1));
                if ((cx != cy) != d) {
                    ok = false;
                }
            }
            if (ok) {
                g[t] += dp[s];
            }
        }
        dp = std::move(g);
    }
    auto ans = std::accumulate(dp.begin(), dp.end(), Z(0));
    std::cout << ans << "\n";
    
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3820kb

input:

3 5 3
3 2 3 0
4 1 2 0
5 1 3 0

output:

19

result:

ok 1 number(s): "19"

Test #2:

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

input:

5 10 10
9 3 4 1
2 4 5 0
7 2 3 0
9 2 3 0
6 3 5 0
6 2 4 1
2 4 5 0
1 1 3 1
7 2 4 0
10 2 3 0

output:

1514

result:

ok 1 number(s): "1514"

Test #3:

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

input:

5 10 20
8 4 5 0
2 2 5 1
8 4 5 0
10 3 5 0
7 1 3 1
1 2 4 1
6 3 5 1
10 3 5 0
4 1 5 1
7 3 4 1
2 2 4 1
8 3 4 0
9 3 5 0
5 2 5 1
9 4 5 0
9 1 2 0
6 1 5 1
8 3 5 0
2 2 4 1
8 3 5 0

output:

28131

result:

ok 1 number(s): "28131"

Test #4:

score: 0
Accepted
time: 3ms
memory: 3580kb

input:

10 100 200
95 5 7 0
7 4 6 1
62 9 10 0
32 5 8 1
31 2 6 1
75 7 9 1
1 4 7 1
18 7 10 1
75 1 8 1
87 6 9 1
44 7 8 1
68 6 9 1
95 4 6 0
34 1 2 1
70 1 6 1
31 5 9 1
15 6 10 1
48 5 8 1
51 3 7 1
39 5 9 1
23 2 3 1
7 8 9 1
84 7 10 1
13 4 9 1
18 3 6 1
59 9 10 0
31 8 10 1
6 7 9 1
76 3 10 1
41 5 6 0
33 3 4 1
96 1 10...

output:

655333622

result:

ok 1 number(s): "655333622"

Test #5:

score: 0
Accepted
time: 3ms
memory: 3884kb

input:

10 200 200
106 9 10 0
93 4 10 1
199 3 7 0
73 2 9 1
105 8 9 0
38 9 10 1
73 8 10 1
153 3 9 1
123 2 5 1
159 7 9 0
154 5 7 1
162 3 7 0
113 1 5 1
131 7 9 1
67 4 6 1
178 6 10 0
157 7 9 0
147 9 10 0
154 7 10 0
123 3 4 1
39 8 10 1
139 2 9 1
191 9 10 0
36 4 5 1
17 2 8 1
124 3 7 1
9 9 10 1
71 9 10 1
181 7 8 0...

output:

552037151

result:

ok 1 number(s): "552037151"

Test #6:

score: 0
Accepted
time: 2ms
memory: 3720kb

input:

10 300 200
252 1 5 0
48 9 10 1
18 9 10 1
233 9 10 0
195 2 9 1
125 2 5 1
263 7 9 1
24 1 6 1
258 2 10 1
272 8 10 1
76 5 7 1
147 1 7 1
93 9 10 1
30 6 9 1
10 1 10 1
56 2 10 1
93 8 9 1
206 6 9 1
65 1 9 1
226 3 5 0
88 7 8 1
151 3 4 1
292 9 10 0
129 2 3 1
292 9 10 0
180 7 10 1
4 5 10 1
10 9 10 1
151 4 7 1
...

output:

4494096

result:

ok 1 number(s): "4494096"

Test #7:

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

input:

10 500 300
210 4 7 1
341 8 9 0
371 2 5 0
21 4 10 1
370 8 9 0
368 1 6 0
395 7 9 0
287 6 10 1
299 3 7 1
379 1 9 1
164 4 10 1
390 7 9 0
455 6 9 0
208 8 10 1
402 3 10 0
112 8 10 1
279 3 10 1
180 7 10 1
456 2 6 0
121 5 6 1
312 5 7 0
335 8 10 0
318 2 10 1
497 8 10 0
108 8 9 0
247 3 6 1
155 5 6 1
308 1 2 0...

output:

705403853

result:

ok 1 number(s): "705403853"

Test #8:

score: 0
Accepted
time: 30ms
memory: 3948kb

input:

12 500 300
115 3 10 1
152 10 12 1
89 8 12 1
276 4 7 0
467 6 7 0
405 5 9 0
189 4 9 1
197 1 3 1
341 7 8 0
67 7 8 1
266 2 6 1
78 8 12 1
317 11 12 0
417 8 10 0
380 2 8 0
255 2 5 1
80 7 9 1
317 5 11 1
470 5 9 0
373 3 4 0
413 4 10 0
393 9 12 0
362 8 10 1
42 7 12 1
486 3 5 0
229 1 5 0
224 6 7 0
55 3 10 1
4...

output:

378086467

result:

ok 1 number(s): "378086467"

Test #9:

score: 0
Accepted
time: 36ms
memory: 3800kb

input:

12 500 500
54 11 12 1
325 10 11 0
83 2 3 1
148 3 10 1
165 3 11 1
16 11 12 1
363 8 10 1
78 11 12 1
258 4 12 1
237 8 11 1
403 2 10 1
354 1 9 1
234 4 7 1
454 9 11 0
160 11 12 1
393 1 3 0
375 9 11 0
494 1 3 0
200 6 12 1
414 11 12 0
217 9 10 0
92 5 9 1
172 5 6 1
110 8 12 1
339 4 12 1
429 2 4 0
29 10 11 1...

output:

948753642

result:

ok 1 number(s): "948753642"

Test #10:

score: 0
Accepted
time: 196ms
memory: 4940kb

input:

14 500 500
362 4 12 1
225 5 9 1
428 5 9 1
101 8 10 1
488 5 9 0
249 11 14 1
232 2 6 1
220 4 10 1
20 7 13 1
154 4 13 1
480 8 14 0
9 2 4 1
201 7 10 1
174 10 11 0
169 13 14 0
256 10 12 1
403 11 13 0
492 10 14 0
167 6 12 1
123 11 13 1
471 9 10 0
77 5 9 1
51 6 10 1
411 11 14 1
422 11 13 0
7 1 7 1
284 5 11...

output:

103280588

result:

ok 1 number(s): "103280588"

Test #11:

score: 0
Accepted
time: 101ms
memory: 4892kb

input:

14 500 0

output:

750061283

result:

ok 1 number(s): "750061283"

Test #12:

score: 0
Accepted
time: 97ms
memory: 4960kb

input:

14 495 0

output:

662120858

result:

ok 1 number(s): "662120858"

Test #13:

score: 0
Accepted
time: 95ms
memory: 4828kb

input:

14 490 0

output:

456608006

result:

ok 1 number(s): "456608006"

Test #14:

score: 0
Accepted
time: 103ms
memory: 4796kb

input:

14 500 5
123 7 12 1
24 13 14 1
170 6 13 1
304 2 8 1
475 10 11 0

output:

715116697

result:

ok 1 number(s): "715116697"

Test #15:

score: 0
Accepted
time: 103ms
memory: 4788kb

input:

14 500 10
237 5 9 1
36 3 14 1
470 5 13 1
315 4 6 1
28 9 12 1
220 11 14 0
160 9 12 1
312 10 11 0
72 7 12 1
230 8 11 0

output:

434022866

result:

ok 1 number(s): "434022866"

Test #16:

score: 0
Accepted
time: 104ms
memory: 4940kb

input:

14 500 15
339 5 10 1
326 4 7 1
421 12 14 0
225 13 14 1
307 1 3 0
285 2 4 0
33 8 10 1
226 2 3 0
478 13 14 1
347 5 11 1
138 5 13 1
141 9 14 1
417 2 8 1
172 6 11 1
222 7 14 1

output:

268520991

result:

ok 1 number(s): "268520991"

Test #17:

score: 0
Accepted
time: 106ms
memory: 4780kb

input:

14 500 20
357 5 14 1
296 10 14 1
490 9 10 0
221 11 12 1
490 12 13 0
469 5 13 1
93 2 8 1
482 12 14 0
461 2 7 1
152 2 13 1
34 8 14 1
60 9 12 1
195 4 5 0
1 6 8 1
3 5 11 1
129 11 13 1
124 13 14 1
434 11 13 0
141 4 5 1
80 6 12 1

output:

691528902

result:

ok 1 number(s): "691528902"

Test #18:

score: 0
Accepted
time: 118ms
memory: 4824kb

input:

14 500 100
85 13 14 0
130 2 7 0
38 5 10 0
450 1 2 1
103 8 10 0
410 11 14 1
39 10 14 0
29 3 4 0
98 9 11 0
226 6 9 1
17 5 6 0
475 9 12 1
337 12 13 1
42 10 11 0
457 8 10 1
49 1 2 0
222 9 13 0
105 7 11 0
403 6 8 1
151 2 8 0
13 11 12 0
483 10 14 1
304 5 9 1
197 5 14 0
58 4 7 0
482 1 12 1
331 12 13 1
398 ...

output:

0

result:

ok 1 number(s): "0"

Test #19:

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

input:

14 498 200
457 10 13 0
163 6 10 0
23 2 5 0
109 5 8 0
113 12 14 0
294 10 12 0
1 10 14 0
451 1 2 0
275 1 13 0
345 10 14 0
171 2 9 0
392 8 11 0
184 13 14 0
328 10 11 0
84 10 13 0
238 6 12 0
306 6 13 0
56 8 14 0
404 10 14 0
90 3 10 0
446 12 14 0
303 9 11 0
71 11 12 0
362 10 13 0
405 13 14 1
258 4 13 0
1...

output:

0

result:

ok 1 number(s): "0"

Test #20:

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

input:

14 497 300
265 5 12 0
368 6 14 0
400 3 10 0
408 13 14 1
494 9 11 1
8 13 14 0
132 10 14 0
203 4 10 0
86 13 14 0
96 3 9 0
39 11 14 0
439 8 9 0
161 1 13 0
264 1 7 0
176 8 10 0
8 10 12 0
299 2 13 0
285 1 13 0
392 7 8 1
143 11 13 0
84 10 11 1
270 1 9 0
311 8 10 0
39 5 10 0
282 4 11 0
45 9 10 0
465 12 14 ...

output:

0

result:

ok 1 number(s): "0"

Test #21:

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

input:

14 499 500
349 7 10 0
440 11 13 0
391 5 11 0
461 8 10 1
172 12 14 0
139 5 10 0
79 3 4 0
456 10 11 0
276 11 14 0
484 5 6 1
178 11 13 0
295 8 11 0
384 3 8 0
112 9 11 0
170 3 7 0
490 12 14 1
243 7 9 0
360 4 7 0
302 10 12 0
266 5 8 0
350 8 12 0
282 7 12 0
480 7 11 1
312 10 13 0
356 13 14 0
277 4 5 0
245...

output:

0

result:

ok 1 number(s): "0"

Test #22:

score: 0
Accepted
time: 102ms
memory: 4840kb

input:

14 500 3
2 1 2 0
2 2 3 0
2 1 3 1

output:

0

result:

ok 1 number(s): "0"

Test #23:

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

input:

1 500 0

output:

1

result:

ok 1 number(s): "1"

Test #24:

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

input:

4 2 0

output:

17

result:

ok 1 number(s): "17"

Extra Test:

score: 0
Extra Test Passed