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. // cout << "have to edit a " << v.size() << " " << i << endl;
  77. string s = to_string(i);
  78. v.push_back("a " + s);
  79. }
  80.  
  81. if (!(b & bit))
  82. {
  83. // cout << "have to edit b " << v.size() << " " << i << endl;
  84. string s = to_string(i);
  85. v.push_back("b " + s);
  86. }
  87. a |= bit;
  88. b |= bit;
  89. }
  90. else
  91. {
  92. int opp = 0;
  93. if (!(a & bit))
  94. {
  95.  
  96. // cout << " edit a " << v.size() << " " << i << endl;
  97. string s = to_string(i);
  98. v.push_back("a " + s);
  99. opp++;
  100. }
  101.  
  102. if (!(b & bit))
  103. {
  104.  
  105. // cout << " edit b " << v.size() << " " << i << endl;
  106. string s = to_string(i);
  107. v.push_back("b " + s);
  108. opp++;
  109. }
  110.  
  111. if (opp == 2)
  112. {
  113. // cout << "HERE\n";
  114. // cout << v.size() << endl;
  115. // cout << i << '\n';
  116. for (int j = i - 1; j >= 0; j--)
  117. {
  118. int nm = (1 << j);
  119. if ((a & nm) != (b & nm))
  120. {
  121. // cout << "j" << j << " " << nm << " " << '\n';
  122. if (((a | nm) & (b | nm)) >= x)
  123. {
  124. // cout << "fffj" << j << '\n';
  125. if (!(a & nm))
  126. {
  127. // cout << "ansa" << '\n';
  128. v.pop_back();
  129. v.pop_back();
  130. string s = to_string(j);
  131. v.push_back("a " + s);
  132. a |= nm;
  133. }
  134. else
  135. {
  136. // cout << "ansb" << '\n';
  137. v.pop_back();
  138. v.pop_back();
  139. string s = to_string(j);
  140. v.push_back("b " + s);
  141. b |= nm;
  142. }
  143. }
  144. break;
  145. }
  146. }
  147. }
  148. a |= bit;
  149. b |= bit;
  150. }
  151. }
  152.  
  153. cout << v.size() << "\n";
  154. for (auto item : v)
  155. {
  156. cout << item << "\n";
  157. }
  158.  
  159. return 0;
  160. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
1
a 30