RainX_69

CODEFORCES DIV-841 C/ IMPORTANT QUESTION

Dec 28th, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | Source Code | 0 0
  1. Link- https://codeforces.com/contest/1731/problem/C
  2.  
  3. You are given an integer array a1,a2,…,an (1 ≤ ai ≤n).
  4.  
  5. Find the number of subarrays of a whose XOR has an even number of divisors. In other words, find all pairs of indices (i,j) (i≤j) such that ai⊕ai+1⊕⋯⊕aj has an even number of divisors.
  6.  
  7. For example, numbers 2, 3, 5 or 6 have an even number of divisors, while 1 and 4 — odd. Consider that 0 has an odd number of divisors in this task.
  8.  
  9. Here XOR (or ⊕) denotes the bitwise XOR operation.
  10.  
  11.  
  12.  
  13. Print the number of subarrays.
  14.  
  15. Input-
  16. Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤10^4).
  17. Description of the test cases follows.
  18. The first line of each test case contains a single integer n (2 ≤ n ≤210^5) — the length of the array a.
  19. The second line contains n integers a1,a2,…,an (1 ≤ ai ≤n).
  20. It is guaranteed that the sum of n over all test cases does not exceed 210^5.
  21.  
  22. Output- For each test case, print the number of subarrays, whose XOR has an even number of divisors.
  23.  
  24. Example
  25.  
  26. input
  27. 4
  28. 3
  29. 3 1 2
  30. 5
  31. 4 2 1 5 3
  32. 4
  33. 4 4 4 4
  34. 7
  35. 5 7 3 7 1 7 3
  36.  
  37. output
  38. 4
  39. 11
  40. 0
  41. 20
  42.  
  43. Note
  44. In the first test case, there are 4 subarrays whose XOR has an even number of divisors: [3], [3,1], [1,2], [2].
  45.  
  46. In the second test case, there are 11 subarrays whose XOR has an even number of divisors: [4,2], [4,2,1], [4,2,1,5], [2], [2,1], [2,1,5], [2,1,5,3], [1,5,3], [5], [5,3], [3].
  47.  
  48. In the third test case, there is no subarray whose XOR has an even number of divisors since XOR of any subarray is either 4 or 0.
  49.  
  50.  
  51. /*-------------------------------------------------------------------------------------------------------------------------------------
  52.  
  53. THOUGHTS-
  54.  
  55. This problem is analogous to the problem [FIND NUMBER OF SUBARRAY WITH SUM K], the only difference here is there is no exact k, We use exclusion principle to do it. What we do here is first find the number of subarrays with XOR as square number, cuz square numbers like 0,1,4,9,16,25,.... these square numbers always have odd number of divisors. WHY? see, 1 and the number itself are divisors, but the square root of it is also a divisor and thus makes it odd count of divisors. Now, what we do is find all subarrays with square XOR, and subtract them from total number of subarrays i.e. n*(n+1)/2, giving us good subarrays. Now, how we do that, we iterate in our array one by one, and keep XORing every element, now after XORing the current element, we wanna see what XOR value can be removed from this current prefix XOR, so that the subarray becomes a square XOR. Similar to what we do in [subarray sum K], there we do sumTillNow-k to get a count of prefix sums with this values and these subarrays if subtracted from current Prefix Sum will result in a subarray with sum K. Similar we do here, but to find a XOR which we need to remove, we do this operation [xorTillNow ^ square Number] = xor to remove from prefix to get current subarray as square XOR.... This is very much analogous to [sumTillNow-k] similar to  [xorTillNow ^ square Number]. We loop over from i=0 till our i * i < 2 * n, and check if there is a prefix XOR which can be removed.
  56.  
  57. -------------------------------------------------------------------------------------------------------------------------------------*/
  58.  
  59.  
  60. CODE-
  61.  
  62. -------------------------------------------------------------------------------------------------------------------------------------
  63. USING UNORDERED HASHMAP (TLE)
  64.  
  65. #include<bits/stdc++.h>
  66. using namespace std;
  67.  
  68. void solve(){
  69.   int n;
  70.   cin>>n;
  71.   int arr[n];
  72.   for(int i=0;i<n;i++){
  73.       cin>>arr[i];
  74.   }
  75.   long long int res=0;
  76.   unordered_map<long long int,int> mpp;
  77.   long long int currXOR=0;
  78.   for(int i=0;i<n;i++){
  79.       currXOR=currXOR ^ arr[i];
  80.       for(int j=0;j*j<2*n;j++){
  81.           long long int sq=j*j;
  82.           if(currXOR==sq){
  83.               res++;
  84.           }
  85.           if(mpp.find(currXOR ^ sq)!=mpp.end() && (currXOR ^ sq) < 2 * n ){
  86.               res+=mpp[currXOR ^ sq];
  87.           }
  88.       }
  89.       mpp[currXOR]++;
  90.   }
  91.   cout<<((long long int)n*(n+1)/2)-res<<endl;
  92. }
  93.  
  94. int main(){
  95.     int TC;
  96.     cin>>TC;
  97.     while(TC--){
  98.       solve();
  99.     }
  100. }
  101. -------------------------------------------------------------------------------------------------------------------------------------
  102.  
  103. USING FREQ ARRAY INSTEAD OF HASHMAP  (ACCEPTED)
  104.  
  105. #include<bits/stdc++.h>
  106. using namespace std;
  107.  
  108. void solve(){
  109.   int n;
  110.   cin>>n;
  111.   int arr[n];
  112.   for(int i=0;i<n;i++){
  113.       cin>>arr[i];
  114.   }
  115.   long long int res=0;
  116.   vector<long long int> freq(2*n,0);
  117.   long long int currXOR=0;
  118.   for(int i=0;i<n;i++){
  119.       currXOR=currXOR ^ arr[i];
  120.       for(int j=0;j*j<2*n;j++){  // as per your constraint, your element will never be greater than 2 * n.
  121.           long long int sq=j*j;
  122.           if(sq==currXOR){
  123.               res++;
  124.           }
  125.           if((currXOR ^ sq) < 2 * n){ // as per your constraint, your element will never be greater than 2 * n.
  126.               res+=freq[currXOR ^ sq];
  127.           }
  128.       }
  129.       freq[currXOR]++;
  130.   }
  131.   cout<<((long long int)n*(n+1)/2)-res<<endl;
  132. }
  133.  
  134. int main(){
  135.     int TC;
  136.     cin>>TC;
  137.     while(TC--){
  138.       solve();
  139.     }
  140. }
  141.  
  142.  
Advertisement
Add Comment
Please, Sign In to add comment