QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#866539 | #9980. Boolean Function Reconstruction | 中国入民小学附属信息I队 (Zicheng Wang, Shubang Liu, Minkai Shao)# | TL | 1637ms | 269984kb | C++14 | 3.1kb | 2025-01-22 16:01:55 | 2025-01-22 16:01:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 14349000;
char st[32800];
bool p[1 << 15], t[maxn], t2[maxn], t3[maxn];
int f[maxn], c[maxn], mi[20], w[20], n;
pii pre[maxn];
inline void dec(int x, int *w)
{
for (int i = 0; i < n; i++) w[i] = x % 3, x /= 3;
}
inline int hsh(int *w)
{
int nw = 0;
for (int i = n - 1; ~i; i--) nw = nw * 3 + w[i];
return nw;
}
inline int hs2(int *w)
{
int nw = 0;
for (int i = 0; i < n; i++) nw |= ((w[i] == 2) << i);
return nw;
}
inline bool hs3(int *w)
{
bool nw = 0;
for (int i = 0; i < n; i++) nw |= (w[i] == 0);
return nw;
}
void solve(int s)
{
int x = pre[s].first, y = pre[s].second;
if (x == -1 && y == -1) return;
if (y == -1) return solve(x), void();
char ch = (char)('a' + c[s]);
if (x == -1)
{
if (!t2[y]) return cout << ch, void();
cout << '(' << ch << '&';
solve(y); cout << ')'; return;
}
cout << '('; solve(x); cout << '|';
if (t2[y]) cout << '(' << ch << '&', solve(y), cout << "))";
else cout << ch << ')';
}
void work()
{
cin >> n >> st;
int N = 1 << n, M = 1;
mi[0] = 1; for (int i = 1; i <= n; i++) mi[i] = mi[i - 1] * 3; M = mi[n];
for (int s = 0; s < N; s++) p[s] = 0;
for (int s = 0; s < M; s++) f[s] = 0;
for (int s = 0; s < N; s++)
{
bool F = (st[s] == '1'), F2 = 1;
for (int i = 0; i < n; i++)
if ((s >> i) & 1) F2 &= (st[s ^ (1 << i)] == '0');
if (F && F2) p[s] = t[s] = 1;
if (!F && !F2) return cout << "No\n", void();
}
if (p[0]) return cout << "Yes\nT\n", void();
if (st[N - 1] != '1') return cout << "Yes\nF\n", void();
for (int s = M - 1; ~s; s--)
{
dec(s, w); t[s] = t2[s] = 0, t3[s] = hs3(w); bool F = 0;
for (int i = 0; i < n; i++)
if (!w[i])
{
t[s] |= t[s + mi[i]], t2[s] |= t2[s + mi[i]];
t[s] |= t[s + (mi[i] << 1)], t2[s] |= t[s + (mi[i] << 1)];
F = 1; break;
}
if (!F) t[s] = p[hs2(w)];
}
for (int s = M - 1; ~s; s--)
{
bool Fl = 0;
dec(s, w); f[s] = 0x3f3f3f3f, pre[s] = make_pair(-1, -1);
for (int i = 0; i < n; i++)
if (!w[i])
{
int res = f[s + mi[i]], F2 = t[s + mi[i]];
res += f[s + (mi[i] << 1)]; if (t[s + (mi[i] << 1)]) res += t3[s + (mi[i] << 1)] + F2;
Fl = 1;
if (f[s] > res)
{
f[s] = res, pre[s] = make_pair(-1, -1), c[s] = i;
if (t[s + mi[i]]) pre[s].first = s + mi[i];
if (t[s + (mi[i] << 1)]) pre[s].second = s + (mi[i] << 1);
}
}
if (!Fl) f[s] = 0;
}
cout << "Yes\n";
solve(0); cout << "\n";
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
int T; cin >> T;
while (T--) work();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 13928kb
input:
7 2 0001 2 0111 2 1111 3 00010111 1 10 2 0101 5 00000000000000000000000000000001
output:
Yes (a&b) Yes (b|a) Yes T Yes ((b&c)|(a&(c|b))) No Yes a Yes (a&(b&(c&(d&e))))
result:
ok 7 lines, tightest: 4 out of 14 (7 test cases)
Test #2:
score: 0
Accepted
time: 0ms
memory: 13928kb
input:
4 1 00 1 10 1 01 1 11
output:
Yes F No Yes a Yes T
result:
ok 4 lines, tightest: 0 out of 11 (4 test cases)
Test #3:
score: 0
Accepted
time: 1ms
memory: 13928kb
input:
16 2 0000 2 1000 2 0100 2 1100 2 0010 2 1010 2 0110 2 1110 2 0001 2 1001 2 0101 2 1101 2 0011 2 1011 2 0111 2 1111
output:
Yes F No No No No No No No Yes (a&b) No Yes a No Yes b No Yes (b|a) Yes T
result:
ok 16 lines, tightest: 1 out of 12 (16 test cases)
Test #4:
score: 0
Accepted
time: 1ms
memory: 14056kb
input:
256 3 00000000 3 10000000 3 01000000 3 11000000 3 00100000 3 10100000 3 01100000 3 11100000 3 00010000 3 10010000 3 01010000 3 11010000 3 00110000 3 10110000 3 01110000 3 11110000 3 00001000 3 10001000 3 01001000 3 11001000 3 00101000 3 10101000 3 01101000 3 11101000 3 00011000 3 10011000 3 01011000...
output:
Yes F No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No ...
result:
ok 256 lines, tightest: 4 out of 14 (256 test cases)
Test #5:
score: 0
Accepted
time: 7ms
memory: 13936kb
input:
65536 4 0000000000000000 4 1000000000000000 4 0100000000000000 4 1100000000000000 4 0010000000000000 4 1010000000000000 4 0110000000000000 4 1110000000000000 4 0001000000000000 4 1001000000000000 4 0101000000000000 4 1101000000000000 4 0011000000000000 4 1011000000000000 4 0111000000000000 4 1111000...
output:
Yes F No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No ...
result:
ok 65536 lines, tightest: 8 out of 18 (65536 test cases)
Test #6:
score: 0
Accepted
time: 0ms
memory: 14056kb
input:
168 4 0000000000000000 4 0000000000000001 4 0000000000000011 4 0000000000000101 4 0000000000000111 4 0000000000001111 4 0000000000010001 4 0000000000010011 4 0000000000010101 4 0000000000010111 4 0000000000011111 4 0000000000110011 4 0000000000110111 4 0000000000111111 4 0000000001010101 4 000000000...
output:
Yes F Yes (a&(b&(c&d))) Yes (b&(c&d)) Yes (a&(c&d)) Yes (c&((b&d)|(a&d))) Yes (c&d) Yes (a&(b&d)) Yes (b&((c&d)|(a&d))) Yes (a&((c&d)|(b&d))) Yes (d&((b&c)|(a&(c|b)))) Yes (d&(c|(a&b))) Yes (b&d) Yes (d&(b|(a&c))) Yes ((c&d)|(b&d)) Yes (a&d) Yes (d&(a|(b&c))) Yes ((c&d)|(a&d)) Yes ((b&d)|(a&d)) Yes ...
result:
ok 168 lines, tightest: 8 out of 18 (168 test cases)
Test #7:
score: 0
Accepted
time: 45ms
memory: 13932kb
input:
7581 5 00000000000000000000000000000000 5 00000000000000000000000000000001 5 00000000000000000000000000000011 5 00000000000000000000000000000101 5 00000000000000000000000000000111 5 00000000000000000000000000001111 5 00000000000000000000000000010001 5 00000000000000000000000000010011 5 0000000000000...
output:
Yes F Yes (a&(b&(c&(d&e)))) Yes (b&(c&(d&e))) Yes (a&(c&(d&e))) Yes (c&(d&((b&e)|(a&e)))) Yes (c&(d&e)) Yes (a&(b&(d&e))) Yes (b&(d&((c&e)|(a&e)))) Yes (a&(d&((c&e)|(b&e)))) Yes (d&(e&((b&c)|(a&(c|b))))) Yes (d&(e&(c|(a&b)))) Yes (b&(d&e)) Yes (d&(e&(b|(a&c)))) Yes (d&((c&e)|(b&e))) Yes (a&(d&e)) Ye...
result:
ok 7581 lines, tightest: 18 out of 26 (7581 test cases)
Test #8:
score: 0
Accepted
time: 436ms
memory: 103800kb
input:
14 1 01 2 0111 3 00010111 4 0001011101111111 5 00000001000101110001011101111111 6 0000000100010111000101110111111100010111011111110111111111111111 7 00000000000000010000000100010111000000010001011100010111011111110000000100010111000101110111111100010111011111110111111111111111 8 00000000000000010000...
output:
Yes a Yes (b|a) Yes ((b&c)|(a&(c|b))) Yes (((c&d)|(b&(d|c)))|(a&((d|c)|b))) Yes (((c&(d&e))|(b&((d&e)|(c&(e|d)))))|(a&(((d&e)|(c&(e|d)))|(b&((e|d)|c))))) Yes ((((d&(e&f))|(c&((e&f)|(d&(f|e)))))|(b&(((e&f)|(d&(f|e)))|(c&((f|e)|d)))))|(a&((((e&f)|(d&(f|e)))|(c&((f|e)|d)))|(b&(((f|e)|d)|c))))) Yes ((((...
result:
ok 14 lines, tightest: 68 out of 74 (14 test cases)
Test #9:
score: 0
Accepted
time: 430ms
memory: 102204kb
input:
14 1 01 2 0001 3 00010111 4 0000000100010111 5 00000001000101110001011101111111 6 0000000000000001000000010001011100000001000101110001011101111111 7 00000000000000010000000100010111000000010001011100010111011111110000000100010111000101110111111100010111011111110111111111111111 8 00000000000000000000...
output:
Yes a Yes (a&b) Yes ((b&c)|(a&(c|b))) Yes ((b&(c&d))|(a&((c&d)|(b&(d|c))))) Yes (((c&(d&e))|(b&((d&e)|(c&(e|d)))))|(a&(((d&e)|(c&(e|d)))|(b&((e|d)|c))))) Yes (((c&(d&(e&f)))|(b&((d&(e&f))|(c&((e&f)|(d&(f|e)))))))|(a&(((d&(e&f))|(c&((e&f)|(d&(f|e)))))|(b&(((e&f)|(d&(f|e)))|(c&((f|e)|d))))))) Yes ((((...
result:
ok 14 lines, tightest: 68 out of 74 (14 test cases)
Test #10:
score: 0
Accepted
time: 437ms
memory: 101916kb
input:
14 1 00 2 0001 3 00000001 4 0000000100010111 5 00000000000000010000000100010111 6 0000000000000001000000010001011100000001000101110001011101111111 7 00000000000000000000000000000001000000000000000100000001000101110000000000000001000000010001011100000001000101110001011101111111 8 00000000000000000000...
output:
Yes F Yes (a&b) Yes (a&(b&c)) Yes ((b&(c&d))|(a&((c&d)|(b&(d|c))))) Yes ((b&(c&(d&e)))|(a&((c&(d&e))|(b&((d&e)|(c&(e|d))))))) Yes (((c&(d&(e&f)))|(b&((d&(e&f))|(c&((e&f)|(d&(f|e)))))))|(a&(((d&(e&f))|(c&((e&f)|(d&(f|e)))))|(b&(((e&f)|(d&(f|e)))|(c&((f|e)|d))))))) Yes (((c&(d&(e&(f&g))))|(b&((d&(e&(f...
result:
ok 14 lines, tightest: 33 out of 42 (14 test cases)
Test #11:
score: 0
Accepted
time: 442ms
memory: 104780kb
input:
14 1 00 2 0000 3 00000001 4 0000000000000001 5 00000000000000010000000100010111 6 0000000000000000000000000000000100000000000000010000000100010111 7 00000000000000000000000000000001000000000000000100000001000101110000000000000001000000010001011100000001000101110001011101111111 8 00000000000000000000...
output:
Yes F Yes F Yes (a&(b&c)) Yes (a&(b&(c&d))) Yes ((b&(c&(d&e)))|(a&((c&(d&e))|(b&((d&e)|(c&(e|d))))))) Yes ((b&(c&(d&(e&f))))|(a&((c&(d&(e&f)))|(b&((d&(e&f))|(c&((e&f)|(d&(f|e))))))))) Yes (((c&(d&(e&(f&g))))|(b&((d&(e&(f&g)))|(c&((e&(f&g))|(d&((f&g)|(e&(g|f)))))))))|(a&(((d&(e&(f&g)))|(c&((e&(f&g))|...
result:
ok 14 lines, tightest: 0 out of 11 (14 test cases)
Test #12:
score: 0
Accepted
time: 960ms
memory: 269984kb
input:
1 15 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000...
output:
Yes ((((((((h&(i&(j&(k&(l&(m&(n&o)))))))|(g&((i&(j&(k&(l&(m&(n&o))))))|(h&((j&(k&(l&(m&(n&o)))))|(i&((k&(l&(m&(n&o))))|(j&((l&(m&(n&o)))|(k&((m&(n&o))|(l&((n&o)|(m&(o|n)))))))))))))))|(f&(((i&(j&(k&(l&(m&(n&o))))))|(h&((j&(k&(l&(m&(n&o)))))|(i&((k&(l&(m&(n&o))))|(j&((l&(m&(n&o)))|(k&((m&(n&o))|(l&((...
result:
ok 1 lines, tightest: 12868 out of 16394 (1 test case)
Test #13:
score: 0
Accepted
time: 972ms
memory: 269964kb
input:
1 15 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000100000000000000010000000100010111000000000000000000000000000000000000000...
output:
Yes (((((((((i&(j&(k&(l&(m&(n&o))))))|(h&((j&(k&(l&(m&(n&o)))))|(i&((k&(l&(m&(n&o))))|(j&((l&(m&(n&o)))|(k&((m&(n&o))|(l&((n&o)|(m&(o|n)))))))))))))|(g&(((j&(k&(l&(m&(n&o)))))|(i&((k&(l&(m&(n&o))))|(j&((l&(m&(n&o)))|(k&((m&(n&o))|(l&((n&o)|(m&(o|n)))))))))))|(h&(((k&(l&(m&(n&o))))|(j&((l&(m&(n&o)))|...
result:
ok 1 lines, tightest: 11438 out of 16394 (1 test case)
Test #14:
score: 0
Accepted
time: 968ms
memory: 269968kb
input:
1 15 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
Yes (((((((g&(h&(i&(j&(k&(l&(m&(n&o))))))))|(f&((h&(i&(j&(k&(l&(m&(n&o)))))))|(g&((i&(j&(k&(l&(m&(n&o))))))|(h&((j&(k&(l&(m&(n&o)))))|(i&((k&(l&(m&(n&o))))|(j&((l&(m&(n&o)))|(k&((m&(n&o))|(l&((n&o)|(m&(o|n)))))))))))))))))|(e&(((h&(i&(j&(k&(l&(m&(n&o)))))))|(g&((i&(j&(k&(l&(m&(n&o))))))|(h&((j&(k&(l...
result:
ok 1 lines, tightest: 11438 out of 16394 (1 test case)
Test #15:
score: 0
Accepted
time: 982ms
memory: 269880kb
input:
1 15 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
Yes ((((((f&(g&(h&(i&(j&(k&(l&(m&(n&o)))))))))|(e&((g&(h&(i&(j&(k&(l&(m&(n&o))))))))|(f&((h&(i&(j&(k&(l&(m&(n&o)))))))|(g&((i&(j&(k&(l&(m&(n&o))))))|(h&((j&(k&(l&(m&(n&o)))))|(i&((k&(l&(m&(n&o))))|(j&((l&(m&(n&o)))|(k&((m&(n&o))|(l&((n&o)|(m&(o|n)))))))))))))))))))|(d&(((g&(h&(i&(j&(k&(l&(m&(n&o))))...
result:
ok 1 lines, tightest: 8006 out of 16394 (1 test case)
Test #16:
score: 0
Accepted
time: 1637ms
memory: 14056kb
input:
65536 6 0000001101111111000111111111111101111111111111111111111111111111 6 0000000000000000000100110011011100000000000000000001001100111111 6 0101010101110111011101111111111101110111111111111111111111111111 6 0000001100000011000000110001011100011111001111110011111100111111 6 000000010001011100000001...
output:
Yes (((((e&f)|(d&(f|e)))|(c&((f|e)|d)))|(b&((f|d)|c)))|(a&((f|d)|(b&e)))) Yes (e&(((b&d)|(c&(b|(d&f))))|(a&(b|(c&d))))) Yes (((a|(e&f))|(d&(f|e)))|(b&((f|e)|d))) Yes ((c&(f|(a&(d&e))))|(b&(((c|(e&f))|(d&f))|(a&(f|(d&e)))))) Yes ((b&((d&f)|(c&(f|d))))|(a&((c&(f|d))|(b&((f|d)|c))))) Yes (((c&(d&e))|(b...
result:
ok 65536 lines, tightest: 33 out of 42 (65536 test cases)
Test #17:
score: -100
Time Limit Exceeded
input:
65536 7 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 7 00000001000100010001000101110111000100010111011101110111011111110001000101110111011101110111111100010001011101110111011111111111 7 000000010001001100000001001101...
output:
Yes (a&(b&(c&(d&(e&(f&g)))))) Yes (((b&(e&(g|f)))|(d&((b&(g|f))|(e&((b|(f&g))|(c&(g|f)))))))|(a&(((e&(g|f))|(d&((g|f)|e)))|(b&((((g|f)|e)|d)|c))))) Yes (((d&(f&(c|(e&g))))|(b&(((e&f)|(d&(f|e)))|(c&((f|(e&g))|d)))))|(a&(((d&f)|(b&(f|d)))|(c&((b|(e&f))|(d&e)))))) Yes ((d&((((f&g)|(e&f))|(b&f))|(a&f)))...