Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <limits>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <bitset>
  7. #include <set>
  8. #include <unordered_set>
  9. using namespace std;
  10.  
  11. using ll = long long;
  12. using ui = unsigned int;
  13.  
  14. #define int ll
  15.  
  16. vector<ui> a;
  17.  
  18. unordered_set<ui> bits;
  19. int diff = 0;
  20.  
  21. bool check(ui x) {
  22. ui cnt = __builtin_popcount(a[0] ^ x);
  23. for (ui y: a) if (__builtin_popcount(y ^ x) != cnt) return false;
  24. return true;
  25. }
  26.  
  27. void rec(int i = 0, ui mask = 0, ui cnt = 0) {
  28. if (cnt > diff) {
  29. return;
  30. }
  31. if (i == 32) {
  32. if (cnt == diff) {
  33. if (check(mask)) {if (cnt > diff) {
  34. return;
  35. }
  36. cout << mask << endl;
  37. exit(0);
  38. }
  39. }
  40. return;
  41. }
  42. rec(i + 1, mask, cnt);
  43. if (bits.count(i)) rec(i + 1, mask | (1 << i), cnt + 1);
  44. }
  45.  
  46. signed main() {
  47. ios_base::sync_with_stdio(0);
  48. cin.tie(0); cout.tie(0);
  49. int n;
  50. cin >> n;
  51. a.resize(n);
  52. for (int i = 0; i < n; i++) {
  53. cin >> a[i];
  54. }
  55. bitset<32> A(a[0]);
  56. bitset<32> B(a[0]);
  57. for (int x: a) {
  58. if (__builtin_popcount(x) != A.count()) {
  59. B = x;
  60. break;
  61. }
  62. }
  63. if (A.count() == B.count()) {
  64. cout << 0 << endl;
  65. return 0;
  66. }
  67. if (A.count() < B.count()) swap(A, B);
  68. diff = A.count() - B.count();
  69. if (diff % 2) {
  70. cout << -1 << endl;
  71. return 0;
  72. }
  73. diff /= 2;
  74. cout << A << " " << B << endl;
  75. for (int i = 0;i < 32; i++) {
  76. if (A[i] && !B[i]) {
  77. bits.insert(i);
  78. }
  79. }
  80. rec();
  81. cout << -1 << endl;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement