• CodeForces


     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.

    Example

    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 inhttps://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.


    题意:用五次运算以内表示题目给的运算。就是0到1023的所有数经过你的运算和题目给的运算结果必须一样,而且在5次以内。

    思路:用 &,^ , |  表示题目给的运算,所以三步就可以,就变成   和 几 进行这几个运算的问题。所以就假设x和y。x=1023(10),1111111111(2).y=0(10),0000000000(2);  分别把x和y经过题目的运算,再来看xy每一位(二进制)的变化。刚开始x每一位(二进制)是1,y的每一位(二进制)是0,当运算结束,x和y的每一位(二进制)结果可以用来推算出这一位(二进制)分别经历了什么(2333)。

    比如:

    x1(表示二进制 x 的第一位数) ,y1(表示二进制 y 的第一位数)

    开始 x1 = 1; y1 = 0;

    结果 x1 = 0; y1 = 1;

    那么 解方程  1 & (?)^(?)|(?) = 0;  0 & (?)^(?)|(?) = 1;

    可以得出括号里依次为  1 , 1 , 0;

    (& :与: 1&1=1,1&0=0&1=1,0&0=0)

    (^ :异或:1^1=0^0=0  ,  1^0=0^1=1 )

    (| :或: 1|1=0|1=1|0=1,0|0=0)

    #include<map>
    #include<set>
    #include<queue>
    #include<math.h>
    #include<vector>
    #include<string>
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #define inf 0x3f3f3f
    #define ll long long
    #define maxn 13005
    using namespace std;
    int x=1023,y=0,And,Or,Xor;
    int main()
    {
        int n;
        cin>>n;
        while(n--)
        {
            char a;int m;
            cin>>a>>m;
            if(a=='&'){
                x&=m;y&=m;
            }
            if(a=='|'){
                x|=m;y|=m;
            }
            if(a=='^'){
                x^=m;y^=m;
            }
        }
        int res=1,xx,yy;
        for(int i=0;i<10;i++)
        {
            xx= (x&1);
            yy= (y&1);
            if(xx==1){
                if(yy==1){
                    And+=res;
                    Xor+=0;
                    Or+=res;
                }
                else{
                    And+=res;
                    Xor+=0;
                    Or+=0;
                }
            }
            else{
                if(yy==1){
                    And+=res;
                    Xor+=res;
                    Or+=0;
                }
                else {
                    And+=0;
                    Xor+=0;
                    Or+=0;
                }
            }
            x>>=1;y>>=1;
            res<<=1;
        }
        printf("3
    & %d
    ^ %d
    | %d
    ",And,Xor,Or);
    }
    

      

  • 相关阅读:
    sc输入输出
    sc基本语法
    sp启动执行
    sp的配置安装
    软件质量属性的代码层实现
    以淘宝网为例描述常见质量属性场景
    架构漫谈读后感
    《架构之美》阅读笔记三
    《架构之美》阅读笔记二
    大三寒假学习进度笔记Day25
  • 原文地址:https://www.cnblogs.com/da-mei/p/8251201.html
Copyright © 2020-2023  润新知