fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int INF = 2e9+1;
  10. const int LINF = 2000000000000000001;
  11.  
  12. inline int power(int a, int b, int mod=M) {
  13. int x = 1;
  14. a %= mod;
  15. while (b) {
  16. if (b & 1) x = (x * a) % mod;
  17. a = (a * a) % mod;
  18. b >>= 1;
  19. }
  20. return x;
  21. }
  22.  
  23.  
  24. //_ ***************************** START Below *******************************
  25.  
  26.  
  27. const int N = 3e6+9;
  28.  
  29. vector<int> a;
  30.  
  31. int consistency1(int n){
  32.  
  33.  
  34. int ct = 0;
  35. for(int i=0; i<=n-4; i++){
  36. for(int j=i+1; j<=n-3; j++){
  37.  
  38. unordered_map<int,int> mp;
  39. for(int k=n-1; k>=j+2; k--){
  40. mp[a[k]]++;
  41. }
  42. for(int k=j+1; k<=n-2; k++){
  43. int xr = a[i] ^ a[j] ^ a[k];
  44.  
  45. ct += mp[xr];
  46.  
  47. mp[a[k+1]]--;
  48. if(mp[a[k+1]] == 0) mp.erase(a[k]);
  49. }
  50. }
  51. }
  52.  
  53. return ct * 24;
  54.  
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. int consistency2(int n){
  63.  
  64. unordered_map<int,int> mp;
  65. for(int k=2; k<=n-2; k++){
  66. for(int l=k+1; l<=n-1; l++){
  67. int xr = a[k] ^ a[l];
  68. mp[xr]++;
  69. }
  70. }
  71.  
  72.  
  73. int ct = 0;
  74. for(int j=1; j<=n-3; j++){
  75. for(int i=j-1; i>=0; i--){
  76. int xr = a[i] ^ a[j];
  77. ct += mp[xr];
  78. }
  79. for(int k=j+2; k<n; k++){
  80. int xr = a[j+1] ^ a[k];
  81. mp[xr]--;
  82. if(mp[xr] == 0) mp.erase(xr);
  83. }
  84. }
  85.  
  86. return ct * 24;
  87.  
  88. }
  89.  
  90.  
  91.  
  92. //* Optimize Unordered map with vector :
  93.  
  94. int consistency3(int n){
  95.  
  96. vector<int> mp(N, 0);
  97. for(int k=2; k<=n-2; k++){
  98. for(int l=k+1; l<=n-1; l++){
  99. int xr = a[k] ^ a[l];
  100. mp[xr]++;
  101. }
  102. }
  103.  
  104.  
  105. int ct = 0;
  106. for(int j=1; j<=n-3; j++){
  107. for(int i=j-1; i>=0; i--){
  108. int xr = a[i] ^ a[j];
  109. ct += mp[xr];
  110. }
  111. for(int k=j+2; k<n; k++){
  112. int xr = a[j+1] ^ a[k];
  113. mp[xr]--;
  114. }
  115. }
  116.  
  117. return ct * 24;
  118.  
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. int practice(int n){
  142.  
  143.  
  144. return 0;
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151. void solve() {
  152.  
  153. int n;
  154. cin>> n;
  155.  
  156. a.resize(n);
  157. for(int i=0; i<n; i++) cin >> a[i];
  158.  
  159. cout << consistency3(n) << endl;
  160.  
  161.  
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168. int32_t main() {
  169. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  170.  
  171. int t = 1;
  172. cin >> t;
  173. while (t--) {
  174. solve();
  175. }
  176.  
  177. return 0;
  178. }
Success #stdin #stdout 0.01s 26948KB
stdin
2
5
1 2 3 4 5
10
8 7 6 4 3 2 1 0 5 9
stdout
24
432