fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long /////////////all are the same ( maybe i mixed between these alot !! )
  5.  
  6. signed main() {
  7.  
  8. int tt=1;
  9. cin>>tt;
  10.  
  11. for(int i =1 ;i <= tt; i++){
  12.  
  13. long long a,b;cin>>a>>b;
  14.  
  15.  
  16.  
  17. long long s = a + b;
  18. long long new_b = -1;
  19.  
  20.  
  21.  
  22. if ((b & s) == b) {
  23. new_b = b; // b is already a submask of s — zero cost, optimal
  24. } else {
  25. // divergence-point search loop ...
  26.  
  27.  
  28.  
  29. for (int bit = 0; bit <= 30; bit++) {
  30.  
  31. long long val = 1LL << bit;
  32.  
  33. // Candidate divergence point requires: b has 0 here (so new_b
  34. // can have 1 and become bigger), and s has 1 here (new_b must
  35. // be a submask of s).
  36. if ((b & val) || !(s & val))
  37. continue;
  38.  
  39. bool possible = true;
  40.  
  41. // Above the divergence point, new_b must equal b exactly.
  42. // So every 1-bit of b up there must also exist in s.
  43. for (int j = bit + 1; j <= 30; j++) {
  44. long long higher = 1LL << j;
  45. if ((b & higher) && !(s & higher)) {
  46. possible = false; // can't reproduce b's prefix using s's bits
  47. break;
  48. }
  49. }
  50.  
  51. if (possible) {
  52. // Build new_b: same prefix as b above this bit, 1 at this
  53. // bit, 0 below (smallest choice — lower bits aren't needed).
  54. new_b = (b >> (bit + 1)) << (bit + 1);
  55. new_b |= val;
  56. break; // lowest achievable divergence point = smallest new_b
  57. }
  58. }
  59.  
  60. }
  61.  
  62.  
  63.  
  64.  
  65. // No divergence point found => b is already a submask of s.
  66. if (new_b == -1) {
  67. new_b = b;
  68. }
  69.  
  70. cout << s << ' ' << new_b - b << '\n';
  71.  
  72.  
  73.  
  74. }
  75.  
  76.  
  77.  
  78. }
Success #stdin #stdout 0.01s 5308KB
stdin
3
3 1
0 5
6 4
stdout
4 3
5 0
10 4