Codeforces 878A - Short Program(位运算)
原题链接:http://codeforces.com/problemset/problem/878/A
题意:给出n个位运算操作, 化简这些操作, 使化简后的操作次数不多于5步。
思路:我们可以对二进制每一位上的1, 0, 进行讨论,
如果n次操作后1 -->1, 0 --> 1, 说明这一位要用或操作(or 1)
类似的,1 -->0, 0 -->1, 说明这一位要用异或操作(xor 1)
1 -->0, 0 -->0, 说明这一位要用与操作(and 0)
1 -->1, 0 -->0,前后不变,可以不用进行操作。
其中,and 0 操作可以用 or 1 xor 1代替, 那么最后化简后的操作次数不会超过 2 次。
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<string>
using namespace std;
typedef long long LL;
int XOR, OR;
int p[];
int b1,b2;
char ch[];
int num[][];
int cal(int b, int j, int i){
if(ch[j]=='|') return b|num[j][i];
if(ch[j]=='&') return b&num[j][i];
else return b^num[j][i];
}
int main()
{
int n,m;
p[]=;
for(int i=;i<;i++) p[i]=p[i-]*;
while(cin>>n)
{
XOR=OR=;
memset(num, , sizeof(num));
for(int i=;i<n;i++){
scanf(" %c %d", &ch[i], &m);
int t=;
while(m){
if(m&) num[i][t]=;
t++;
m>>=;
}
}
for(int i=;i<;i++){
b1=,b2=;
for(int j=;j<n;j++){
b1=cal(b1, j, i);
b2=cal(b2, j, i);
}
if(b1==&&b2==){//n and 1 相当于 n or 1 xor 1
OR^=p[i];
XOR^=p[i];
}
if(b1==&&b2==) OR^=p[i];
if(b1==&&b2==) XOR^=p[i];
}
int sum=;
if(OR) sum++;
if(XOR) sum++;
cout<<sum<<endl; if(OR) cout<<'|'<<' '<<OR<<endl;
if(XOR) cout<<'^'<<' '<<XOR<<endl;
}
}
Codeforces 878A - Short Program(位运算)的更多相关文章
- Codeforces Round #443 (Div. 2) C: Short Program - 位运算
传送门 题目大意: 输入给出一串位运算,输出一个步数小于等于5的方案,正确即可,不唯一. 题目分析: 英文题的理解真的是各种误差,从头到尾都以为解是唯一的. 根据位运算的性质可以知道: 一连串的位运算 ...
- Codeforces 879C/878A - Short Program
传送门:http://codeforces.com/contest/879/problem/C 本题是一个位运算问题——位运算的等价变换. 假设位运算符“&”“|”“^”是左结合的,且优先级相 ...
- Codeforces 868D Huge Strings - 位运算 - 暴力
You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...
- codeforces - 15C Industrial Nim(位运算+尼姆博弈)
C. Industrial Nim time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...
- Codeforces 868C Qualification Rounds - 位运算
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quar ...
- CodeForces - 1230D(思维+位运算)
题意 https://vjudge.net/problem/CodeForces-1230D 要组建一个小组,要求小组中每个人都不比所有人强,当一个人懂得一个算法但是另一个不懂那么前者认为他比后者强. ...
- 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 (Div. 2) C. Short Program
C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- cocos2dx基础篇(17) 音乐音效SimpleAudioEngine
[3.x] (1)获取单例:sharedEngine() 改为 getInstance() (2)实现了:音量的调节. (3)修改了播放音效 playEffect() 的参数: ...
- Golang中的error类型
Golang中的error类型 error类型本身就是一个预定义好的接口,里面定义了一个method type error interface { Error() string } 生成一个新的err ...
- myBatis 基于javaBean配置
MyBatis的持久化解决方案是将用户从原始的JDBC访问中解放出来,用户只需要定义需要操作的SQL语句, 无须关注底层的JDBC操作,就可以以面向对象的方式来进行持久化层操作.底层数据库连接的获取, ...
- Mysql-使用xtrabackup添加Slave
1.备份主库数据(主库操作) (1)安装innobackupex # yum -y install http://www.percona.com/downloads/percona-release/r ...
- [BZOJ 3456]城市规划(cdq分治+FFT)
[BZOJ 3456]城市规划(cdq分治+FFT) 题面 求有标号n个点无向连通图数目. 分析 设\(f(i)\)表示\(i\)个点组成的无向连通图数量,\(g(i)\)表示\(i\)个点的图的数量 ...
- 0ctf 2017 kernel pwn knote write up
UAF due to using hlist_add_behind() without checking. There is a pair locker(mutex_lock) at delete_n ...
- 分布式事务解决方案汇总:2PC、3PC、消息中间件、TCC、状态机+重试+幂等(转)
数据一致性问题非常多样,下面举一些常见例子.比如在更新数据的时候,先更新了数据库,后更新了缓存,一旦缓存更新失败,此时数据库和缓存数据会不一致.反过来,如果先更新缓存,再更新数据库,一旦缓存更新成功, ...
- TApplication,TForm,TControl,TComponent,TWinControl研究(博客索引)good
TApplication,TForm,TControl,TComponent,TWinControl研究 http://blog.csdn.net/suiyunonghen/article/detai ...
- 2019 NCTF Re WP
0x01 debug 测试文件:https://www.lanzous.com/i7kr2ta 1.Linux运行环境 在Linux上运行linux_server64文件 2.IDA配置 __int6 ...
- 关于jsp 获得当前绝对路径的方法
方法1) request.getRequestURL(); 方法2) request.getScheme()+"://"+request.getServerName()+&quo ...