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

Copy

3
| 3
^ 2
| 1

output

Copy

2
| 3
^ 2

input

Copy

3
& 1
& 3
& 5

output

Copy

1
& 1

input

Copy

3
^ 1
^ 2
^ 3

output

Copy

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.

 

给出原来的表达式,问化简后的表达式(缩减至5行内)。

求出该数每一个位置上的变化。

设x=1023,二进制全为1,设y=0,二进制全为0,让他们执行操作。

看操作后每一位的变化情况。

对于每一个位置来说

若xx==0 yy==0,那么先或一个1,再异或一个1

若xx>0 yy>0,那么或上一个1

如果xx==0 yy>0,那么异或一个1

如果xx>0 yy==0,那么此位置不变

#include <algorithm>    //STL通用算法
#include <bitset>     //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>     //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>      //STL双端队列容器
#include <exception>    //异常处理类
#include <fstream>
#include <functional>   //STL定义运算函数(代替运算符)
#include <limits>
#include <list>      //STL线性列表容器
#include <map>       //STL 映射容器
#include <iomanip>
#include <ios>      //基本输入/输出支持
#include<iosfwd>     //输入/输出系统使用的前置声明
#include <iostream>
#include <istream>     //基本输入流
#include <ostream>     //基本输出流
#include <queue>      //STL队列容器
#include <set>       //STL 集合容器
#include <sstream>    //基于字符串的流
#include <stack>      //STL堆栈容器    
#include <stdexcept>    //标准异常类
#include <streambuf>   //底层输入/输出支持
#include <string>     //字符串类
#include <utility>     //STL通用模板类
#include <vector>     //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 100000+6;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        getchar();
        char c;
        int k;
        int a=1023,b=0;
        rep(i,1,n)
        {
            char s[20];
            scanf("%s %d",s,&k);
            if(s[0]=='^')
            {
                a^=k;
                b^=k;
            }
            else if(s[0]=='|')
            {
                a|=k;
                b|=k;
            }
            else
            {
                a&=k;
                b&=k;
            }
        }
        int a1=-1,b1=-1;
        rep(i,0,9)
        {
            int xx;
            int yy;
            xx=a&(1<<i);
            yy=b&(1<<i);//看xx yy 对应的第i位是什么
            if(xx==0&&yy==0)
            {
                if(a1==-1)
                    a1=(1<<i);
                else
                    a1=a1|(1<<i);//  |
                if(b1==-1)
                    b1=(1<<i);
                else
                    b1=b1|(1<<i);//  ^
            }
            if(xx>0&&yy>0)
            {
                if(a1==-1)a1=(1<<i);
                else a1=a1|(1<<i);
            }
            if(xx==0&&yy>0)
            {
                if(b1==-1)b1=(1<<i);
                else b1=b1|(1<<i);
            }
        }
        int n1=0;
        if(a1!=-1)n1++;
        if(b1!=-1)n1++;
        printf("%d\n",n1);
        if(a1!=-1)
        {
            printf("| %d\n",a1);
        }
        if(b1!=-1)
        {
            printf("^ %d\n",b1);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值