Блог пользователя EternalFire

Автор EternalFire, история, 6 лет назад, По-английски

Given an array A consist of N integers (N <= 100000, 0 <= Ai <= sqrt(i) for 1 <= i <= N). Count the number of triple (i, j, k) such that i <= j <= k and (Ai xor Aj xor Ak) = 0.

I tried many approaches but I couldn't optimize my O(N^2) solution. Is there any efficient algorithm to solve this problem?

  • Проголосовать: нравится
  • -6
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится +15 Проголосовать: не нравится

We can improve complexity to due to . Calculate frequencies of the elements while going from left to right, considering the Ak element in the triple. Now, we need to find the number of pairs i, j such that . Now, in time we can traverse the frequencies, considering element Aj, assuming , and adding freqAj × freqAi for Ai < Aj (that way we will count each pair once). Case of Ak = 0 should be handled separately — . The actual running time will be slightly better than since we don't traverse all the frequencies every iteration.