您的位置:首页 > 其它

Codeforces Round #443 (Div. 2): C. Short Program

2017-10-27 11:58 573 查看
C. Short Program

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Examples

input
3
| 3
^ 2
| 1


output
2
| 3
^ 2


input
3
& 1
& 3
& 5


output
1
& 1


input
3
^ 1
^ 2
^ 3


output
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];
else
ans1 ^= 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: