zoukankan      html  css  js  c++  java
  • 443 C. Short Program

    http://codeforces.com/contest/879/problem/C

    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
    2
    | 3
    ^ 2
    input
    Copy
    3
    & 1
    & 3
    & 5
    output
    1
    & 1
    input
    Copy
    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.

    题意:给出一段程序,只有三种操作,^ & |,然后让你输出一段不超过5行的程序使得运算结果和给出程序的运算结果相同

    这题注意几点:

    1.母数用000000不够,还需要与1111111结合,将其经过处理后数的每一位发生变化 0->? 1->?,

    所以对于每个处理的数位我们有:

    (0->0,1->0)none

    (0->1,1->1)or 1

      (0->1,1->0)  xor 1

      (0->0,1->1)  and 1

    2.位运算处理数后保持原样的操作有这三个  & 1    | 0   ^ 0 

    3.复习下位运算的优先级 AND>XOR>OR

    #include <bits/stdc++.h>
    using namespace std;
    #define maxn 100000
    typedef long long ll;
    #define inf 2147483647
    #define ri register int
    
    int n, x;
    char ch;
    int f0 = 0, f1 = 1023;
    int AND = 0, XOR = 0, OR = 0;
    
    int main() {
      ios::sync_with_stdio(false);
      // freopen("test.txt", "r", stdin);
      //  freopen("outout.txt","w",stdout);
      cin >> n;
      for (int i = 1; i <= n; i++) {
        cin >> ch >> x;
        if (ch == '|') {
          f0 |= x;
          f1 |= x;
        }
        if (ch == '&') {
          f0 &= x;
          f1 &= x;
        }
        if (ch == '^') {
          f0 ^= x;
          f1 ^= x;
        }
      }
      int base = 1;
      int w0, w1;
    
      for (int i = 1; i <= 10; i++) {
        w0 = f0 & 1;
        w1 = f1 & 1;
        if (w0 == 0) {
          if (w1 == 1)
            AND += base;
        } else {
          if (w1 == 0) {
            AND += base;
            XOR += base;
          } else {
            AND += base;
            OR += base;
          }
        }
        base <<= 1;
        f0 >>= 1, f1 >>= 1;
      }
      cout << 3 << endl
           << "& " << AND << endl
           << "^ " << XOR << endl
           << "| " << OR << endl;
    
      return 0;
    }
  • 相关阅读:
    通过Xshell连接CentOS虚拟机
    Linux学习笔记
    JAVA学习摘要
    4k 对齐,你准备好了吗?
    图片种子制作方法,利用图片上传附件
    利用京东服务免费打造属于自己的网站
    PE制作实录 —— 补充说明
    PE制作实录 —— 定义我的 PE 工具箱
    浏览器 — 各项基准测试
    Windows 8.1 归档 —— Step 3 软件的选择与安装
  • 原文地址:https://www.cnblogs.com/planche/p/8506539.html
Copyright © 2011-2022 走看看