【Codeforces Round #402 (Div. 1)】Codeforces 778B 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:
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. 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.

位运算的各位是独立的,对每一位枚举是 0 还是1分别计算,复杂度 O(mn)

#include<cstdio>
#include<string>
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
map<string,int> id;
int n,m,kind[5010],val[5010][1010],opt[5010],opta[5010],optb[5010],mxa[1010],mna[1010];
int calc(int p)
{
    int ret=0;
    for (int i=1;i<=n;i++)
    {
        if (kind[i]==1) val[i][p]=(val[opta[i]][p]&val[optb[i]][p]);
        if (kind[i]==2) val[i][p]=(val[opta[i]][p]|val[optb[i]][p]);
        if (kind[i]==3) val[i][p]=(val[opta[i]][p]^val[optb[i]][p]);
        ret+=val[i][p];
    }
    return ret;
}
int main()
{
    int x0,x1;
    string s;
    ios::sync_with_stdio(0);
    cin>>n>>m;
    id["?"]=0;
    for (int i=1;i<=n;i++)
    {
        cin>>s;
        id[s]=i;
        cin>>s;
        cin>>s;
        if (s[0]=='0'||s[0]=='1')
        {
            kind[i]=0;
            for (int j=1;j<=m;j++)
                val[i][j]=s[j-1]-'0';
        }
        else
        {
            opta[i]=id[s];
            cin>>s;
            if (s[0]=='A') kind[i]=1;
            else
            {
                if (s[0]=='O') kind[i]=2;
                else kind[i]=3;
            }
            cin>>s;
            optb[i]=id[s];
        }
    }
    for (int j=1;j<=m;j++)
    {
        val[0][j]=0;
        x0=calc(j);
        val[0][j]=1;
        x1=calc(j);
        if (x0==x1) mxa[j]=mna[j]=0;
        else
        {
            if (x0<x1)
            {
                mna[j]=0;
                mxa[j]=1;
            }
            else
            {
                mna[j]=1;
                mxa[j]=0;
            }
        }
    }
    for (int j=1;j<=m;j++) cout<<mna[j];
    cout<<endl;
    for (int j=1;j<=m;j++) cout<<mxa[j];
    cout<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值