QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#439324 | #8770. Comparator | PetroTarnavskyi | WA | 148ms | 14700kb | C++20 | 3.5kb | 2024-06-11 19:37:35 | 2024-06-11 19:37:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int N = 200'447;
const int K = 10;
VI f[N];
string s[N];
int res[N];
int idxA[N];
int idxB[N];
int val[K][K][2][2];
int h[1 << K][1 << K];
bitset<(1 << K)> bts[1 << K];
bitset<(1 << K)> rbts[1 << K];
void upd(int& a, int b)
{
a = min(a, b);
}
bool g(bool x, bool y, char c)
{
if (c == '=')
return x == y;
if (c == '&')
return x && y;
if (c == '|')
return x || y;
return x ^ y;
}
bool eval(int i, int l, int r, bool x, bool y)
{
//cerr << s[i] << ' ' << l << ' ' << r << '\n';
bool nt = false;
VI v;
string op = "";
FOR (j, l, r)
{
if (s[i][j] == '!')
nt ^= 1;
else if (s[i][j] == '(')
{
v.PB(eval(i, j + 1, f[i][j], x, y) ^ nt);
nt = 0;
j = f[i][j];
}
else if (s[i][j] == 'x')
{
v.PB(x ^ nt);
nt = 0;
}
else if (s[i][j] == 'y')
{
v.PB(y ^ nt);
nt = 0;
}
else if (s[i][j] == '0' || s[i][j] == '1')
{
v.PB(s[i][j] - '0');
}
else
{
//cerr << s[i][j] << '\n';
assert(s[i][j] == '=' || s[i][j] == '&' || s[i][j] == '|' || s[i][j] == '^');
op += s[i][j];
}
}
string all = "=&|^";
FOR (t, 0, 4)
{
int last = v[0];
VI v2;
string op2 = "";
FOR (j, 0, SZ(op))
{
if (op[j] == all[t])
{
last = g(last, v[j + 1], op[j]);
}
else
{
v2.PB(last);
op2 += op[j];
last = v[j + 1];
}
}
v2.PB(last);
v = v2;
op = op2;
}
//cerr << SZ(v) << ' ' << SZ(op) << '\n';
assert(SZ(v) == 1);
return v[0];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
FOR (i, 0, k) FOR (j, 0, k) FOR (vi, 0, 2) FOR (vj, 0, 2) val[i][j][vi][vj] = n;
FOR (i, 0, n)
{
cin >> idxA[i] >> idxB[i];
idxA[i] = k - idxA[i];
idxB[i] = k - idxB[i];
cin >> s[i];
f[i].resize(SZ(s[i]));
VI st;
FOR (j, 0, SZ(s[i]))
{
if (s[i][j] == '(')
st.PB(j);
else if (s[i][j] == ')')
{
f[i][st.back()] = j;
st.pop_back();
}
}
FOR (vi, 0, 2)
{
FOR (vj, 0, 2)
{
int x = eval(i, 0, SZ(s[i]), vi, vj);
//cerr << x;
if (x)
upd(val[idxA[i]][idxB[i]][vi][vj], i);
}
}
//cerr << "\n";
cin >> res[i];
}
cin >> res[n];
//FOR (i, 0, k) FOR (j, 0, k) FOR (vi, 0, 2) FOR (vj, 0, 2)
// cerr << i << ' ' << j << ' ' << vi << ' ' << vj << ' ' << val[i][j][vi][vj] << '\n';
int a = 0;
int b = 0;
int c = 0;
FOR (mask1, 0, 1 << k)
{
FOR (mask2, 0, 1 << k)
{
int mn = n;
FOR (i, 0, k)
{
FOR (j, 0, k)
{
int vi = (mask1 >> i) & 1;
int vj = (mask2 >> j) & 1;
upd(mn, val[i][j][vi][vj]);
}
}
h[mask1][mask2] = res[mn];
if (mask1 == mask2 && h[mask1][mask2] == 1)
a++;
if (h[mask1][mask2])
bts[mask1][mask2] = rbts[mask2][mask1] = 1;
}
}
//b = (1 << k) * ((1 << k) - 1) / 2;
FOR (x, 0, 1 << k)
{
FOR (y, 0, 1 << k)
{
if (h[x][y] && h[y][x])
b++;
}
}
//b += 2 * a;
FOR (x, 0, 1 << k)
{
FOR (z, 0, 1 << k)
{
if (h[x][z] == 0)
{
c += (bts[x] & rbts[z]).count();
}
}
}
cout << a << ' ' << b << ' ' << c << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 10076kb
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: 9932kb
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: -100
Wrong Answer
time: 148ms
memory: 14700kb
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:
3 13 8
result:
wrong answer 1st lines differ - expected: '4 16 0', found: '3 13 8'