codeforces 878A
2 seconds
256 megabytes
standard input
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.
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 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.
3
| 3
^ 2
| 1
2
| 3
^ 2
3
& 1
& 3
& 5
1
& 1
3
^ 1
^ 2
^ 3
0
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,不需要从原步骤里选
解题思路:先取两个典型的例子0和1023,按照input的步骤进行下去,分别得到答案a和b,然后按位分析如何由0和1023到a和b
按位分析:
0->0 1->0 先&0再^0
0->0 1->1 先&1再^0
0->1 1->0 先&1再^1
0->1 1->1 先&0再^1
对每一位进行上述的四种情况分析就行啦。
ac代码:
1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 using namespace std;
5 int x=0,y=1023;
6 int fun(int u,int i) {
7 if(u&(1<<i)) return 1;
8 return 0;
9 }
10 int main() {
11 ios::sync_with_stdio(false);
12 cin.tie(0);cout.tie(0);
13 int n,a;
14 cin>>n;
15 string s;
16 for(int i=0;i<n;++i) {
17 cin>>s>>a;
18 if(s[0]=='|') {x|=a; y|=a;}
19 if(s[0]=='^') {x^=a; y^=a;}
20 if(s[0]=='&') {x&=a; y&=a;}
21
22 }
23 int i=0,v1=0,v2=0;
24 while(i<=10) {
25 if(!fun(x,i) && fun(y,i)) v1+=(1<<i);
26 if(fun(x,i) && fun(y,i)) v2+=(1<<i);
27 if(fun(x,i) && !fun(y,i)) {
28 v1+=(1<<i);
29 v2+=(1<<i);
30 }
31 ++i;
32 }
33 cout<<2<<endl<<"& "<<v1<<endl<<"^ "<<v2<<endl;
34 return 0;
35 }
codeforces 878A的更多相关文章
- Codeforces 878A - Short Program(位运算)
原题链接:http://codeforces.com/problemset/problem/878/A 题意:给出n个位运算操作, 化简这些操作, 使化简后的操作次数不多于5步. 思路:我们可以对二进 ...
- Codeforces 879C/878A - Short Program
传送门:http://codeforces.com/contest/879/problem/C 本题是一个位运算问题——位运算的等价变换. 假设位运算符“&”“|”“^”是左结合的,且优先级相 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- centos7.4使用filrewalld打开关闭防火墙与端口
1.firewalld的基本使用启动: systemctl start firewalld关闭: systemctl stop firewalld查看状态: systemctl status fire ...
- Java 栈的使用
讲栈之前,要先讲一下Deque双端队列 既可以添加到队尾,也可以添加到队首 既可以从队首获取又可以从队尾获取 public interface Deque<E> extends Queue ...
- Linux内核分析_课程学习总结报告
请您根据本课程所学内容总结梳理出一个精简的Linux系统概念模型,最大程度统摄整顿本课程及相关的知识信息,模型应该是逻辑上可以运转的.自洽的,并举例某一两个具体例子(比如读写文件.分配内存.使用I/O ...
- Java 8中字符串拼接新姿势:StringJoiner
介绍 StringJoiner是java.util包中的一个类,用于构造一个由分隔符分隔的字符序列(可选),并且可以从提供的前缀开始并以提供的后缀结尾.虽然这也可以在StringBuilder类的帮助 ...
- kubernetes备份恢复之velero
Velero备份.恢复.迁移Kubernetes集群 Velero简介 Velero 地址:https://github.com/vmware-tanzu/velero Velero属于VMWare开 ...
- JVM学习-运行时数据区域
目录 前言 运行时数据区 程序计数器 Java虚拟机栈 局部变量表 基础数据类型 对象引用 returnAddress 操作数栈 动态链接 方法返回地址 Java堆 方法区 类型信息 字段描述符 方法 ...
- 6到8个月如何达到三年加得前端经验,对标P7,“慕课网 Java工程师2020”
百度网盘链接:https://pan.baidu.com/s/1xshLRO3ru0LAsQQ0pE67Qg 提取码:bh9f 阶段一:课程设计及前端创建脚手架开发 第1周 需求分析和架构设计 ...
- LOJ10100
原题来自:CEOI 1996 一个电话线公司(简称 TLC)正在建立一个新的电话线缆网络,他们连接了若干个地点,编号分别从 1 到 N,没有两个地点有相同的号码,这些线是双向的并且能使两个地点保持通讯 ...
- FFmpeg libswscale源码分析1-API介绍
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/14349382.html libswscale 是 FFmpeg 中完成图像尺寸缩放和像素 ...
- JavaWeb——JSP,JSP指令,注释
什么是JSP JSP原理 JSP页面中的java代码服务器是如何执行的 Web服务器在调用jsp时会给jsp提供一些什么java对象 JSP执行过程 JSP页面的元素包括 JSP模版元素 JSP脚本片 ...