C. Short Program
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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

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.

Examples
input
  1. 3
    | 3
    ^ 2
    | 1
output
  1. 2
    | 3
    ^ 2
input
  1. 3
    & 1
    & 3
    & 5
output
  1. 1
    & 1
input
  1. 3
    ^ 1
    ^ 2
    ^ 3
output
  1. 0
Note

You can read about bitwise operations in https://en.wikipedia.org/wiki/Bitwise_operation.

Second sample:

Let x be an input of the Petya's program. It's output is ((x&1)&3)&5 = x&(1&3&5) = x&1. So these two programs always give the same outputs.

思路:取x=0,y=1023(即两个互为取反数的数字)。将n次位运算操作后的x,y值,对比即可得到每一位数字进行的 操作。

代码:

  1. #include<bits/stdc++.h>
  2. #define db double
  3. #include<vector>
  4. #define ll long long
  5. #define vec vector<ll>
  6. #define Mt vector<vec>
  7. #define ci(x) scanf("%d",&x)
  8. #define cd(x) scanf("%lf",&x)
  9. #define cl(x) scanf("%lld",&x)
  10. #define pi(x) printf("%d\n",x)
  11. #define pd(x) printf("%f\n",x)
  12. #define pl(x) printf("%lld\n",x)
  13. const int N = 1e6 + ;
  14. const int mod = 1e9 + ;
  15. const int MOD = mod - ;
  16. const db eps = 1e-;
  17. const db PI = acos(-1.0);
  18. using namespace std;
  19. bool cal(int a,int i){
  20. if(a&(<<i)) return ;
  21. return ;
  22. }
  23. int main(){
  24. int n;ci(n);
  25. int x=,y=;
  26. while (n--) {
  27. char c[];
  28. int t;
  29. scanf("%s %d", c, &t);
  30. if (c[] == '|') x|=t,y|=t;
  31. if (c[] == '&') x&=t,y&=t;
  32. if (c[] == '^') x^=t,y^=t;
  33. }
  34. /*
  35. 4种:
  36. x:0,y:1.
  37. x y:0011,0010,0111,0110
  38. */
  39. int v1 = , v2 = , v3 = ;
  40. for (int i = ; i < ; i++) {
  41. if(!cal(x,i)&&cal(y,i)) v2+=(<<i);//0011: |0 &1 ^0
  42. if(cal(x,i)&&cal(y,i)) v3+=(<<i);//0111: |0 &0 ^1
  43. if(cal(x,i)&&!cal(y,i)) v2+=(<<i),v3+=(<<i);//0110: |0 &1 ^1
  44. }
  45. printf("3\n");
  46. printf("| %d\n", v1);
  47. printf("& %d\n", v2);
  48. printf("^ %d\n", v3);
  49. return ;
  50. }

Codeforces Round #443 (Div. 2) C 位运算的更多相关文章

  1. Codeforces Round #443 (Div. 2) 【A、B、C、D】

    Codeforces Round #443 (Div. 2) codeforces 879 A. Borya's Diagnosis[水题] #include<cstdio> #inclu ...

  2. Codeforces Round #443 (Div. 2) C: Short Program - 位运算

    传送门 题目大意: 输入给出一串位运算,输出一个步数小于等于5的方案,正确即可,不唯一. 题目分析: 英文题的理解真的是各种误差,从头到尾都以为解是唯一的. 根据位运算的性质可以知道: 一连串的位运算 ...

  3. Codeforces Round #443 (Div. 1) A. Short Program

    A. Short Program link http://codeforces.com/contest/878/problem/A describe Petya learned a new progr ...

  4. Codeforces Round #443 (Div. 2)

    C. Short Program Petya learned a new programming language CALPAS. A program in this language always ...

  5. 【Codeforces Round #443 (Div. 2) C】Short Program

    [链接] 我是链接,点我呀:) [题意] 给你一个n行的只和位运算有关的程序. 让你写一个不超过5行的等价程序. 使得对于每个输入,它们的输出都是一样的. [题解] 先假设x=1023,y=0; 即每 ...

  6. Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算

    D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...

  7. Codeforces Round #443 (Div. 2) C. Short Program

    C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. Codeforces Round #443 Div. 1

    A:考虑每一位的改变情况,分为强制变为1.强制变为0.不变.反转四种,得到这个之后and一发or一发xor一发就行了. #include<iostream> #include<cst ...

  9. Codeforces Round #443 (Div. 1) B. Teams Formation

    B. Teams Formation link http://codeforces.com/contest/878/problem/B describe This time the Berland T ...

随机推荐

  1. spring数组注入

    数组注入 public class MyCollection {     private  String[]array;     private List<String>list;     ...

  2. usb-host一步一步学(一)安卓在usb-host模式下列出当前连接的usb设备

    在本次尝试中,我的安卓手机(HTC One X) 通过OTG线作为usb主机模式列出当前插入的usb设备,版本要求minSDKVersion="12". 没有外设的情况下,结果如下 ...

  3. 【解决】Git failed with a fatal error. Authentication failed for ‘http://......’

    今天在visual studio中运行项目,打算pull最新的代码的时候,报错: Git failed with a fatal error. Authentication failed for ‘h ...

  4. 从零开始的全栈工程师——js篇2.5

    数据类型与全局属性 js的本质就是处理数据 数据来自于后台的数据库所以变量就起到一个临时存储数据的这作用ECMAscirpt 制定了js的数据类型 一.数据类型 1.基本数据类型 基本数据类型就是简单 ...

  5. Javascript Number

    Number 对象 Number对象是原始值的包装对象 创建Number对象的语法: var myNum = new Number(value): var myNum = Number(value): ...

  6. Android 从零开始搭建一个主流项目框架—RxJava2.0+Retrofit2.0+OkHttp

    我这里的网络请求是用的装饰者模式去写的,什么是装饰者模式呢?在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象.我的理解就是一个接口, ...

  7. MySQL-5.6.30 (OpenLogic CentOS7.2)

    平台: CentOS 类型: 虚拟机镜像 软件包: centos7.2 mysql5.6.30 basic software database linux open source 服务优惠价: 按服务 ...

  8. redis在Windows下以后台服务一键搭建集群(多机器)

    redis在Windows下以后台服务一键搭建集群(多机器) 一.概述 此教程介绍如何在windows系统中多台机器之间布置redis集群,同时要以后台服务的模式运行.布置以脚本的形式,一键完成.多台 ...

  9. iphone开发笔记

    1.uiimage图片拉伸 - (void)stretchBackgroundImage { //UIImage *originalImage = [[self backgroundImageForS ...

  10. pat乙级1034

    1.vs2013不能用scanf,改为scanf_s,但是提交时不能用scanf_s,用scanf... scanf_s(], &a[], &b[], &b[]); 2.c++ ...