cf878a(位运算)
来源:互联网 发布:java安卓4.4.4模拟器 编辑:程序博客网 时间:2024/06/06 03:08
Petya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well.
In the language, there are only three commands: apply a bitwise operation AND, OR or XOR with a given constant to the current integer. A program can contain an arbitrary sequence of these operations with arbitrary constants from 0 to 1023. When the program is run, all operations are applied (in the given order) to the argument and in the end the result integer is returned.
Petya wrote a program in this language, but it turned out to be too long. Write a program in CALPAS that does the same thing as the Petya's program, and consists of no more than 5 lines. Your program should return the same integer as Petya's program for all arguments from 0 to 1023.
The first line contains an integer n (1 ≤ n ≤ 5·105) — the number of lines.
Next n lines contain commands. A command consists of a character that represents the operation ("&", "|" or "^" for AND, OR or XOR respectively), and the constant xi 0 ≤ xi ≤ 1023.
Output an integer k (0 ≤ k ≤ 5) — the length of your program.
Next k lines must contain commands in the same format as in the input.
3| 3^ 2| 1
2| 3^ 2
3& 1& 3& 5
1& 1
3^ 1^ 2^ 3
0
思路:分别用全1和全0的数进行一遍输入的操作,比较结果中的各个位,如果都是0,则都是0,如果都是1,则都是1,如果原为0后为1原为1后为0则异或,和原来不变则没有操作。
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int main(){int n;while(~scanf("%d",&n)){int a=0,b=1023;char p[3];int x;for(int i=0;i<n;i++){scanf("%s %d",p,&x);if(p[0]=='&'){a=a&x;b=b&x;}else if(p[0]=='|'){a=a|x;b=b|x;}else{a=a^x;b=b^x;}}int y1=a&b;int y2=a|b;int ans=0;for(int i=0;i<10;i++){if((a>>i&1)&&!(b>>i&1))ans|=1<<i;}printf("3\n");printf("| %d\n",y1);printf("& %d\n",y2);printf("^ %d\n",ans);}}
- cf878a(位运算)
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- 位运算
- hadoop记录篇5-eclipse开发mapreduce
- (继承方式)反射获取泛型类的类型和对象
- java学习第38天,集合排序
- Debian GNU/Linux 8.4 (jessie)编译安装php.md
- 第四次自考总结
- cf878a(位运算)
- OOAD-设计模式(四)结构型模式之适配器、装饰器、代理模式
- 运行301毕设-初步学习
- UML的视图分类
- HDOJ2806 位运算
- python中chr与ord函数的使用
- 【转】Spring知识点提炼
- 文件系统 | 如何使用网络根文件系统启动开发板?
- 【NOIP模拟】 (10.23) T2 杆子的排列