codeforces 778B 拆位运算
来源:互联网 发布:公安局网络通缉人员 编辑:程序博客网 时间:2024/06/03 20:07
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.
Example
Input
3 3
a := 101
b := 011
c := ? XOR b
Output
011
100
Input
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
Output
0
0
Note
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.
?是你选择的一个数,给你n个变量和变量的得到方式,让你构造出?,使得所有的变量和最大和所有变量和最小的 ?,输出最小的?
暴力拆位就行,每一位假设为0或者1,套进去得到结果比较就好
#include <bits/stdc++.h>using namespace std;string minn,maxx;int bit[5010][1010];int tot,tot1,n,m;map<string,int> mp;struct node{ int f[1010]; int a,op,b;}no[5010];int check(int id,int x){ no[0].f[id]=x; int res=0; for(int i=1;i<=n;i++) { int ff=0; int zuo=no[i].a,you=no[i].b; if(no[i].op==1) if(no[zuo].f[id]!=no[you].f[id]) no[i].f[id]=1,ff=1; if(no[i].op==2) if(no[zuo].f[id]||no[you].f[id]) no[i].f[id]=1,ff=1; if(no[i].op==3) if(no[zuo].f[id]&&no[you].f[id]) no[i].f[id]=1,ff=1; if(!ff) no[i].f[id]=0; if(no[i].f[id]) res++; } return res;}int main(){ cin>>n>>m; mp["?"]=0; for(int i=1;i<=n;i++) { string s,s1,s2; cin>>s>>s1>>s2; mp[s]=i; if(s2[0]>='0'&&s2[0]<='9') for(int j=0;j<m;j++) no[i].f[j]=s2[j]-'0'; else { string s3,s4; cin>>s3>>s4; no[i].a=mp[s2]; if(s3[0]=='X') no[i].op=1; if(s3[0]=='O') no[i].op=2; if(s3[0]=='A') no[i].op=3; no[i].b=mp[s4]; } } for(int i=0;i<m;i++) { if(check(i,1)>check(i,0)) maxx+='1',minn+='0'; else maxx+='0',minn+='1'; } cout<<minn<<endl; cout<<maxx<<endl;}
- codeforces 778B 拆位运算
- Codeforces Problem 778B 拆位做法
- Codeforces 293B Distinct Paths (DFS+位运算)
- codeforces 244B Restoring Table(“&”位运算)
- Codeforces Round #306 (Div. 2) B.Preparing Olympiad (位运算)
- codeforces 486B OR in Matrix(位运算)
- Codeforces 578B Or Game [贪心] [位运算]
- Codeforces Round #443 (Div. 2) A、B、C 位运算
- A+B(位运算)
- 【位运算+暴力】Codeforces Beta Round #86 (Div. 2 Only) B
- Codeforces Round #267 (Div. 2) B. Fedor and New Game(位运算)
- Codeforces Round #275 (Div. 1)B(线段树+位运算)
- Codeforces Round #318 (Div. 2) B. Bear and Three Musketeers 位运算压缩
- CodeForces 611B New Year and Old Property【DFS&&模拟位运算】
- CodeForces 482B 有趣的数列 数据结构+线段树+位运算的技巧
- CodeForces 558C(位运算)
- 位运算实现a+b
- CF 778B Bitwise Formula 位运算,贪心
- LintCode 83. 落单的数II
- 【Scikit-Learn 中文文档】朴素贝叶斯
- Java中对List集合的常用操作
- 35 个 Java 代码性能优化总结
- Java字符流
- codeforces 778B 拆位运算
- Zabbix 监控TCP
- Android获取APP版本号与版本名的方法.
- leetcode 226. Invert Binary Tree
- iOS开发(Swift)——Swift开发技巧与注意事项
- django 使用中间件 访问request里面的内容
- ceph (luminous 版) pool 管理
- ORA-00821: Specified value of sga_target
- 关于java的算术问题的总结