QOJ.ac

QOJ

実行時間制限: 1.5 s メモリ制限: 256 MB 満点: 100

#11100. 丢失的逻辑

統計

Gustav 正在阅读关于 2-可满足性(2-satisfiability)的内容,这是一个众所周知的布尔变量赋值问题,旨在满足一系列约束条件——即每个约束涉及两个变量的简单逻辑公式。

我们使用 $n$ 个变量 $x_1, x_2, \dots, x_n$,它们可以取值 0(假)和 1(真)。一个约束是一个形如 $a \to b$ 的公式,其中 $a$ 和 $b$ 都是可能被取反的变量。通常情况下,$\to$ 表示逻辑蕴含:$a \to b$ 仅在 $a$ 为 1 且 $b$ 为 0 时为 0。变量 $a$ 的取反记作 $!a$。

给定一个变量赋值,如果约束的计算结果为 1,则称该约束被满足。Gustav 构建了一个约束列表,并正确地得出结论:恰好有三种不同的变量赋值满足所有这些约束。Gustav 写下了这三种赋值,但不幸的是,他丢失了约束列表。

给定三种 $n$ 个变量的赋值,请找到任意一个包含最多 500 个约束的列表,使得这三种给定的赋值是满足所有约束的仅有的赋值。

输入格式

第一行包含一个整数 $n$ ($2 \le n \le 50$),表示变量的数量。接下来三行,每行描述一个赋值。第 $k$ 行包含 $n$ 个以空格分隔的整数 $v_1^k, v_2^k, \dots, v_n^k$,其中每个 $v_i^k$ 为 0 或 1,表示第 $k$ 种赋值中变量 $x_i$ 的值。这三种赋值各不相同。

输出格式

如果没有解,输出一行,包含整数 $-1$。

否则,第一行应包含一个整数 $m$ ($1 \le m \le 500$),表示你方案中的约束数量。接下来的 $m$ 行中,第 $k$ 行应包含第 $k$ 个约束。每个约束应按照以下规则构建:

  • 变量是一个形如 “xi” 的字符串,其中 $i$ 是 1 到 $n$ 之间(含)的整数,且不含前导零。
  • 文字(literal)是一个字符串,由变量组成,前面可能带有 “!” 字符。
  • 约束是一个形如 “a -> b” 的字符串,其中 $a$ 和 $b$ 均为文字。蕴含符号由 “减号” 和 “大于号” 组成,且蕴含符号前后各有一个空格。

样例

输入 1

3
0 0 0
0 1 0
1 0 0

输出 1

3
x1 -> !x2
x3 -> x1
x3 -> x2

输入 2

4
0 0 1 0
1 0 0 0
1 0 1 1

输出 2

-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.