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 ...
随机推荐
- kafka学习(七)
跨集群数据镜像 跨集群镜像的使用场景 1.区域集群和中心集群 2.冗余,发生紧急情况下使用第二个集群,保存相同的数据. 3.云迁移 多集群架构 跨集群中心通信的一些现实情况 1.高延迟 2.有 ...
- CentOSLinux安装Docker容器
Docker 使用 环境说明 CentOS 7.3(不准确地说:要求必须是 CentOS 7 64位) 不建议在 Windows 上使用 Docker 基本概念 官网:https://www.dock ...
- P1622释放囚犯
这是一道绿题,是一道让人想用贪心但却是区间DP的题目,难倒了我这个蒟蒻. 这个题其实仔细观察是类似于石子合并的!合并石子的代价便是肉的数量,求最小代价.所以我们设dp[i][j]为释放第i个到第j个所 ...
- 对C++拷贝构造函数的一点理解
一. 什么是拷贝构造函数 先看一个简单的例子: #include <iostream> using namespace std; class CExample { private: int ...
- web前后端数据交互
前后端数据交互是每一名web程序员必须熟悉的过程,前后端的数据交互重点在于前端是如何获取后端返回的数据,毕竟后端一般情况下只需要将数据封装到一个jsonMap,然后return就完了.下面通过一个li ...
- vscode调试php
xdebug调试vscode 下载xdebug.dll扩展库 php.ini配置 [XDebug] xdebug.remote_enable = xdebug.remote_autostart = z ...
- HTML A标签 href click事件冲突
转自:https://blog.csdn.net/xinglu/article/details/45199337
- Spring Boot实现SAAS平台的基本思路
一.SAAS是什么 SaaS是Software-as-a-service(软件即服务)它是一种通过Internet提供软件的模式,厂商将应用软件统一部署在自己的服务器 上,客户可以根据自己实际需求,通 ...
- 如何编写testbench的总结(非常实用的总结)
1.激励的设置 相应于被测试模块的输入激励设置为reg型,输出相应设置为wire类型,双向端口inout在测试中需要进行处理. 方法1:为双向端口设置中间变量inout_reg作为该inout的输出寄 ...
- PAT Advanced 1065 A+B and C (64bit) (20 分)(关于g++和clang++修改后能使用)
Given three integers A, B and C in [−], you are supposed to tell whether A+B>C. Input Specificati ...