QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#875443#9980. Boolean Function Reconstructionucup-team5008#RE 90ms4224kbC++201.2kb2025-01-29 19:44:092025-01-29 19:44:10

Judging History

This is the latest submission verdict.

  • [2025-01-29 19:44:10]
  • Judged
  • Verdict: RE
  • Time: 90ms
  • Memory: 4224kb
  • [2025-01-29 19:44:09]
  • Submitted

answer

#include <cstdio>
#include <cassert>
#include <string>

const int N = 1 << 15;
int t, n;
bool ok = 1;
char s[N + 1];

std::string run(int ind = n - 1, int rest = 0) {
	if (ind == -1) {
		if (s[rest] == '0') return "F";
		return "T";
	}
	bool valuable = 0;
	for (int i = 0; i < 1 << ind; ++i) if (s[i | rest] != s[i | rest | 1 << ind]) {
		valuable = 1;
		if (s[i | rest] == '1') ok = 0;
	}
	if (!valuable) return run(ind - 1, rest);
	//printf("valubale %d\n", ind);
	std::string X = run(ind - 1, rest), Y = run(ind - 1, rest | 1 << ind);
	if (X == "T") return "T";
	if (Y == "F") return X;
	if (X == "F") {
		if (Y == "T") return std::string() + char('a' + ind);
		return '(' + Y + '&' + char('a' + ind) + ')';
	}
	if (Y == "T") return '(' + X + '|' + char('a' + ind) + ')';
	return '(' + X + "|(" + char('a' + ind) + '&' + Y + "))";
}

int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		scanf("%s", s);
		ok = 1;
		auto ans = run();
		if (ok) {
			printf("Yes\n");
			int op = 0;
			for (int i = 0; i < ans.size(); ++i) if (ans[i] == '&' || ans[i] == '|') ++op;
			assert(op <= (1 << n - 1) + 10);
			printf("%s\n", ans.c_str());
		} else printf("No\n");
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

7
2
0001
2
0111
2
1111
3
00010111
1
10
2
0101
5
00000000000000000000000000000001

output:

Yes
(a&b)
Yes
(a|b)
Yes
T
Yes
((a&b)|(c&(a|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: 3840kb

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: 0ms
memory: 3840kb

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
(a|b)
Yes
T

result:

ok 16 lines, tightest: 1 out of 12 (16 test cases)

Test #4:

score: 0
Accepted
time: 1ms
memory: 3968kb

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: 29ms
memory: 3968kb

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: 9 out of 18 (65536 test cases)

Test #6:

score: 0
Accepted
time: 1ms
memory: 3840kb

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
(((a|b)&c)&d)
Yes
(c&d)
Yes
((a&b)&d)
Yes
(((a&b)|(c&b))&d)
Yes
(((a&b)|(c&a))&d)
Yes
(((a&b)|(c&(a|b)))&d)
Yes
(((a&b)|c)&d)
Yes
(b&d)
Yes
((b|(c&(a|b)))&d)
Yes
((b|c)&d)
Yes
(a&d)
Yes
((a|(c&(a|b)))&d)
Yes
((a|c)&d)
Yes
((a|b)&d)
Yes
(((a|b)|...

result:

ok 168 lines, tightest: 9 out of 18 (168 test cases)

Test #7:

score: 0
Accepted
time: 7ms
memory: 3840kb

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
((((a|b)&c)&d)&e)
Yes
((c&d)&e)
Yes
(((a&b)&d)&e)
Yes
((((a&b)|(c&b))&d)&e)
Yes
((((a&b)|(c&a))&d)&e)
Yes
((((a&b)|(c&(a|b)))&d)&e)
Yes
((((a&b)|c)&d)&e)
Yes
((b&d)&e)
Yes
(((b|(c&(a|b)))&d)&e)
Yes
(((b|c)&d)&e)
Yes
((a&d)&e)
Yes
((...

result:

ok 7581 lines, tightest: 20 out of 26 (7581 test cases)

Test #8:

score: 0
Accepted
time: 1ms
memory: 3968kb

input:

14
1
01
2
0111
3
00010111
4
0001011101111111
5
00000001000101110001011101111111
6
0000000100010111000101110111111100010111011111110111111111111111
7
00000000000000010000000100010111000000010001011100010111011111110000000100010111000101110111111100010111011111110111111111111111
8
00000000000000010000...

output:

Yes
a
Yes
(a|b)
Yes
((a&b)|(c&(a|b)))
Yes
(((a&b)|(c&(a|b)))|(d&((a|b)|c)))
Yes
((((a&b)&c)|(d&((a&b)|(c&(a|b)))))|(e&(((a&b)|(c&(a|b)))|(d&((a|b)|c)))))
Yes
(((((a&b)&c)|(d&((a&b)|(c&(a|b)))))|(e&(((a&b)|(c&(a|b)))|(d&((a|b)|c)))))|(f&((((a&b)|(c&(a|b)))|(d&((a|b)|c)))|(e&(((a|b)|c)|d)))))
Yes
((((...

result:

ok 14 lines, tightest: 68 out of 74 (14 test cases)

Test #9:

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

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
((a&b)|(c&(a|b)))
Yes
(((a&b)&c)|(d&((a&b)|(c&(a|b)))))
Yes
((((a&b)&c)|(d&((a&b)|(c&(a|b)))))|(e&(((a&b)|(c&(a|b)))|(d&((a|b)|c)))))
Yes
(((((a&b)&c)&d)|(e&(((a&b)&c)|(d&((a&b)|(c&(a|b)))))))|(f&((((a&b)&c)|(d&((a&b)|(c&(a|b)))))|(e&(((a&b)|(c&(a|b)))|(d&((a|b)|c)))))))
Yes
((((...

result:

ok 14 lines, tightest: 68 out of 74 (14 test cases)

Test #10:

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

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
(((a&b)&c)|(d&((a&b)|(c&(a|b)))))
Yes
((((a&b)&c)&d)|(e&(((a&b)&c)|(d&((a&b)|(c&(a|b)))))))
Yes
(((((a&b)&c)&d)|(e&(((a&b)&c)|(d&((a&b)|(c&(a|b)))))))|(f&((((a&b)&c)|(d&((a&b)|(c&(a|b)))))|(e&(((a&b)|(c&(a|b)))|(d&((a|b)|c)))))))
Yes
((((((a&b)&c)&d)&e)|(f&((((a&b)&...

result:

ok 14 lines, tightest: 33 out of 42 (14 test cases)

Test #11:

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

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
((((a&b)&c)&d)|(e&(((a&b)&c)|(d&((a&b)|(c&(a|b)))))))
Yes
(((((a&b)&c)&d)&e)|(f&((((a&b)&c)&d)|(e&(((a&b)&c)|(d&((a&b)|(c&(a|b)))))))))
Yes
((((((a&b)&c)&d)&e)|(f&((((a&b)&c)&d)|(e&(((a&b)&c)|(d&((a&b)|(c&(a|b)))))))))|(g&(((((a&b)&c)&d)|(e&(((a&b)&c)|...

result:

ok 14 lines, tightest: 0 out of 11 (14 test cases)

Test #12:

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

input:

1
15
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000...

output:

Yes
((((((((((((((a&b)&c)&d)&e)&f)&g)&h)|(i&(((((((a&b)&c)&d)&e)&f)&g)|(h&((((((a&b)&c)&d)&e)&f)|(g&(((((a&b)&c)&d)&e)|(f&((((a&b)&c)&d)|(e&(((a&b)&c)|(d&((a&b)|(c&(a|b)))))))))))))))|(j&((((((((a&b)&c)&d)&e)&f)&g)|(h&((((((a&b)&c)&d)&e)&f)|(g&(((((a&b)&c)&d)&e)|(f&((((a&b)&c)&d)|(e&(((a&b)&c)|(d&((...

result:

ok 1 lines, tightest: 12868 out of 16394 (1 test case)

Test #13:

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

input:

1
15
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000100000000000000010000000100010111000000000000000000000000000000000000000...

output:

Yes
((((((((((((((a&b)&c)&d)&e)&f)&g)|(h&((((((a&b)&c)&d)&e)&f)|(g&(((((a&b)&c)&d)&e)|(f&((((a&b)&c)&d)|(e&(((a&b)&c)|(d&((a&b)|(c&(a|b)))))))))))))|(i&(((((((a&b)&c)&d)&e)&f)|(g&(((((a&b)&c)&d)&e)|(f&((((a&b)&c)&d)|(e&(((a&b)&c)|(d&((a&b)|(c&(a|b)))))))))))|(h&((((((a&b)&c)&d)&e)|(f&((((a&b)&c)&d)|...

result:

ok 1 lines, tightest: 11438 out of 16394 (1 test case)

Test #14:

score: 0
Accepted
time: 1ms
memory: 4096kb

input:

1
15
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

Yes
((((((((((((((a&b)&c)&d)&e)&f)&g)&h)&i)|(j&((((((((a&b)&c)&d)&e)&f)&g)&h)|(i&(((((((a&b)&c)&d)&e)&f)&g)|(h&((((((a&b)&c)&d)&e)&f)|(g&(((((a&b)&c)&d)&e)|(f&((((a&b)&c)&d)|(e&(((a&b)&c)|(d&((a&b)|(c&(a|b)))))))))))))))))|(k&(((((((((a&b)&c)&d)&e)&f)&g)&h)|(i&(((((((a&b)&c)&d)&e)&f)&g)|(h&((((((a&b...

result:

ok 1 lines, tightest: 11438 out of 16394 (1 test case)

Test #15:

score: 0
Accepted
time: 1ms
memory: 4224kb

input:

1
15
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

Yes
((((((((((((((a&b)&c)&d)&e)&f)&g)&h)&i)&j)|(k&(((((((((a&b)&c)&d)&e)&f)&g)&h)&i)|(j&((((((((a&b)&c)&d)&e)&f)&g)&h)|(i&(((((((a&b)&c)&d)&e)&f)&g)|(h&((((((a&b)&c)&d)&e)&f)|(g&(((((a&b)&c)&d)&e)|(f&((((a&b)&c)&d)|(e&(((a&b)&c)|(d&((a&b)|(c&(a|b)))))))))))))))))))|(l&((((((((((a&b)&c)&d)&e)&f)&g)&h...

result:

ok 1 lines, tightest: 8006 out of 16394 (1 test case)

Test #16:

score: 0
Accepted
time: 90ms
memory: 3840kb

input:

65536
6
0000001101111111000111111111111101111111111111111111111111111111
6
0000000000000000000100110011011100000000000000000001001100111111
6
0101010101110111011101111111111101110111111111111111111111111111
6
0000001100000011000000110001011100011111001111110011111100111111
6
000000010001011100000001...

output:

Yes
((((b&c)|(d&((a|b)|c)))|(e&(((a&b)|c)|d)))|(f&((((a|b)|c)|d)|e)))
Yes
(((((a&b)|(c&b))|(d&(b|(c&(a|b)))))&e)|(f&((((a&b)|(c&b))|(d&(b|c)))&e)))
Yes
(((a|(d&(a|b)))|(e&((a|b)|d)))|(f&(((a|b)|d)|e)))
Yes
(((b&c)|(e&((b&c)|(d&((a&b)|(c&(a|b)))))))|(f&((((a&b)|c)|(d&(b|c)))|(e&(b|c)))))
Yes
((((a&b)...

result:

ok 65536 lines, tightest: 41 out of 42 (65536 test cases)

Test #17:

score: -100
Runtime Error

input:

65536
7
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
7
00000001000100010001000101110111000100010111011101110111011111110001000101110111011101110111111100010001011101110111011111111111
7
000000010001001100000001001101...

output:

Yes
((((((a&b)&c)&d)&e)&f)&g)
Yes
((((((a&b)&c)|(d&(a&b)))|(e&((a&b)|(d&(a|b)))))|(f&(((a&b)|(d&(a|b)))|(e&((a|b)|(d&((a|b)|c)))))))|(g&((((a&b)|(d&(a|b)))|(e&((a|b)|(d&((a|b)|c)))))|(f&(((a&b)|(d&(a|b)))|(e&((a|b)|d)))))))
Yes
((((((a&b)&c)|(d&((a&b)|(c&b))))|(e&(((a&b)&c)|(d&(b|(c&(a|b)))))))|(f&(...

result: