题目链接:http://codeforces.com/contest/879/problem/C

C. Short Program

time limit per test2 seconds
memory limit per test256 megabytes

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.

Input

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

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.

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.


别人写的太好了,不敢下手。

别人家的博客
之前题意了解错了,就是位运算的等价变换(看不懂的可以试试将位运算替换成加减)。


#include<bits/stdc++.h>
using namespace std;
int main()
{int a,b;a = 0,b = 1023;int n;scanf("%d",&n);char s[100];int num;while(n--){scanf("%s%d",s,&num);if(s[0] == '|'){a |= num;b |= num;}else if(s[0] == '&'){a &= num;b &= num;}else if(s[0] == '^'){a ^= num;b ^= num;}}int num_and = 0,num_or = 0,num_xor = 0;num_and = a | b;//0->0,1->1,可以与上b二进制表示中1的部分num_or = a & b;//0->1,1->1,两个二进制中都是1的部分num_xor = a & (b ^ 1023);//0->1,1->0,两个二进制中都变成1的部分printf("3\n");printf("| %d\n",num_or);printf("& %d\n",num_and);printf("^ %d\n",num_xor);return 0;
}

转载于:https://www.cnblogs.com/GoldenFingers/p/9107254.html

Codeforces Round #879 (Div. 2) C. Short Program相关推荐

  1. Codeforces Round #277 (Div. 2) 题解

    Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...

  2. Codeforces Round #579 (Div. 3) E.Boxers

    Codeforces Round #579 (Div. 3) E.Boxers 题目链接 There are n boxers, the weight of the i-th boxer is ai. ...

  3. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  4. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  5. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  6. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

  7. Codeforces Round #712 Div.2(A ~ F) 超高质量题解(每日训练 Day.15 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #712 Div.2(A ~ F) 题解 比赛链接:https:// ...

  8. Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...

  9. Codeforces Round #700 (Div. 2) D2 Painting the Array II(最通俗易懂的贪心策略讲解)看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 整场比赛的A ~ E 6题全,全部题目超高质量题解链接: Codeforces Round #700 ...

最新文章

  1. hic染色体构想_了解微服务:从构想到起点
  2. 用python处理文本数据_用Python读取几十万行文本数据
  3. 1202此服务器的证书无效,ios - iOS:URLRequest错误域= NSURLErrorDomain代码= -1202“此服务器的证书无效 - 堆栈内存溢出...
  4. QT+vs2010下改变可执行程序的图标
  5. WCF服务实例激活类型编程与开发(转)
  6. 又是一年国庆假期最后一天
  7. python下载以后在哪找_Python下载要保存到哪
  8. 查询软件和硬件列表清单[将文章里代码另存为 list.vbs,双击运行就会出现一个html页面]...
  9. 大家一起来谈论C#的前途
  10. hping3使用详解
  11. office2003无法正常安装卸载问题解决
  12. JavaScript模板引擎
  13. 商业智能应用的五大步骤
  14. 联想服务器查看raid信息,联想服务器raid信息恢复
  15. 算法题6 b站扭蛋机
  16. 两种图像骨架提取算法的研究原理及实现
  17. 51单片机控制LCD1602模块
  18. 如何利用Qt 3D 渲染与 Qt Quick 2D 元素结合创建太阳系行星元素?
  19. 远程计算机桌面登录拒绝访问,win7远程界面显示拒绝访问
  20. 关于热是什么与人体怎么感受到热的笔记

热门文章

  1. 什么是BIOS?BIOS是什么
  2. 《深入理解OSGi:Equinox原理、应用与最佳实践》一3.2 Bundle状态及转换
  3. windows多线程详解
  4. jquery[学习心得]ajax的注意点
  5. 2008年信息安全服务市场发展报告
  6. Linux 开发环境工具 下载网址大全
  7. Python源代码文件的文本编码
  8. c语言酒店管理系统,C语言酒店管理系统.pdf
  9. @ARGV:perl命令行参数
  10. Node编写API接口,ajax实现增删改查