Скоро Codeforces Round 1133 (Div. 1, Div. 2)! Регистрация здесь. ×

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

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

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.

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

»
11 лет назад, скрыть # |
← Rev. 3  
Проголосовать: нравится +1 Проголосовать: не нравится

Hint: use that is divisible by precisely when .

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Sum(L, R) = Sum(1..R) — Sum(1..L-1)
So, let F[i] = (a[1] + a[2] + a[3] + ... + a[i]) % K
Then you just do it like this, since to get Sum(L,R) = 0 (mod K), you should have F[L-1] and F[r] equal mod K:
cnt[0] = 1;
for (int i = 1; i <= n; i++) {
ans += cnt[F[i]];
cnt[F[i]]++;
}

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I think this the same problem on codeforces ([Link]).(http://codeforces.me/problemset/problem/577/B)

See the tutorial for more help on it.