Codeforces Round #879 (Div. 2) C. Short Program
题目链接: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相关推荐
- Codeforces Round #277 (Div. 2) 题解
Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...
- 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. ...
- Codeforces Round #506 (Div. 3)
Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...
- 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 ...
- 构造 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 的例子可以 ...
- Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...
- Codeforces Round #712 Div.2(A ~ F) 超高质量题解(每日训练 Day.15 )
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #712 Div.2(A ~ F) 题解 比赛链接:https:// ...
- Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...
- Codeforces Round #700 (Div. 2) D2 Painting the Array II(最通俗易懂的贪心策略讲解)看不懂来打我 ~
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 整场比赛的A ~ E 6题全,全部题目超高质量题解链接: Codeforces Round #700 ...
最新文章
- hic染色体构想_了解微服务:从构想到起点
- 用python处理文本数据_用Python读取几十万行文本数据
- 1202此服务器的证书无效,ios - iOS:URLRequest错误域= NSURLErrorDomain代码= -1202“此服务器的证书无效 - 堆栈内存溢出...
- QT+vs2010下改变可执行程序的图标
- WCF服务实例激活类型编程与开发(转)
- 又是一年国庆假期最后一天
- python下载以后在哪找_Python下载要保存到哪
- 查询软件和硬件列表清单[将文章里代码另存为 list.vbs,双击运行就会出现一个html页面]...
- 大家一起来谈论C#的前途
- hping3使用详解
- office2003无法正常安装卸载问题解决
- JavaScript模板引擎
- 商业智能应用的五大步骤
- 联想服务器查看raid信息,联想服务器raid信息恢复
- 算法题6 b站扭蛋机
- 两种图像骨架提取算法的研究原理及实现
- 51单片机控制LCD1602模块
- 如何利用Qt 3D 渲染与 Qt Quick 2D 元素结合创建太阳系行星元素?
- 远程计算机桌面登录拒绝访问,win7远程界面显示拒绝访问
- 关于热是什么与人体怎么感受到热的笔记