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
output
input
output
input
output
题意:
给你一个只包含('&', '^', '|')的计算步骤(输入及数据范围[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;
}
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;
}
相关文章推荐
- Codeforces Round #443 (Div. 2) - C. Short Program
- Codeforces Round #443 (Div. 1) A. Short Program
- Codeforces Round #443 (Div. 2) A. Borya's Diagnosis
- Codeforces Round #443 (Div. 2) D. Teams Formation
- Codeforces Round #443 (Div. 2) B. Table Tennis On模拟(+跳出)
- Codeforces Round #443 (Div. 2) C Short Program
- Codeforces Round #231 div2前三题
- Codeforces Round #415 (Div. 2)
- Codeforces Round 347 div1 abc 662BD 663A
- Codeforces Round #277.5(Div. 2) C. Given Length and Sum of Digits...【贪心】
- Codeforces Round #184 (Div. 2) E. Playing with String(博弈)
- Codeforces Round #277.5(Div. 2) F. Special Matrices【思维+Dp】好题~好题~
- Codeforces Beta Round #80 (Div. 2 Only)
- Codeforces Round #378 (Div. 2) A B 题
- 【Codeforces Round 362 (Div 2)D】
- Codeforces Round #410 (Div. 2) D. Mike and distribution(贪心)
- 【Codeforces Round 365 (Div 2)B】【容斥】Mishka and trip 环加完全点图的边权乘积和
- Codeforces Beta Round #92 (Div. 2 Only) B. Permutations 模拟
- Codeforces Round #249 (Div. 2) B. Pasha Maximizes
- 【瞎搞】 Codeforces Round 276 DIV 2 C.Bits