QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#436722#8770. Comparatormendicillin2AC ✓56ms114772kbC++203.9kb2024-06-09 02:50:352024-06-09 02:50:36

Judging History

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

  • [2024-06-09 02:50:36]
  • 评测
  • 测评结果:AC
  • 用时:56ms
  • 内存:114772kb
  • [2024-06-09 02:50:35]
  • 提交

answer

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

template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

const string BINOPS = "^|&=";
int op(int p, int x, int y) {
    if (p == 0) return x ^ y;
    else if (p == 1) return x | y;
    else if (p == 2) return x & y;
    else if (p == 3) return 15 ^ x ^ y;
    assert(false);
}

int evaluate(string expr) {
    expr.push_back(char(0));
    int cur = 0;
    auto evaluate = [&](auto&& self, int p) -> int {
        if (p < 4) {
            // p-th binary operator
            int res = self(self, p+1);
            while (expr[cur] == BINOPS[p]) {
                cur++;
                auto rres = self(self, p+1);
                res = op(p, res, rres);
            }
            return res;
        } else if (p == 4) {
            char c = expr[cur++];
            if (c == 'x') {
                return (1 << 1) | (1 << 3);
            } else if (c == 'y') {
                return (1 << 2) | (1 << 3);
            } else if (c == '0') {
                return 0;
            } else if (c == '1') {
                return 15;
            } else if (c == '!') {
                return 15 ^ self(self, 4);
            } else if (c == '(') {
                int res = self(self, 0);
                cur++; // Skip ')'
                return res;
            } else {
                cerr << "Unexpected character: " << c << endl;
                assert(false);
            }
        } else assert(false);
    };
    return evaluate(evaluate, 0);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(20);

    int N, K;
    cin >> N >> K;

    using Comb = array<int, 4>; // (a, b, x, y)
    auto encode = [&](int a, int b, int x, int y) -> int {
        return a * (K*2*2)
             + b * (2*2)
             + x * 2
             + y;
    };
    V<bool> seen(K*K*2*2, false);
    V<pair<Comb, int>> interesting;
    for (int i = 0; i < N; i++) {
        int a, b, r;
        string expr;
        cin >> a >> b >> expr >> r;
        a--, b--;
        auto result = evaluate(expr);

        for (int x = 0; x < 2; x++) {
            for (int y = 0; y < 2; y++) {
                int result_xy = (result >> (x + 2*y)) & 1;
                if (result_xy) {
                    int e = encode(a, b, x, y);
                    if (!seen[e]) {
                        seen[e] = true;
                        interesting.emplace_back(Comb{a, b, x, y}, r);
                    }
                }
            }
        }
    }
    int R_end;
    cin >> R_end;

    auto compute_f = [&](int x, int y) -> int {
        for (const auto& [inp, r] : interesting) {
            const auto& [a, b, dx, dy] = inp;
            if (((x >> a) & 1) == dx && ((y >> b) & 1) == dy) {
                return r;
            }
        }
        return R_end;
    };

    using Bitset = bitset<1 << 10>;

    const int M = 1 << K;
    V<Bitset> fxy(M);
    V<Bitset> fyx(M);
    for (int x = 0; x < M; x++) {
        for (int y = 0; y < M; y++) {
            auto f = compute_f(x, y);
            fxy[x][y] = f;
            fyx[y][x] = f;
        }
    }

    cout << [&]() -> int {
        return ranges::count_if(ranges::iota_view{0, M}, [&](int x) -> bool {
            return fxy[x][x];
        });
    }() << ' ' << [&]() -> int {
        int cnt = 0;
        for (int x = 0; x < M; x++) {
            for (int y = 0; y < M; y++) {
                cnt += (fxy[x][y] && fxy[y][x]);
            }
        }
        return cnt;
    }() << ' ' << [&]() -> int {
        int cnt = 0;
        for (int x = 0; x < M; x++) {
            for (int z = 0; z < M; z++) {
                if (!fxy[x][z]) {
                    cnt += int((fxy[x] & fyx[z]).count());
                }
            }
        }
        return cnt;
    }() << '\n';

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3520kb

input:

3 2
1 1 (x=0)&(y=1) 1
1 1 (x=1)&(y=(x^x)) 0
2 2 (x=1)|(y=0) 0
1

output:

0 0 0

result:

ok single line: '0 0 0'

Test #2:

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

input:

4 3
2 1 x=0&(y=1) 1
1 2 !x^!!y 0
2 3 ((x|1)=y)&1&1 1
3 1 !x&!x&!x 0
1

output:

3 25 52

result:

ok single line: '3 25 52'

Test #3:

score: 0
Accepted
time: 13ms
memory: 3640kb

input:

1413 3
1 3 0 0
3 3 !x 0
2 2 x=0 1
1 2 !y^0 1
2 3 (x^1) 0
3 2 ((!0)) 1
1 1 !!1=(y) 0
2 2 !(1^x)&y 1
3 2 (y)&1|!!1 0
3 1 !x=(y&y=y) 0
2 1 (((!1)^!x)) 1
2 3 !0=(0&y)=1&y 0
1 2 ((((!0)))|!1) 0
3 1 !(y=!1=x|(!x)) 0
1 1 ((((y=!y)))&!0) 0
2 3 ((y=1)^!1^!!1|0) 1
2 3 1|(!x)&!x|1|(x=1) 1
2 3 !0^!!!!y&0=(!1&!0...

output:

4 16 0

result:

ok single line: '4 16 0'

Test #4:

score: 0
Accepted
time: 47ms
memory: 3568kb

input:

181737 10
5 2 1 1
1 10 !1=!x 0
10 1 (1^x) 0
2 4 !1 1
10 8 y=(!1)^1 1
6 2 !((x&!x)) 1
1 10 !!((!x)|x) 1
7 10 (((0))) 0
7 3 !(1)^!x 0
10 4 (!1)&x 0
7 7 !y&!0 1
8 8 !1=(x)|1^1 1
2 6 ((!!!x)) 0
7 2 1 1
2 2 y=1=0 0
6 3 (!0) 0
6 4 0 0
1 1 (!1) 1
1 8 y 1
3 5 !x|!x^!1 0
4 7 (!0) 0
3 4 !1&1&!1|!0 1
2 7 ((0|1...

output:

1024 1048576 0

result:

ok single line: '1024 1048576 0'

Test #5:

score: 0
Accepted
time: 27ms
memory: 114772kb

input:

1 3
1 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...

output:

4 16 0

result:

ok single line: '4 16 0'

Test #6:

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

input:

1 1
1 1 x^y|1 0
1

output:

1 1 0

result:

ok single line: '1 1 0'

Test #7:

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

input:

1 1
1 1 x&y|1 0
1

output:

0 0 0

result:

ok single line: '0 0 0'

Test #8:

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

input:

1 1
1 1 x=y|1 0
1

output:

0 0 0

result:

ok single line: '0 0 0'

Test #9:

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

input:

2 2
1 2 !x&!y 1
1 1 !x&y 0
1

output:

4 12 2

result:

ok single line: '4 12 2'

Test #10:

score: 0
Accepted
time: 49ms
memory: 3760kb

input:

2 10
9 8 ((((!((!x=1))^(!1&(x|x|!y))&((!y&!x=(x=y)))&!((((x=1))&(0=(y))^(!!(!!x^1=x)&(x)^y&1=!x&1=(((!0^(1)^1))^!(((((y))|x|!y))))^!!0=!y&(0)|(y=x&!y&y=x)))=((((!!y&!!0|!0^!0)=!x))))^0&(((!1=!(!x)))|(((((x=1|!y|y)=(!1^1^0^(0)))=!0^1)))=(!(!1^(((((!1)^!x^0))))=(1)^((((y=((x))|(0)|(!1^1)))|(!!!y))=((!...

output:

0 0 0

result:

ok single line: '0 0 0'

Test #11:

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

input:

4 3
1 1 !!!!!!x 0
2 1 !!y 1
1 2 !!!!!x 1
2 2 !!!x 0
1

output:

4 16 0

result:

ok single line: '4 16 0'

Test #12:

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

input:

1 1
1 1 ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((...

output:

1 1 0

result:

ok single line: '1 1 0'

Test #13:

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

input:

200000 10
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10 !x^!y 1
3 10...

output:

512 262144 134217728

result:

ok single line: '512 262144 134217728'

Test #14:

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

input:

10 3
3 1 (!x) 1
3 2 !1&x&1&!y 1
2 1 ((x)&y) 1
1 3 0 0
2 2 !1&0=y&0 1
3 3 (!y)^y 1
2 1 0|((!y)) 0
3 2 x 0
2 2 (y|1^x) 0
2 1 ((!0)|y) 0
1

output:

8 64 0

result:

ok single line: '8 64 0'

Test #15:

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

input:

0 3
1

output:

8 64 0

result:

ok single line: '8 64 0'