443 C. Short Program
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.
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行的程序使得运算结果和给出程序的运算结果相同
这题注意几点:
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的更多相关文章
- 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. Short Program
A. Short Program link http://codeforces.com/contest/878/problem/A describe Petya learned a new progr ...
- Codeforces Round #879 (Div. 2) C. Short Program
题目链接:http://codeforces.com/contest/879/problem/C C. Short Program time limit per test2 seconds memor ...
- [Codeforces Round #443]Div2 C Short Program
给你一串$n$个按顺序的位运算操作(&,|,^),形如"opt x",要求用不超过5行的位运算,按顺序操作出来和那个结果是一样的.$(n<=5e5,0<=x&l ...
- 【Codeforces Round #443 (Div. 2) C】Short Program
[链接] 我是链接,点我呀:) [题意] 给你一个n行的只和位运算有关的程序. 让你写一个不超过5行的等价程序. 使得对于每个输入,它们的输出都是一样的. [题解] 先假设x=1023,y=0; 即每 ...
- Codeforces Round #443 (Div. 2) C: Short Program - 位运算
传送门 题目大意: 输入给出一串位运算,输出一个步数小于等于5的方案,正确即可,不唯一. 题目分析: 英文题的理解真的是各种误差,从头到尾都以为解是唯一的. 根据位运算的性质可以知道: 一连串的位运算 ...
- Codeforces878 A. Short Program
题目类型:位运算 传送门:>Here< 题意:给出\(N\)个位运算操作,要求简化操作数量,使之结果不受影响(数据在1023之内) 解题思路 我们发现数字的每一位是独立的.也就是说,每一个 ...
- Codeforces 879C/878A - Short Program
传送门:http://codeforces.com/contest/879/problem/C 本题是一个位运算问题——位运算的等价变换. 假设位运算符“&”“|”“^”是左结合的,且优先级相 ...
- Codeforces 878A - Short Program(位运算)
原题链接:http://codeforces.com/problemset/problem/878/A 题意:给出n个位运算操作, 化简这些操作, 使化简后的操作次数不多于5步. 思路:我们可以对二进 ...
随机推荐
- angular2 引入jquery
(注:以下命令都是在项目文件夹根目录下运行的,保证下载的文件都在项目的node_modules文件夹里面) 1. 运行 npm install jquery -save 后会发现在package.j ...
- FineReport中JS如何自定义按钮导出
FineReport支持多种不同的导出方式,直接使用FineReport内置导出按钮可以非常快捷方便的来对各种格式的输出,但是我们在web页面集成中的时候,往往只想将报表内容嵌入到iframe中,而工 ...
- FastDFS部署安装全过程
你好!欢迎阅读我的博文,你可以跳转到我的个人博客网站,会有更好的排版效果和功能. 此外,本篇博文为本人Pushy原创,如需转载请注明出处:https://pushy.site/posts/153205 ...
- linux 压缩解压命令zip、gz、tar.gz、bz2、tar.bz2、.tar.xz
linux压缩格式:.gz windows压缩格式:.zip .rar默认情况下,windows和linux都支持zip格式,都不需要安装额外软件. .zip格式 压缩zip /usr/bin/zip ...
- 普通用户查看Oracle参数的值
create or replace function get_param(p_name in varchar2)return varchar2as l_param_type number; l_in ...
- OpenStreetMap、googleMap等经纬度和行列号之间相互转化(python,JavaScript,php,Java,C#等)
python: # OpenStreetMap经纬度转行列号 def deg2num(lat_deg, lon_deg, zoom): lat_rad = math.radians(lat_deg) ...
- 微信小程序接口开发中解决https外网调试问题
准备工作 1.有一台阿里云的Linux CentOS6 服务器,安装支持ssl模块的Nginx 2.有自己的域名,把域名解析到指向该服务器IP,本例中将wx.wangjiang.net 解析到服务器I ...
- Spring手册
一.Spring 简介 二.结构体系 三.七大主要模块 四.Spring Maven依赖 五 .Sprinf framework 一.Spring 简介 spring是一个开源的轻量级的应用开发框架, ...
- Oracle AP更新供应商
/*l_return_status:S l_msg_count:0 l_msg_data: l_vendor_id:133003 l_party_id:236055 */ DECLARE l_ ...
- Hive的介绍及安装
简介 Hive 是基于 Hadoop 的一个数据仓库工具,可以将结构化的数据文件 映射为一张数据库表,并提供类 SQL 查询功能. 本质是将 SQL 转换为 MapReduce 程序. Hive组件 ...