fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int n;
  9. cin >> n;
  10.  
  11. // Read the sequence of operations
  12. vector<pair<char,int>> ops(n);
  13. for(int i = 0; i < n; i++){
  14. cin >> ops[i].first >> ops[i].second;
  15. }
  16.  
  17. // We'll build the three masks bit by bit.
  18. int AND_mask = 1023; // 10 bits of 1
  19. int OR_mask = 0;
  20. int XOR_mask = 0;
  21.  
  22. // For each bit position 0..9:
  23. for(int bit = 0; bit < 10; bit++){
  24. // Simulate starting from 0 and from 1.
  25. int v0 = 0, v1 = 1;
  26. for(auto &op : ops){
  27. char c = op.first;
  28. int x = (op.second >> bit) & 1;
  29. if(c == '&'){
  30. v0 &= x;
  31. v1 &= x;
  32. } else if(c == '|'){
  33. v0 |= x;
  34. v1 |= x;
  35. } else { // '^'
  36. v0 ^= x;
  37. v1 ^= x;
  38. }
  39. }
  40. // Decide what our combined masks must do to reproduce (v0, v1):
  41. if(v0 == 0 && v1 == 0){
  42. // force 0: AND=0, OR=0, XOR=0
  43. AND_mask &= ~(1<<bit);
  44. // OR_mask bit stays 0
  45. // XOR_mask bit stays 0
  46. }
  47. else if(v0 == 0 && v1 == 1){
  48. // identity: AND=1, OR=0, XOR=0
  49. // all masks already have these defaults
  50. }
  51. else if(v0 == 1 && v1 == 0){
  52. // invert: AND=1, OR=0, XOR=1
  53. XOR_mask |= (1<<bit);
  54. }
  55. else if(v0 == 1 && v1 == 1){
  56. // force 1: AND=1, OR=1, XOR=0
  57. OR_mask |= (1<<bit);
  58. }
  59. }
  60.  
  61. // Collect the result operations
  62. vector<pair<char,int>> result;
  63. if(AND_mask != 1023) result.push_back({'&', AND_mask});
  64. if(OR_mask != 0) result.push_back({'|', OR_mask});
  65. if(XOR_mask != 0) result.push_back({'^', XOR_mask});
  66.  
  67. // Output
  68. cout << result.size() << "\n";
  69. for(auto &p : result){
  70. cout << p.first << " " << p.second << "\n";
  71. }
  72.  
  73. return 0;
  74. }
  75.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
0