Codeforces Round #443 (Div. 1) A. Short Program
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 500000 + 50; 4 int n, a[12][2], b[N]; 5 int ans[3];/* & ^ | */ 6 char op[N][2]; 7 int main(){ 8 scanf("%d", &n); 9 for (int i = 1; i <= n; ++i) scanf("%s%d", op[i] + 1, &b[i]); 10 for (int j = 1; j <= 10; ++j){ 11 a[j][0] = 0; a[j][1] = 1; 12 for (int i = 1; i <= n; ++i){ 13 int x = ((b[i] >> (j - 1))&1); 14 // cout << j << ": " << x << endl; 15 if (op[i][1] == '&'){ 16 a[j][0] &= x; 17 a[j][1] &= x; 18 } 19 if (op[i][1]== '|'){ 20 a[j][0] |= x; 21 a[j][1] |= x; 22 } 23 if (op[i][1] == '^'){ 24 a[j][0] ^= x; 25 a[j][1] ^= x; 26 } 27 } 28 // cout << j <<" " << a[j][0] << " " << a[j][1] << endl; 29 if (a[j][0] == 0 && a[j][1] == 0){ 30 continue; 31 }else if (a[j][0] == 0 && a[j][1] == 1){ 32 ans[0] |= (1 << (j - 1)); 33 }else if (a[j][0] == 1 && a[j][1] == 0){ 34 ans[0] |= (1 << (j - 1)); 35 ans[1] |= (1 << (j - 1)); 36 }else if (a[j][0] == 1 && a[j][1] == 1){ 37 ans[2] |= (1 << (j - 1)); 38 } 39 } 40 cout << 3 << endl; 41 cout << "& " << ans[0] << endl; 42 cout << "^ " << ans[1] << endl; 43 cout << "| " << ans[2] << endl; 44 return 0; 45 }
题目链接:http://codeforces.com/problemset/problem/878/A
题目大意
给出一大串位运算(or,and,xor), 称这组运算的集合位一个程序, 那么对于任意一个输入这个程序的数经过一系列位运算后,得到的数是一定的.现在要求你给出不超过5行位运算, 使得输入程序的数字在经过你这5行位运算后得到的数字与程序得到的数字一样.
题解
显然可以枚举每个二进制位, 然后暴力构建3行位运算(and,or,xor), 用人类智慧得出这一位上的值,然后连在一起输出即可.