CF778B(round 402 div.2 E) Bitwise Formula
Posted 王宜鸣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF778B(round 402 div.2 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 mbits. 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.
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.
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.
3 3
a := 101
b := 011
c := ? XOR b
011
100
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
0
0
思路:
1.由于是位操作,所以各个位之间的运算结果相互独立。因此一位一位割裂来计算,选取每一位取“0”还是取“1”更好一点。对于每一位来讲,n个数的这一位上的“1”的个数的和越多,则所有数的和越大。另外,如果取“0”和取“1”能获得相同的结果,则取“0”即可。
2.学习使用bitset用法。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <bitset> 4 #include <map> 5 using namespace std; 6 7 int n, m, res[1005][2]; 8 bitset<1005> bs[5005][2]; 9 map<string, int> mp; 10 int main() 11 { 12 mp["?"] = 0; 13 bs[0][1].set(); 14 cin >> n >> m; 15 for (int i = 0; i < n; i++) 16 { 17 string x, tmp, y, op, z; 18 cin >> x >> tmp >> y; 19 mp[x] = i + 1; 20 if (y[0] == ‘0‘ || y[0] == ‘1‘) 21 { 22 for (int j = 0; j < m; j++) 23 { 24 bs[i + 1][0].set(j, y[j] - ‘0‘); 25 bs[i + 1][1].set(j, y[j] - ‘0‘); 26 } 27 } 28 else 29 { 30 cin >> op >> z; 31 for (int j = 0; j <= 1; j++) 32 { 33 if (op == "AND") 34 { 35 bs[i + 1][j] = bs[mp[y]][j] & bs[mp[z]][j]; 36 } 37 else if (op == "OR") 38 { 39 bs[i + 1][j] = bs[mp[y]][j] | bs[mp[z]][j]; 40 } 41 else 42 { 43 bs[i + 1][j] = bs[mp[y]][j] ^ bs[mp[z]][j]; 44 } 45 } 46 } 47 for (int j = 0; j < m; j++) 48 { 49 for (int k = 0; k <= 1; k++) 50 res[j][k] += bs[i + 1][k][j]; 51 } 52 } 53 for (int i = 0; i < m; i++) 54 { 55 cout << (res[i][0] > res[i][1] ? 1 : 0); 56 } 57 puts(""); 58 for (int i = 0; i < m; i++) 59 { 60 cout << (res[i][0] >= res[i][1] ? 0 : 1); 61 } 62 return 0; 63 }
以上是关于CF778B(round 402 div.2 E) Bitwise Formula的主要内容,如果未能解决你的问题,请参考以下文章
CF779A(round 402 div.2 A) Pupils Redistribution
CF Round #600 (Div 2) 解题报告(A~E)
cf Round #309 (Div. 2) E - Love Triangles(二分图染色)
Codeforces Round #402 (Div. 2)