Codeforces Round #443 (Div. 2): C. Short Program
来源:互联网 发布:程序员逻辑分析面试题 编辑:程序博客网 时间:2024/06/10 00:09
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
题意:
给你一个只包含('&', '^', '|')的计算步骤(输入及数据范围[0, 1023]),你需要构造出另一个计算步骤,要求
①两个计算步骤等效,即同样的输入能得出同样的输出
③你构造的计算步骤不能超过5步
结论:只需要一次^,一次&,一次|即可
方法:令x = 0,y = 1023,之后通过题目中的计算步骤算出x'和y'
通过判断x'和y'每一位的状态判断当前位是^还是&还是|
复杂度O(n)
#include<stdio.h>int a[500005];char str[500005];int main(void){int n, i, ans1, ans2, ans, c, d, e;scanf("%d", &n);for(i=1;i<=n;i++)scanf(" %c%d", &str[i], &a[i]);ans1 = 0, ans2 = 1023;for(i=1;i<=n;i++){if(str[i]=='|')ans1 |= a[i], ans2 |= a[i];else if(str[i]=='&')ans1 &= a[i], ans2 &= a[i];elseans1 ^= a[i], ans2 ^= a[i];}c = e = 0;d = 1023;for(i=0;i<=9;i++){if(ans1&(1<<i) && ans2&(1<<i))c ^= (1<<i);else if(ans1&(1<<i) && (ans2&(1<<i))==0)e ^= (1<<i);else if((ans1&(1<<i))==0 && (ans2&(1<<i))==0)d ^= (1<<i);}printf("3\n");printf("& %d\n", d);printf("^ %d\n", e);printf("| %d\n", c);return 0;}
- Codeforces Round #443 (Div. 2): C. Short Program
- Codeforces Round #443 (Div. 2) C Short Program
- Codeforces Round #443 (Div. 2) C. Short Program
- Codeforces Round #443 (Div. 2) C. Short Program
- Codeforces Round #443 (Div. 2) C. Short Program 【模拟】
- Codeforces Round #879 (Div. 2) C. Short Program
- Codeforces Round #443(Div.2) C.Short Program(位运算+思维)
- Codeforces #443 Div.2 C. Short Program 【位操作】
- Codeforces Round #443 (Div. 1) A. Short Program
- Codeforces 879C Short Program【思维】
- CF #443 C Short Program
- 【codeforces】Codeforces Round #370 (Div. 2) C
- Short Program CodeForces
- Codeforces Round #443 (Div. 2)
- Codeforces Round #443 (Div.2)
- Codeforces Round #443 (Div. 2)
- Codeforces Round #443 (Div.2)
- Codeforces Round #443 (Div.2)
- spring多数据源的处理 mybatis实现跨库查询 实现Myibatis动态sql跨数据库的处理 Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样
- 欢迎使用CSDN-markdown编辑器
- ◆竞赛题目◆◇NOIP2015普及组◇求和
- Linux下部署oracle
- windows10编译 Pycocotools出错解决方案
- Codeforces Round #443 (Div. 2): C. Short Program
- Minimum Inversion Number HDU-1394
- iMindMap思维导图集结令
- CatchSync算法原理及应用
- Codeforces #443 Div.2 C. Short Program 【位操作】
- Webpack配置详解(package.json/webpack.config.js详细配置 )
- 微信 JS-SDK
- 在日常的开发中,我们有时候确实需要跨域的访问,怎么办?
- 线程通信