QSET - Editorial

Can anyone please explain the alternative solution in detail once more.
I cannot understand why answer will be choose(s1,2)+choose(s2,2)+choose(s3,2).

the links of Setter solution give xml error … please correct

The links to setters and tester’s solution is showing xml error! Please check the links!

please explain node3.count1[i] = node1.count1[i] + node2.count1[3-node1.total+i]

why this term:[3-node1.total+i] why not (i + node1.total) % 3

2 Likes

Editorial is pretty much clear… and explanatory… but can anyone plzz help me how to built a query function… means how can query for the range like 4 to 7 in segment tree when array size,n=8
only summing of node.ans is not enough so i am not able to manipulate how count1 and count2 is added… or it is to be added to find ans in the same way like in merge operation explained above…
@darkshadows
please explain it…Would be very much thankful if anybody can help me to understand…

count1[], where count1[i] denotes number of prefixes of interval which modulo 3 give i.

  • count2[], where count2[i] denotes number of suffixes of interval which modulo 3 give i.

what does this mean?

why are we doing this

 for i = 0 to 2:
        for j = 0 to 2:
            //if adding suffix of node1 with modulo i
            //to prefix of node2 with modulo j
            //gives us a subarray divisible by 3
            if (i + j) % 3 == 0:

in the pseudo code ?

what function choose(x,y) does?

how could i modify this to check if the substring between C and D inclusive is divisible by 3 ??
Or am i trying to explore a wrong approach for doing this ?

#include <stdio.h>

int main()
{
int rows, cols, i, j;

/*
 * Reads number of rows, columns to be printed
 */
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

for(i=1; i<=rows; i++)
{
    for(j=1; j<=cols; j++)
    {
        //Print 1 if its first or last row
        //Print 1 if its first or last column
        if(i==1 || i==rows || j==1 || j==cols)
        {
            printf("*");
        }
        else
        {
            printf(" ");
        }
    }

    printf("\n");
}

return 0;

}

why don’t you use CHelper + IntelliJ IDEA?

I’m an Eclipse guy. I tried IntelliJ IDEA and also NetBeans several times but I do not like those (maybe there is not rational reason for that)…

What I do not like on IDEs (not sure now if it is the same in IntelliJ) the most is, that there have to be just one main project/class and especially for contests I’m using it different way (I have same experience with all C++ IDEs, also Eclipse for C/C++ one :frowning: ) that’s my biggest problem in using other IDEs… In Eclipse I’m using run this class as Java/JUnit and I’m happy…

Ok. But, just so you know … you can achieve the same in IntelliJ IDEA.

1 Like

yes, you are right, that was probably a typo. fixed that!

Here is my code based on the above approach …

I also coded it during the contest itself … :smiley:

I don’t know what choose(sx, 2) means, but you can read my comment above if it helps :slight_smile:

Someone please fix the “Access Denied” issue!

1 Like

@akumar3 : I saw your solution for this problem.
https://www.codechef.com/viewsolution/5863202
Can you tell why have you incremented curr when i = 0 in processResult() ?

That is because the case with 0 remainder is special.
For every two prefixes with equal remainder, we have 1 substring which is divisible by 3;

Now consider following string:

33

Prefixes:

3, 33 (both gives remainder 0 when divided by 3)

If you apply above formula, you will get ‘1’ substring which is divisible by 3.
But here both the original prefixes are also divisible by 3.

To account this fact, we have to update our formula for the case when remainder is 0, so if we have N prefixes which gives remainder 0; our answer will be:

= (N(N-1))/2 + N (original prefixes)

= (N*(N+1))/2

1 Like

Thank you @akumar3.