an array A how many indexes (i,j) are there and cumulative sum%K = 0?

Revision en3, by Komyona_neko, 2016-01-28 20:18:01

Problem Type 1)

Lets say I have an array A[x1,x2,x3,...xN] of size N.

for N = 4 , A = {x1,x2,x3,x4}. [1 based index]

Now,I have to tell how many tuples (i,j) are there such that i<=j and cumulative sum(i,j) is divisible by K.

For example, N = 4

A = { 3,2,5,7} and K = 5.

Now , For this case the answer is 3. as, ( 1,2 ) [cum_sum = 5 ,divisible by K(5)], (3,3) [cum_sum = 5 ,divisible by K(5)] , (1,3) [ cum_sum = 10 ,divisible by K(5)] are such tuples.

I can do this in O(N^2) easily.But how to do this in O(N) or O(N*log(N)) ?

Please give me some problem links based on this (or similar) idea/trick from CodeForces.

Tags o(n), array, cumulative sum

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English Komyona_neko 2016-01-28 20:18:01 67
en2 English Komyona_neko 2016-01-28 20:06:06 19
en1 English Komyona_neko 2016-01-28 19:16:03 746 Initial revision (published)