Advertisement
a53

xor

a53
Jun 21st, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define NMAX 100000
  5. #define KMAX 20
  6. #define VMAX (1 << KMAX)
  7. using namespace std;
  8. int a[NMAX];
  9. int count[VMAX];
  10.  
  11. int main()
  12. {
  13. FILE* fin = fopen("xor.in", "r");
  14. FILE* fout = fopen("xor.out", "w");
  15.  
  16. int n, m;
  17.  
  18. fscanf(fin, "%d %d", &n, &m);
  19.  
  20. int i, k, j;
  21. for (i = 0; i < n; i++)
  22. {
  23. fscanf(fin, "%d", &a[i]);
  24. }
  25.  
  26. int comb = n * (n - 1) / 2;
  27.  
  28. long long countXor0;
  29. int len;
  30.  
  31. len = 2;
  32.  
  33. int x = 0;
  34. for (k = KMAX - 1; k >= 0; k--)
  35. {
  36. memset(count, 0, sizeof(count));
  37. for (i = 0; i < n; i++)
  38. {
  39. count[a[i] >> k]++;
  40. }
  41.  
  42. countXor0 = 0;
  43. x = x << 1;
  44.  
  45. for (i = 0; i < len; i++)
  46. {
  47. //acum nr de posibilitati astfel incat sa avem (x0)
  48. j = i ^ x;
  49. if (i == j)
  50. {
  51. countXor0 += (long long)count[i] * (count[i] - 1) / 2;
  52. }
  53. if (i < j)
  54. {
  55. countXor0 += (long long)count[i] * count[j];
  56. }
  57. }
  58.  
  59. if (countXor0 < m)
  60. {
  61. //alegem 1
  62. x++;
  63. m -= countXor0;
  64. }
  65.  
  66. len = len * 2;
  67. }
  68.  
  69. fprintf(fout, "%d", x);
  70.  
  71. fclose(fin);
  72. fclose(fout);
  73.  
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement