fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <cmath>
  6. #include <map>
  7. #include <deque>
  8. #include <set>
  9. #include <stack>
  10. #include <queue>
  11. #include <numeric>
  12. #include <cctype>
  13. #include <bits/stdc++.h>
  14. #include <string>
  15. #include <iomanip>
  16.  
  17. using namespace std;
  18. typedef long long ll;
  19. const int MOD = 1000000007;
  20. const int MOD2 = 998244353;
  21.  
  22. int main()
  23. {
  24. ios_base::sync_with_stdio(false);
  25. cin.tie(0);
  26.  
  27. // freopen("input.txt", "r", stdin);
  28.  
  29. int a, b, x;
  30. cin >> a >> b >> x;
  31.  
  32. string sa, sb, sx;
  33. cin >> sa >> sb >> sx;
  34.  
  35. // Convert binary string to integer
  36. // ll a = stoi(sa, nullptr, 2);
  37. // int b = stoi(sb, nullptr, 2);
  38. // int x = stoi(sx, nullptr, 2);
  39. // cout << a << endl;
  40. // cout << b << endl;
  41. // cout << x << endl;
  42.  
  43. if ((a & b) >= x)
  44. {
  45. cout << 0 << "\n";
  46. return 0;
  47. }
  48.  
  49. for (int i = 0; i < 31; i++)
  50. {
  51. int bit = 1 << i;
  52.  
  53. if (((a | bit) & b) >= x)
  54. {
  55. cout << 1 << "\n";
  56. cout << "a " << i << "\n";
  57. return 0;
  58. }
  59. if (((b | bit) & a) >= x)
  60. {
  61. cout << 1 << "\n";
  62. cout << "b " << i << "\n";
  63. return 0;
  64. }
  65. }
  66.  
  67. vector<string> v;
  68.  
  69. for (int i = 30; i >= 0 && ((a & b) < x); i--)
  70. {
  71. int bit = 1 << i;
  72. if ((x & (bit)))
  73. {
  74. if (!(a & bit))
  75. {
  76. string s = to_string(i);
  77. v.push_back("a " + s);
  78. }
  79.  
  80. if (!(b & bit))
  81. {
  82. string s = to_string(i);
  83. v.push_back("b " + s);
  84. }
  85. a |= bit;
  86. b |= bit;
  87. }
  88. else
  89. {
  90. int opp = 0;
  91. if (!(a & bit))
  92. {
  93. string s = to_string(i);
  94. v.push_back("a " + s);
  95. opp++;
  96. }
  97.  
  98. if (!(b & bit))
  99. {
  100.  
  101. string s = to_string(i);
  102. v.push_back("b " + s);
  103. opp++;
  104. }
  105.  
  106. if (opp == 2)
  107. {
  108. for (int j = i - 1; j >= 0; j--)
  109. {
  110. int nm = (1 << j);
  111. if ((a & nm) != (b & nm))
  112. {
  113. if (((a | nm) & (b | nm)) >= x)
  114. {
  115. if (!(a & nm))
  116. {
  117. v.pop_back();
  118. v.pop_back();
  119. string s = to_string(j);
  120. v.push_back("a " + s);
  121. a |= nm;
  122. }
  123. else
  124. {
  125. // cout << "ansb" << '\n';
  126. v.pop_back();
  127. v.pop_back();
  128. string s = to_string(j);
  129. v.push_back("b " + s);
  130. b |= nm;
  131. }
  132. }
  133. break;
  134. }
  135. }
  136. }
  137. a |= bit;
  138. b |= bit;
  139. }
  140. }
  141.  
  142. cout << v.size() << "\n";
  143. for (auto item : v)
  144. {
  145. cout << item << "\n";
  146. }
  147.  
  148. return 0;
  149. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
0