Codeforces Round #443 (Div. 2) C 位运算
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.
思路:取x=0,y=1023(即两个互为取反数的数字)。将n次位运算操作后的x,y值,对比即可得到每一位数字进行的 操作。
代码:
- #include<bits/stdc++.h>
- #define db double
- #include<vector>
- #define ll long long
- #define vec vector<ll>
- #define Mt vector<vec>
- #define ci(x) scanf("%d",&x)
- #define cd(x) scanf("%lf",&x)
- #define cl(x) scanf("%lld",&x)
- #define pi(x) printf("%d\n",x)
- #define pd(x) printf("%f\n",x)
- #define pl(x) printf("%lld\n",x)
- const int N = 1e6 + ;
- const int mod = 1e9 + ;
- const int MOD = mod - ;
- const db eps = 1e-;
- const db PI = acos(-1.0);
- using namespace std;
- bool cal(int a,int i){
- if(a&(<<i)) return ;
- return ;
- }
- int main(){
- int n;ci(n);
- int x=,y=;
- while (n--) {
- char c[];
- int t;
- scanf("%s %d", c, &t);
- if (c[] == '|') x|=t,y|=t;
- if (c[] == '&') x&=t,y&=t;
- if (c[] == '^') x^=t,y^=t;
- }
- /*
- 4种:
- x:0,y:1.
- x y:0011,0010,0111,0110
- */
- int v1 = , v2 = , v3 = ;
- for (int i = ; i < ; i++) {
- if(!cal(x,i)&&cal(y,i)) v2+=(<<i);//0011: |0 &1 ^0
- if(cal(x,i)&&cal(y,i)) v3+=(<<i);//0111: |0 &0 ^1
- if(cal(x,i)&&!cal(y,i)) v2+=(<<i),v3+=(<<i);//0110: |0 &1 ^1
- }
- printf("3\n");
- printf("| %d\n", v1);
- printf("& %d\n", v2);
- printf("^ %d\n", v3);
- return ;
- }
Codeforces Round #443 (Div. 2) C 位运算的更多相关文章
- Codeforces Round #443 (Div. 2) 【A、B、C、D】
Codeforces Round #443 (Div. 2) codeforces 879 A. Borya's Diagnosis[水题] #include<cstdio> #inclu ...
- Codeforces Round #443 (Div. 2) C: Short Program - 位运算
传送门 题目大意: 输入给出一串位运算,输出一个步数小于等于5的方案,正确即可,不唯一. 题目分析: 英文题的理解真的是各种误差,从头到尾都以为解是唯一的. 根据位运算的性质可以知道: 一连串的位运算 ...
- 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 ...
- Codeforces Round #443 (Div. 2)
C. Short Program Petya learned a new programming language CALPAS. A program in this language always ...
- 【Codeforces Round #443 (Div. 2) C】Short Program
[链接] 我是链接,点我呀:) [题意] 给你一个n行的只和位运算有关的程序. 让你写一个不超过5行的等价程序. 使得对于每个输入,它们的输出都是一样的. [题解] 先假设x=1023,y=0; 即每 ...
- Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算
D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...
- 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 ...
- Codeforces Round #443 Div. 1
A:考虑每一位的改变情况,分为强制变为1.强制变为0.不变.反转四种,得到这个之后and一发or一发xor一发就行了. #include<iostream> #include<cst ...
- Codeforces Round #443 (Div. 1) B. Teams Formation
B. Teams Formation link http://codeforces.com/contest/878/problem/B describe This time the Berland T ...
随机推荐
- spring数组注入
数组注入 public class MyCollection { private String[]array; private List<String>list; ...
- usb-host一步一步学(一)安卓在usb-host模式下列出当前连接的usb设备
在本次尝试中,我的安卓手机(HTC One X) 通过OTG线作为usb主机模式列出当前插入的usb设备,版本要求minSDKVersion="12". 没有外设的情况下,结果如下 ...
- 【解决】Git failed with a fatal error. Authentication failed for ‘http://......’
今天在visual studio中运行项目,打算pull最新的代码的时候,报错: Git failed with a fatal error. Authentication failed for ‘h ...
- 从零开始的全栈工程师——js篇2.5
数据类型与全局属性 js的本质就是处理数据 数据来自于后台的数据库所以变量就起到一个临时存储数据的这作用ECMAscirpt 制定了js的数据类型 一.数据类型 1.基本数据类型 基本数据类型就是简单 ...
- Javascript Number
Number 对象 Number对象是原始值的包装对象 创建Number对象的语法: var myNum = new Number(value): var myNum = Number(value): ...
- Android 从零开始搭建一个主流项目框架—RxJava2.0+Retrofit2.0+OkHttp
我这里的网络请求是用的装饰者模式去写的,什么是装饰者模式呢?在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象.我的理解就是一个接口, ...
- MySQL-5.6.30 (OpenLogic CentOS7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: centos7.2 mysql5.6.30 basic software database linux open source 服务优惠价: 按服务 ...
- redis在Windows下以后台服务一键搭建集群(多机器)
redis在Windows下以后台服务一键搭建集群(多机器) 一.概述 此教程介绍如何在windows系统中多台机器之间布置redis集群,同时要以后台服务的模式运行.布置以脚本的形式,一键完成.多台 ...
- iphone开发笔记
1.uiimage图片拉伸 - (void)stretchBackgroundImage { //UIImage *originalImage = [[self backgroundImageForS ...
- pat乙级1034
1.vs2013不能用scanf,改为scanf_s,但是提交时不能用scanf_s,用scanf... scanf_s(], &a[], &b[], &b[]); 2.c++ ...