C. Short Program

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
3
| 3
^ 2
| 1
output
2
| 3
^ 2
input
3
& 1
& 3
& 5
output
1
& 1
input
3
^ 1
^ 2
^ 3
output
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,进行位运算,求怎么简化到不超过5次。

分析:看出每次的操作数不超过2^10-1,先用0000000000,1111111111,进行题意的操作,发现规律,01-----> 0/1,通过 | ^ & 运算使得它成立。

#include <bits/stdc++.h>

using namespace std;

bool calc(int a,int i) {
if(a&(<<i)) return ;
return ;
} int main()
{
int n;
int x = ,y = ; cin>>n;
while(n--) {
char str[];
int t;
scanf("%s%d",str,&t); if(str[]=='|') x|=t,y|=t; if(str[]=='&') x&=t,y&=t; if(str[]=='^') x^=t,y^=t; } int v1 = ; // |
int v2 = ; // ^
int v3 = ;
for(int i = ; i < ; i++) {
if(calc(x,i)&&calc(y,i)) v1 = v1 + (<<i);
if(calc(x,i)&&!calc(y,i)) v2 = v2 + (<<i);
//if(!calc(x,i)&&calc(y,i))
if(!calc(x,i)&&!calc(y,i)) v3 = v3 - (<<i);
} printf("3\n"); printf("| %d\n",v1);
printf("^ %d\n",v2);
printf("& %d\n",v3); return ;
}

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

  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. 1

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

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

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

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #443 (Div. 1) C. Tournament

    题解: 思路挺简单 但这个set的应用好厉害啊.. 我们把它看成图,如果a存在一门比b大,那么a就可以打败b,a——>b连边 然后求强联通分量之后最后顶层的强联通分量就是能赢的 但是因为是要动态 ...

  8. Codeforces Round #443 (Div. 2) C 位运算

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

  9. 【Codeforces Round #443 (Div. 2) A】Borya's Diagnosis

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟 [代码] #include <bits/stdc++.h> using namespace std; const ...

随机推荐

  1. Maven系统学习

    1. 1.1 何为构建? 编译.测试.运行.打包.部署等工作: Maven就是用软件的办法让这一系列工作自动化,只需要一条简单的命令,所有繁琐的工作就会自动完成: Maven最大的消除了构建的重复,抽 ...

  2. VI设计对于企业文化建设的重要性

    VI设计对于企业文化建设非常重要,包括企业品牌形象塑造.企业价值提炼.企业文化建设等有着非常重要的作用.VI设计的发展趋势是什么? 第一 从静态到动态 中国过去一段时间以来的VI设计,也是以一种静止和 ...

  3. AUTO Uninstaller【教程】AUTODESK系列软件MAYA,3DSMAX,CAD,INVENTOR,REVIT修复卸载工具 Windows x64位

    小伙伴是不是遇到 MAYA/CAD/3DSMAX/INVENTOR/REVIT 安装失败或者安装不了的问题了呢?AUTODESK系列软件着实令人头疼,MAYA/CAD/3DSMAX/INVENTOR/ ...

  4. (转)linux sed命令就是这么简单

    sed替换命令 原文:https://www.cnblogs.com/zd520pyx1314/p/6061337.html http://www.cnblogs.com/wangqiguo/p/67 ...

  5. PHP Mongodb API参考

    <?php /*** Mongodb类** examples: * $mongo = new HMongodb("127.0.0.1:11223"); * $mongo-&g ...

  6. 使用awstat分析Nginx的访问日志

    在我的上一篇文章<使用 Nginx 提升网站访问速度>中介绍了 Nginx 这个 HTTP 服务器以及如何通过它来加速网站的访问速度.在实际的网站运营中,我们经常需要了解到网站的访问情况, ...

  7. 新版本火狐 ,Chrome不支持showModalDialog解决办法

    平常的网站中,有时我们会希望使用者按下按钮后开启一个保持在原窗口前方的子窗口,在IE中,我们可以使用showModalDialog来达成,但是chrome早就不支持showModalDialog,最近 ...

  8. opencv2.4.10+VS2012配置问题

    opencv2.4.10+VS2012配置 作为opencv的初学者,第一个难题想必都一样,如何配置opencv+VS的环境呢?在网上的教程,铺天盖地,但我仍然是尝试了十几次才找到属于自己的那套配置方 ...

  9. module.exports和 exports 方法暴露

    在Node.js包管理时需要把方法暴露给外部文件 文件:Hello.js    Hello方法是模仿面向对象类的写法 function Hello() { var name; this.setName ...

  10. C# 数据库连接增删改查

    C# 连接数据库 using System.Data.sqlCliect; // 加在头 main函数里面用法(删) //连接字符串 string Connectionstring = "s ...