879C
贪心
题目看错了。。。还以为是从操作序列中选5个。。。然后半个小时没了。。。
我们把每位分别用0和1带入,看看返回值是什么,然后分类讨论。千万不用特判!!!之前忘了删了就fst。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; const int N = 5e5 + 5; int n, x1, x2, x3; int a[N], mark[N]; char s[N][10]; int calc(int x, int t) { t = (t << x); for(int i = 1; i <= n; ++i) { if(s[i][0] == '|') t |= a[i]; if(s[i][0] == '&') t &= a[i]; if(s[i][0] == '^') t ^= a[i]; } return (t & (1 << x)) > 0; } int main() { scanf("%d", &n); for(int i = 1; i <= n; ++i) { int x; scanf("%s%d", s[i], &a[i]); for(int j = 0; j < 10; ++j) if(a[i] & (1 << j)) mark[j] = 1; } x2 = 1023; for(int i = 0; i < 10; ++i) { int x = calc(i, 0), y = calc(i, 1); // printf("i = %d x = %d y = %d\n", i, x, y); if(x == 0 && y == 0) x2 -= (1 << i); if(x == 1 && y == 0) x3 += (1 << i); // if(x == 0 && y == 1) x2 += (1 << i); if(x == 1 && y == 1) x1 += (1 << i); } int cnt = 3; if(!x1) --cnt; // if(!x2) --cnt; if(!x3) --cnt; printf("%d\n", cnt); if(x1) printf("| %d\n", x1); printf("& %d\n", x2); if(x3) printf("^ %d\n", x3); return 0; }