CF778B.Bitwise Formula
普及+/提高
通过率:0%
时间限制:3.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.
Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.
Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.
鲍勃最近学习了计算机中使用的位运算:与(AND)、或(OR)和异或(XOR)。他研究了这些运算的性质,并发明了一个新游戏。
游戏开始时,鲍勃首先选择一个整数 m,称为游戏的“位宽”,这意味着游戏中所有数字均由 m 位二进制位组成。接着,他让彼得选择某个 m 位的数字。随后,鲍勃计算 n 个变量的值。每个变量被赋予一个常数 m 位数字,或某次位运算的结果;该位运算的操作数可以是此前已定义的变量,也可以是彼得所选的数字。最后,彼得的得分等于所有变量取值之和。
鲍勃想知道:彼得应选择哪个数字,才能使得分最小?又应选择哪个数字,才能使得分最大?在两种情况下,若存在多个数字能达成相同的最小(或最大)得分,则选择其中最小的那个数字。
输入格式
The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000).
The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of:
- Binary number of exactly m bits.
- The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter.
Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.
第一行包含两个整数 n 和 m,分别表示变量的个数和位宽(1≤n≤5000;1≤m≤1000)。
接下来的 n 行描述各个变量。每行恰好描述一个变量,其格式为:新变量的名称、一个空格、符号 :=、一个空格,随后为以下二者之一:
- 一个恰好含 m 位的二进制数;
- 第一个操作数、一个空格、按位运算符(
AND、OR或XOR)、一个空格、第二个操作数。每个操作数要么是此前已定义的变量名,要么是符号?(表示由 Peter 选定的数)。
变量名为仅由小写拉丁字母组成的字符串,长度至多为 10。所有变量名互不相同。
输出格式
In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers.
第一行输出彼得应选择的最小数字,使得所有变量值的和尽可能小;第二行输出彼得应选择的最小数字,使得所有变量值的和尽可能大。两个数字均应以 m 位二进制数形式输出。
输入输出样例
输入#1
3 3 a := 101 b := 011 c := ? XOR b
输出#1
011 100
输入#2
5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb
输出#2
0 0
说明/提示
In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15.
For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.
在第一个样例中,如果彼得选择数字 0112,那么 a=1012,b=0112,c=0002,它们的值之和为 8。如果他选择数字 1002,那么 a=1012,b=0112,c=1112,它们的值之和为 15。
对于第二个测试用例,变量 a、bb、cx、d 和 e 的和的最小值与最大值均为 2,且该和不依赖于彼得所选的数字,因此彼得可选择的最小数字为 0。
输入解题思路,AI测评打分。不知道怎么写?