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 = , f1 = ;
int AND = , XOR = , OR = ; int main() {
ios::sync_with_stdio(false);
// freopen("test.txt", "r", stdin);
// freopen("outout.txt","w",stdout);
cin >> n;
for (int i = ; 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 = ;
int w0, w1; for (int i = ; i <= ; i++) {
w0 = f0 & ;
w1 = f1 & ;
if (w0 == ) {
if (w1 == )
AND += base;
} else {
if (w1 == ) {
AND += base;
XOR += base;
} else {
AND += base;
OR += base;
}
}
base <<= ;
f0 >>= , f1 >>= ;
}
cout << << endl
<< "& " << AND << endl
<< "^ " << XOR << endl
<< "| " << OR << endl; return ;
}

443 C. Short Program的更多相关文章

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

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

  3. Codeforces Round #879 (Div. 2) C. Short Program

    题目链接:http://codeforces.com/contest/879/problem/C C. Short Program time limit per test2 seconds memor ...

  4. [Codeforces Round #443]Div2 C Short Program

    给你一串$n$个按顺序的位运算操作(&,|,^),形如"opt x",要求用不超过5行的位运算,按顺序操作出来和那个结果是一样的.$(n<=5e5,0<=x&l ...

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

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

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

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

  7. Codeforces878 A. Short Program

    题目类型:位运算 传送门:>Here< 题意:给出\(N\)个位运算操作,要求简化操作数量,使之结果不受影响(数据在1023之内) 解题思路 我们发现数字的每一位是独立的.也就是说,每一个 ...

  8. Codeforces 879C/878A - Short Program

    传送门:http://codeforces.com/contest/879/problem/C 本题是一个位运算问题——位运算的等价变换. 假设位运算符“&”“|”“^”是左结合的,且优先级相 ...

  9. Codeforces 878A - Short Program(位运算)

    原题链接:http://codeforces.com/problemset/problem/878/A 题意:给出n个位运算操作, 化简这些操作, 使化简后的操作次数不多于5步. 思路:我们可以对二进 ...

随机推荐

  1. CSS字体无法设置成功的问题

    在 CSS 中设置字体名称,直接写中文是可以的.但是在文件编码(GB2312.UTF-8 等)不匹配时会产生乱码的错误.xp 系统不支持 类似微软雅黑的中文. 方案一: 你可以使用英文来替代. 比如 ...

  2. 【canvas系列】canvas实现“ 简单的Amaziograph效果”--画对称图【强迫症福利】

    标题很难引人入胜,先放个效果图好了 如果图片吸引不了你,那我觉得也就没啥看的了. demo链接: https://win7killer.github.io/demo_set/html_demo/can ...

  3. 2017年CCF大数据与计算智能大赛,梳理总结新鲜出炉啦~~~

    0 序言 比赛已经过去一段时间,现在才来写总结似乎有点儿晚,但是挡不住内心发出的强烈呼唤的声音,所以决定静下心来梳理一遍,查缺补漏. 参赛契机: 2017年9月偶然在学校的官方微信推送中看到2017年 ...

  4. java基础(七) java四种访问权限

    引言   Java中的访问权限理解起来不难,但完全掌握却不容易,特别是4种访问权限并不是任何时候都可以使用.下面整理一下,在什么情况下,有哪些访问权限可以允许选择. 一.访问权限简介 访问权限控制: ...

  5. 风险管理,未雨绸缪——《代码之殇》读书笔记II

    这次的内容主要是关于软件开发过程中的风险管理,包括项目用时估计.产品的发布与更新.承诺兑现的重要性. ①项目用时估计 有人会质疑项目用时估计的可靠性,因为就事而言,这次的任务可能和上次不一样了,开发环 ...

  6. netty发送和接收数据handler处理器

    netty发送和接收数据handler处理器 主要是继承 SimpleChannelInboundHandler 和 ChannelInboundHandlerAdapter 一般用netty来发送和 ...

  7. Entity Framework 更新模式之Attach与EntityState.Modified模式的区别

    数据库中有一个City表 初始时数据: 实体类与Fluent Api配置映射 public class City { public int Id { get; set; } public string ...

  8. 网罗收集10046的各种Case,方便trace信息的收集

    每逢与遇到SQL相关性能,我们总是需要收集10046的,来查看和诊断问题.因为10046真实的反应的SQL语句执行的时候的真实信息,解析,执行,获取的时间消耗,row source operation ...

  9. [翻译] GCDiscreetNotificationView

    GCDiscreetNotificationView GCDiscreetNotificationView is a discreet, non-modal, notification view fo ...

  10. 解决win 和 ubuntu 虚拟机之间 无法 复制粘贴的问题,以及重装vmtool后,还是无法解决的办法

    第一步:重新安装vmware-tool 我这里已经装过了,所以显示这个,不然应该是显示:安装 VMware Tools 它会打开一个文件夹,把压缩包复制到任一个地方解压,执行.pl的那个文件:sudo ...