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");
}
深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值