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 ...
随机推荐
- HDU 5496——Beauty of Sequence——————【考虑局部】
Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- Entity Framewrok 7beta7中不同版本sql server自动生成分页sql语句的问题
在EF中,使用linq进行分页是很方便的,假如我们有一个EMP表,结构如下: public class Emp { [Key] public Guid No { get; set; } public ...
- electron 开发记录
判断是否开发环境 安装 electron-is-dev npm install electron-is-dev // main.js const isDev = require('electron-i ...
- ios Lable 添加删除线
遇到坑了: NSString *goodsPrice = @"230.39"; NSString *marketPrice = @"299.99"; NSStr ...
- java网络编程—TCP(1)
演示tcp的传输的客户端和服务端的互访. 需求:客户端给服务端发送数据,服务端收到后,给客户端反馈信息. 客户端: 1,建立socket服务.指定要连接主机和端口. 2,获取socket流中的输出流. ...
- Excel2Dataset
//获取用户打开的Excel文档路径 private stringkkk() { OpenFileDialog selectFile = new OpenFileDialog(); selectFil ...
- SQL 语句实现行转列
CREATE TABLE #tempcloum( [productNum] varchar() null, [year1] decimal not null, [year2] decimal not ...
- linux服务器免密钥登录
方法一:通过下载服务器私钥方式 服务器端执行: ssh-keygen -t rsa -b 4096 cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys ...
- GitLab一个非标准的端口远程仓库导致clone失败
首先看下报错信息 当gitlab服务器ssh端口不是默认的22时,使用ssh连接gitlab会出现上面的错误 解决方法: 修改/etc/gitlab/gitlab.rd gitlab_rails['g ...
- graylog插件的安装
什么是插件和插件的作用我就不说了,大家应该都知道了. 安装方法是打开下面选择项 进去后出现如下界面 选择事先下载好的插件后,点击上传.这里以nginx日志插件为例 之后查看效果,发现对应的过滤 ...