How to find the number of multipliers from 3

It was a contest Q:

There are N numbers a [0], a [1] .. a [N - 1]. Initially, it is still 0. You need to perform two types of operations:

  • Increase the numbers between indices A and B by 1. This is represented by the command "0 A B"
  • Answer how many numbers between indices A and B are divisible by 3. This is represented by the command "1 A B".

Input: the first line contains two integers: N and Q.

Each of the following Q lines has either the form “0 A B” or “1 A B”, as mentioned above.

Conclusion: output 1 line for each of the requests of the form “1 A B” containing the required response for the corresponding request.

Input Example:

4 7 1 0 3 0 1 2 0 1 3 1
0 0 0 0 3 1 3 3 1 0 3

Output Example:

4 1 0 2

Limitations:

1 <= N <= 100000 1 <= Q <= 100000 0 <= A <= B <= N - 1

I have no idea how to solve this. you can help?

1 . , 3, i- i.

C:

#include <stdio.h>


int nums[100*1000+20];
int d[100*1000+20];
int e[100*1000+20];
int dah[100*1000+20];

int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    int h;
    for(h=0;h<n;h++)
        {d[h/100]++; e[h/1000]++; dah[h/10]++;}
    int test;
    for(test=0;test<q;test++)
    {
        int op,start,end;
        scanf("%d%d%d",&op,&start,&end);
        if(0==op)
        {
            int x;
            for(x=start;x<=end;x++)
            {
                nums[x]++;
                nums[x]%=3;
                if(nums[x]==0)
                {
                    d[x/100]++;
                    e[x/1000]++;
                    dah[x/10]++;
                }
                else if(nums[x]==1)
                {
                    d[x/100]--;
                    e[x/1000]--;
                    dah[x/10]--;
                }
            }
        }
        else if(1==op)
        {
            int f;
            int ans=0;
            for(f=start;f<=end;)
            {
                if(f%1000==0&&f+1000<end)
                {
                    ans+=e[f/1000];
                    f+=1000;
                }
                else if(f%100==0&&f+100<end)
                {
                    ans+=d[f/100];
                    f+=100;
                }
                else if(f%10==0&&f+10<end)
                {
                    ans+=dah[f/10];
                    f+=10;
                }
                else
                {
                    ans+=(nums[f]==0);
                    f++;
                }
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

3 k * 1000 (k + 1) * 1000, k * 100 (k + 1) * 100, 10. . .

+3
4

# 1:

, MODULUS, . N , N 5.

, (.. 0 MOD 3, 1 MOD 3, 2 MOD 3 ..):

a[0] = 0
a[1] = 1
a[2] = 2
a[3] = 0
a[4] = 1
a[5] = 2

, A B, 0, 1 2 . , 2, 3. 3, 0 . , , 0 , 1, 1, 2, 2, 0.

, . - , , .

, 0 5 :

a[0] = 1
a[1] = 2
a[2] = 0
a[3] = 1
a[4] = 2
a[5] = 0

A B, 3, - , 0 (2).

, A B, , 3.

# 2:

, [A, B] 3, / , , - . . , , , 3 [A, B], , .

+5

№ 3:

dcp. , . MOD 3 . , O(Q * N), N, Q 1 . . Ali dcp.

MOD%0, MOD%1, MOD%2 node . , O(log N), O(Q log N) . O(log N). MOD% 3 , ( ), , 3 , , , node . - O(Q log N), 1 sec. time limit.

, , .

+1

? -, . . 0 A B , ? , , 1 A B.

0

, , , , , modulus operation, , , %.

0

Source: https://habr.com/ru/post/1762699/


All Articles