codeforces 779 E. Bitwise Formula
来源:互联网 发布:华三交换机开启端口 编辑:程序博客网 时间:2024/06/06 02:55
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.
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 3a := 101b := 011c := ? XOR b
011100
5 1a := 1bb := 0cx := ? OR ad := ? XOR ?e := d AND bb
00
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.
题解:
变量每一位是0或1对答案影响独立,所以枚举每一位为1和0,统计该位的答案和,记入
答案即可.
代码:
#include<algorithm>#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<map>#define M 100000#define N 6006using namespace std;typedef unsigned long long ll;const ll sed=173;map<ll,int>id;int val[N][N];int opt[N],fir[N],sec[N];int n,m;bool f[N];void read(int u){char ch=getchar();while(ch==' '||ch=='\n')ch=getchar();ll name=0;while(ch!=' '){name=name*sed+ch-'a'+1;ch=getchar();}id[name]=u;ch=getchar();while(ch!=' ')ch=getchar();ch=getchar();if(ch=='0'||ch=='1'){val[u][1]=ch-'0';for(int i=2;i<=m;i++){ch=getchar();int x=ch-'0';val[u][i]=x;}}else{if(ch=='?')fir[u]=-1,ch=getchar();else {ll tmp=0;while(ch!=' '){tmp=tmp*sed+ch-'a'+1;ch=getchar();}fir[u]=id[tmp];}char op[10];scanf("%s",op+1);if(op[1]=='X')opt[u]=1;else if(op[1]=='O')opt[u]=2;else opt[u]=3;ch=getchar();ch=getchar();if(ch=='?')sec[u]=-1,ch=getchar();else {ll tmp=0;while(ch!=' '&&ch!='\n'){tmp=tmp*sed+ch-'a'+1;ch=getchar();}sec[u]=id[tmp];}f[u]=1;}}int mx[N],mn[N];int cal(int u,int x){int ret=0;for(int i=1;i<=n;i++){if(!f[i])continue;int a,b;if(fir[i]==-1)a=x;else a=val[fir[i]][u];if(sec[i]==-1)b=x;else b=val[sec[i]][u];if(opt[i]==1)val[i][u]=a^b;else if(opt[i]==2)val[i][u]=a|b;else val[i][u]=a&b;ret+=val[i][u];}return ret;}int main(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)read(i);for(int i=1;i<=m;i++){int x=cal(i,1);int y=cal(i,0);if(x>y)mx[i]=1,mn[i]=0;else if(x==y)mx[i]=mn[i]=0;else mx[i]=0,mn[i]=1;}for(int i=1;i<=m;i++)printf("%d",mn[i]);printf("\n");for(int i=1;i<=m;i++)printf("%d",mx[i]);}
- codeforces 779 E. Bitwise Formula
- Codeforces Round #402 E. Bitwise Formula
- (贪心, 字符串, 位运算)Codeforces Round #402 E. Bitwise Formula
- CodeForces 778B Bitwise Formula 解题报告
- 【Codeforces Round #402 (Div. 1)】Codeforces 778B Bitwise Formula
- CF 778B Bitwise Formula 位运算,贪心
- bitwise
- codeforces 163E e-Government
- 【Codeforces 163E】E-Government
- Codeforces 78E Evacuation
- 【dp】codeforces 83E
- Codeforces 124 E
- Codeforces 231E
- Codeforces 231E - Cactus
- Codeforces #163 Div2 E
- codeForces 35E
- Codeforces 35E
- Playlist codeforces 268E
- 2张图理解resnet核心思想
- Java for Web学习笔记(三五):自定义tag(3)TLDS和Tag Handler
- Mysql各个版本区别及官网下载
- 发送邮件
- eclipse配置tomcat问题
- codeforces 779 E. Bitwise Formula
- 摩拜CEO胡炜炜 一席 演讲中的 点亮北京,深圳骑行动态,地图技术实现
- MongoDB 分片
- 二叉树算法
- 九阳真经 基础心法
- java去分隔符的三种方法
- 北京阿凡达雪景
- 三维模型的主成分分析(3D Model PCA, CPCA, NPCA)
- 使用注解注入sessionFactory的注意点