QOJ.ac

QOJ

Time Limit: 4.0 s Memory Limit: 1024 MB Total points: 100 Difficulty: [show] Hackable ✓

#9980. 布尔函数重构

Statistics

给定一个包含 $n$ 个布尔变量的布尔函数的真值表,请构造一个满足该函数的表达式。在表达式中,只允许使用逻辑与(&)和逻辑或(|)运算符。

具体来说,一个包含 $n$ 个布尔变量的布尔函数的真值表给出了对应于 $n$ 个输入变量所有可能取值的 $2^n$ 个输出。布尔表达式 <expr> 具有以下形式:

  • T, F:表示真(True)和假(False)。
  • a, b, . . . , z:表示其中一个变量。第 $i$ 个变量用字母表中第 $i$ 个小写字母表示。
  • (&):表示对两个表达式的结果进行逻辑与运算。
  • (|):表示对两个表达式的结果进行逻辑或运算。

逻辑与运算和逻辑或运算定义为如下两个接受两个布尔值的布尔函数:

$x_1$ $x_2$ $x_1\&x_2$ $x_1 x_2$
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1

确定是否存在满足条件的表达式。如果存在这样的表达式,请确保二元运算符(& 和 |)的数量不超过 $2^{n-1} + 10$,且括号嵌套深度不超过 100 层。

可以证明,如果存在解,则一定存在一个满足上述问题约束的解。

输入格式

输入包含多个测试用例。第一行包含一个整数 $T$ ($1 \le T \le 2^{16}$),表示测试用例的数量。每个测试用例包含两行:

  • 第一行包含一个整数 $n$ ($1 \le n \le 15$)。
  • 第二行包含一个长度为 $2^n$ 的二进制字符串 $s$,表示给定函数的真值表。

为了理解输入的二进制字符串,假设第 $i$ 个变量的值为 $x_i$。那么,对应的函数值 $f(x_1, x_2, \dots, x_n)$ 等于字符串 $s$ 的第 $(\sum_{i=1}^{n} x_i \cdot 2^{i-1} + 1)$ 位。

保证所有测试用例的 $2^{2n}$ 之和不超过 $2^{30}$。

输出格式

对于每个测试用例:

  • 第一行输出 Yes 或 No,表示是否存在满足条件的表达式。
  • 如果存在表达式,则在第二行输出该表达式。表达式必须严格遵守题目描述中给出的格式,不得添加或省略括号,且不得添加多余的空格。

样例

输入 1

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

输出 1

Yes
(a&b)
Yes
(a|b)
Yes
T
Yes
((a&(b|c))|(b&c))
No
Yes
a
Yes
(a&(b&(c&(d&e))))

说明

以下是第四个样例的真值表解释。

$x_3$ $x_2$ $x_1$ $f(x_1, x_2, x_3)$
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.