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 ...
随机推荐
- Core3.1 微信v3 JSAPI支付
1.前言 "小魏呀,这个微信支付还要多久?","快了快了老板,就等着最后一步了...","搞快点哈,就等着上线呢","...... ...
- 微人事项目-mybatis-持久层
摘要 最近将微人事这个开源项目进行了复现,这篇文章记录mybaits访问数据库这一块. 其中MyBatis是一个流行的持久层框架,支持自定义SQL.存储过程和高级映射.MyBatis消除了几乎所有的J ...
- 都知道Base64,Base32你能实现吗?
很长时间没有更新个人博客了,因为前一段时间在换工作,入职了一家新的公司,刚开始需要适应一下新公司的节奏,开始阶段也比较忙.新公司还是有一定的技术气氛的,每周都会有技术分享,而且还会给大家留一些思考题, ...
- 基于循环队列的BFS的原理及实现
文章首发于微信公众号:几何思维 1.故事起源 有一只蚂蚁出去寻找食物,无意中进入了一个迷宫.蚂蚁只能向上.下.左.右4个方向走,迷宫中有墙和水的地方都无法通行.这时蚂蚁犯难了,怎样才能找出到食物的最短 ...
- 0到1:微信后台系统的演进之路 原创 张文瑞 InfoQ 2016-01-14
0到1:微信后台系统的演进之路 原创 张文瑞 InfoQ 2016-01-14
- (008)每日SQL学习:Oracle Not Exists 及 Not In 使用
今天遇到一个问题,not in 查询失效,我以为是穿越了,仔细查了点资料,原来理解有误! select value from temp_a a where a.id between 1 and 100 ...
- Redis下载及安装(windows版)
下载地址1.Github下载地址:https://github.com/MicrosoftArchive/redis/releases2.百度网盘下载地址 https://pan.baidu.com/ ...
- 十一:SpringBoot-事务管理
SpringBoot-事务管理 1.事务管理简介 1.1 特性:ACID 1.2 隔离问题 1.3 隔离级别 2.Spring事务管理 2.1 顶级接口 2.2 事务状态 2.3 事务定义 1.事务管 ...
- (二)SpringBoot应用运维脚本
SpringBoot应用运维脚本 一.获取PID 二.Kill命令 三.nohup命令 四.编写SpringBoot应用运维脚本 4.1全局变量 4.2编写核心方法 4.3Info方法 4.4stat ...
- ajax 用fom提交
$.ajax({ type : "POST", url : "${ctx}/credit/LoanauditCtrl/qwe.do?hetong="+heton ...