Codeforces Round #402 E. Bitwise Formula

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.

Input

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:

  1. Binary number of exactly m bits.
  2. 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.

Output

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.

题意

给定 n 个变量,每个变量的值用二进制表示,一定有 m 位。 (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000).

之后 n 行,每行首先给出一个新的变量名(即保证与前面出现的变量名不重复),紧跟着 := ,然后是下列两种表示的一种:

  1. 直接给出长为 m 位的二进制数
  2. 形如 操作数1 操作符 操作数2 。 其中 操作数1操作数2? 或者 之前已经出现过的变量名;操作符 仅有 ANDORXOR 三种。

要求将 ? 用长为 m 位的二进制数表示。问使得上述 n 个不同变量的和最小的 ? 以及使得 n 个不同变量的和最大的 ?

如果存在多个 ? 值可使变量和最大或最小,输出满足条件的最小的 ? 。(貌似翻得不好,有不明白的细节请自行阅读题目描述)

分析

貌似是很容易想到可行方案的,:cry: 可惜写代码的时候逻辑倒着写,被大数据跑了个 WA

首先,二进制的位操作必然之和当前位有关,不会影响其它二进制位。故在求使得变量和最大、最小的 ? 时,将每一位单独考虑,每一位仅可能为 01 ,考虑 m 位的复杂度只有 O(m)

由于 ? 未知,且变量所给出的表达式可能的操作过于复杂,例如:

a := 101
b := a XOR ?
c := b AND a

上述变量 c 并不能由 a 和 b 快速求出(由于变量 b 实际上根据 ? 在发生改变)。

但是,通过假设每一位的值可以解决这个问题。

将每一位拆分出来思考,如果 ? 在第 j 位为 0,对任意含有 ? 的表达式,通过该位为 0 即可完全求出对应的该位值;第 j 位为 1 同理。同时对 ? 假设 m 位每一位为 0 及 为 1 的情况。如此,我们可以得到一个 bit[0][i][j]bit[1][i][j] 分别表示第 j 位为 0 或 1 时候的变量 i 的情况。

最终对每一位单独判断,第 j 位的 cnt[0]=Σni=1bit[0][i][j] , cnt[1]=Σni=1bit[1][i][j] 。cnt[0] 表示假设 ? 的第 j 位为 0 时,第 j 位为 1 的变量个数;cnt[1] 表示假设 ? 的第 j 位为1 时,第 j 位为 1 的变量个数。

如果 cnt[0] > cnt[1] : 表示 ? 的第 j 位取 0 比 取 1 对最后答案的贡献更大,其它情况类似。

讲的可能很绕,不清楚可留言,博主将尽可能回复。

代码

#include<bits/stdc++.h>
using namespace std;
int n, m, idxa, idxb, cnt[2];
bool bit[2][5010][1010], MIN[1010], MAX[1010];
string s, a, b, ope;
map<string, int> mp;
bool bitOpe(bool x, bool y) {
    if(ope == "AND")    return x & y;
    else if(ope == "OR")    return x | y;
    return x ^ y;
}
void read(int idx)
{
    cin>>a;
    if(a[0] == '0' || a[0] == '1')
    {
        for(int j=0;j<m;j++)
            bit[0][idx][j] = bit[1][idx][j] = a[j]-'0';
    }
    else
    {
        cin>>ope>>b;
        if(a != "?" && b != "?")
        {
            idxa = mp[a],   idxb = mp[b];
            for(int j=0;j<m;j++)
                bit[0][idx][j] = bitOpe(bit[0][idxa][j], bit[0][idxb][j]),
                bit[1][idx][j] = bitOpe(bit[1][idxa][j], bit[1][idxb][j]);
        }
        else if(a == "?" && b == "?")
        {
            for(int j=0;j<m;j++)
                bit[0][idx][j] = bitOpe(0, 0),
                bit[1][idx][j] = bitOpe(1, 1);
        }
        else
        {
            if(a == "?")    idxa = mp[b];
            else    idxa = mp[a];
            for(int j=0;j<m;j++)
                bit[0][idx][j] = bitOpe(bit[0][idxa][j], 0),
                bit[1][idx][j] = bitOpe(bit[1][idxa][j], 1);
        }
    }
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        cin>>s;
        mp[s] = i;
        scanf(" := ");
        read(i);
    }
    for(int j=0;j<m;j++)
    {
        cnt[0] = cnt[1] = 0;
        for(int i=1;i<=n;i++)
        {
            cnt[0] += bit[0][i][j];
            cnt[1] += bit[1][i][j];
        }
        if(cnt[0] > cnt[1])
            MIN[j] = 1, MAX[j] = 0;
        else if(cnt[0] == cnt[1])
            MIN[j] = MAX[j] = 0;
        else
            MIN[j] = 0, MAX[j] = 1;
    }
    for(int j=0;j<m;j++)
        printf("%d",MIN[j]);
    printf("\n");
    for(int j=0;j<m;j++)
        printf("%d",MAX[j]);
    printf("\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值