zscoder's blog

By zscoder, history, 3 years ago, In English,

Here are the editorials for all the problems. Hope you enjoyed them and found them interesting!

Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code (O(nkm^2))
Code (O(nkm))
Tutorial is loading...
Code
Tutorial is loading...
Code
 
 
 
 
  • Vote: I like it  
  • +100
  • Vote: I do not like it  

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by zscoder (previous revision, new revision, compare).

»
3 years ago, # |
Rev. 3   Vote: I like it -17 Vote: I do not like it

Nice editorial!

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it
  • »
    »
    3 years ago, # ^ |
      Vote: I like it +10 Vote: I do not like it

    Why use a "For" loop for calculating power of 2? Use Modular exponentiation by Squaring instead.

  • »
    »
    3 years ago, # ^ |
    Rev. 4   Vote: I like it +2 Vote: I do not like it

    Although a log(N) time complexity for computing the product is not the cause for the time out, I strongly recommend you to learn the fast exponential function.

    I am still reading the code to see if there is a possible infinite loop.

    Edit:

    Try this case:

    5

    2 3 1 3 4

    You failed to handle cycles properly.

    Edit: I think my English got murdered today.

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks , btw what is the output of that operation ?

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        24 if I am not mistaken, my code just passed the system test so it should be correct.

»
3 years ago, # |
  Vote: I like it +12 Vote: I do not like it

Problems were very nice: short and to the point! Editorial was also given so fast, and the queue also displayed the verdict on time without any delays. Everything was perfect and I solved 4 problems until I received "WA on 105" in C.

»
3 years ago, # |
  Vote: I like it +29 Vote: I do not like it

The main blog was deleted?

  • »
    »
    3 years ago, # ^ |
    Rev. 2   Vote: I like it +2 Vote: I do not like it

    Sorry it was my fault. I accidentally clicked "Save Draft" when I was trying to publish the winners and it went missing. It is back now.

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

For what test cases would these 2 solution for A and B not work? :(

2A — http://ideone.com/He9UCX

2B — http://ideone.com/ymynPJ

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    For 2A, it's incorrect if there is "XX|OO" where this is the first available row.

    For 2B you didn't take care of n=1

»
3 years ago, # |
Rev. 2   Vote: I like it -17 Vote: I do not like it

I failed on C because I declared the array as n*k*n instead of n*k*m..and problem b because I didnt verify the boolean in case the answer is not possible,,,Yes, these are the mistakes I made.. Today d saved me, otherwise my day could have been 100x worse...Nice problems. I was 120 before sys tests, 830 afterwards..Thats life mate...

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Same story about problem C. Solution was correct, but final cycle to find minimum value from last colors was from 1 to n instead of m.

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Could someone please explain Problem D more clearly. Do we just find the connected components using kosaraju's method and then proceed?

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +17 Vote: I do not like it

    First, consider the original oriented graph. Each node has exactly one edge going out. If we start at some point, we can follow the edges and obviously we will get looped (e.g. 1-2-3-4-5-3). For points inside loop, we will always stay inside loop. For other points, they will fall into some loop eventually.

    Conclusion: the graph consists of several disjoint cycles, and there are some "free" paths leading from "leaves" into some cycle. These paths do not intersect (only in cycle points).

    Now consider the unoriented graph, i.e. remove all directions. We can orient those "free" paths arbitrarily, independently: 2num_free_edges. And for each cycle, there are only two edge orientations that are bad for us — full clockwise cycle and full ccw cycle. Therefore for each cycle of length c we need to multiply the answer by 2c - 2.

    How to find cycles? We always have only one edge outgoing from any node (in the original directed graph). Let's go from each node and follow the edges until we meet an already visited node. We also keep track whether the node's cycle was already counted, and a "time" counter. Then when we get to the node second time, we check if its cycle was already counted and if not, we use the time difference to get the cycle length.

    Code
    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Okay, this definitely makes things more clearer. Thanks. :)

  • »
    »
    3 years ago, # ^ |
      Vote: I like it -6 Vote: I do not like it

    Yes, finding strongly connected components with Tarjan or Kosaraju and the rest you can solve by fast exponentiation.

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thank you. :)

      • »
        »
        »
        »
        3 years ago, # ^ |
        Rev. 2   Vote: I like it +3 Vote: I do not like it

        We don't need to find strongly connected components in this problem, since if we make the graph undirected, there will be only one simple cycle in each component, and we can find it by simply dfs and find the only back edge.

»
3 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

Problem E. Why do we calculate complement probability as but not as (with k! in denominator) ?

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    'cause we define the probability that no two ones have the same birth day as follow : k! *c[2^n][k] /((2^n)^k) now if u decompose c[2^n][k] in the numerator , u can find that we can delete the k! from both the numerator and denominator .

    this is true when k<=2^n

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      how from the problem statement follows that order matters? when I read it I thought, that order does not matter and therefore the probability you are speaking about is , not .

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I'm late but I'll try to explain. I think it's because the order in which we choose the birthdays matters, i.e. we assumed the i-th paranthesis in the numerator refers to the birthday of the i-th person.

    Example: let's have 3 days in a year (A, B, C) and 2 people.
    If we use the second formula you wrote, we'd have 3 cases for 'distinct birthdays': one person is born on A and the other on B, one is born on A and the other on C; and one is born on B and the other on C.
    If we use the author's formula we would have 6 cases for 'distinct birthdays'.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For 2E, why for test case 4 the output has to be "906300 906300"? Wouldn't "1 1" be the same?

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Because you want original numbers to be coprime (not the answer after modulus)

    ==> If it would be possible, the process would be following: A) Find two numbers B) Make them coprime C) Do modulo operation

    Here, it can happen that two numbers can be coprime, but after modulo, the results are not:

    Little example: A=3,B=8,MOD=5

    A and B are coprime, but "A % MOD" and "B % MOD" (3 / 3) are not!

    Good Luck & Have Nice Day! :) ^_^

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it +5 Vote: I do not like it

      But since k > MOD, A is guaranteed to be equal as B. What I don't get is why I must output (B%MOD)/(B%MOD) while 1/1 is not accepted . @_@

      UPD: Ahh I think I got it.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

My codes were accepted during the system checking but now its showing skipped and there is no change in my rating.My all submissions of the contest are showing "skipped". Can somebody please help me regarding this issue?

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Probably your codes were very similar to some one else.

»
3 years ago, # |
  Vote: I like it +1 Vote: I do not like it

I have a tendency of writing recursive solutions for DP problems. Will that be cause any problem in near future? Here is my accepted solution for DIV 2C (Complexity O(NKM2)). Can anyone help how to modify the same recursive solution so that complexity is reduced to O(NKM).

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can't find why is this WA?

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

dunnow why this results WA

http://codeforces.com/contest/711/submission/20256542

solved it ... thanks

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Silly doubt, but really helpful. What happens if k>n in Problem C. How does DP gives answer for that case?

  • »
    »
    3 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    supposing that ur dp parameters are :

    1 : the index of the tree (ind)

    2: how many different groups u have till now (diff)

    3: and the previous color (prev)

    of course when u reach ind=n(if u start enumerating the trees from 0 ,then u processed all the trees) and diff< k return oo ,

    so at the end ,when u finish ur dp function , if the result >= oo ,then there is no way to get k different groups ,so print -1;

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      But, how to prove that when k>n, result will be INF according to the dp made in editorial?

      • »
        »
        »
        »
        3 years ago, # ^ |
        Rev. 3   Vote: I like it 0 Vote: I do not like it

        You just don't have enough trees to have k groups.

»
3 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

In the editorial for E, these 2 lines are used to calculate the highest power of 2 which divides 1.2.3...k-1.

int digits = __builtin_popcountll(k — 1);

v2 = k — 1 — digits;.

Can somebody please elaborate on this formula.

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Never mind!

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For problem E, I think it should be (k-1)<MOD and not (k-1)<=MOD.

»
3 years ago, # |
Rev. 2   Vote: I like it -6 Vote: I do not like it

For problem c, Why complexity nkm^2 can pass all the test as n=k=m=100?

Won't it be TLE?

During the contest, I considered the solution of nkm^2. But because I am afraid to be TLE, I didn't write it.

sad...:(

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +9 Vote: I do not like it

    Now you know, 108 simple operations can be completed in < 1 second on Codeforces.

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      Oh maybe I am wrong, I always consider n2 for N = 103, n3 for N = 102, or for N = 105 for N = 2 × 105.

      Maybe my entire outlook should be changed...

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

for problem E : can anyone explain the details of the last part of the author's code — calculating and reducing the numerator — thanks in advance

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can you give me a webite link to explain "Legendre's formula" in the editorial of Problem E? I know nothing about that:(

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone explain the Div 2C in a top- down manner? I am not able to understand the editorial.

Thanks!

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    the function would look like this

    long long calc(int now,int k,int prev);
    

    now :the index of the current tree, k :the number of contiguous groups until now, prev : the color of the previous tree.

    now for every uncolored tree you should try each color then move to the next tree then minimize the result, if the the tree is colored just ignore it. but remember to increase k when you find two adjacent trees with different colors , when you reach the end just see if k equals the given number of beauty otherwise it is not a valid solution. here is my solution

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I did not get this line in first problem

if(bus[i][j*3] == 'O' && bus[i][j*3+1] == 'O')

please explain.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The cycle for j is from 0 to 1. So on the first iteration we have 3 * j and 3 * j + 1 equals to 0 and 1. That's indexes of the first pair seats. On the second we have 3 * j and 3 * j + 1 equals to 3 and 4. That's indexes of the second pair seats.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Very happy to see such fast rating update and also there was minimum queue delay. Editorials are great.

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

I didnt understand how problem-C was done in O(nkm). Can anyone please help. In the code given in editorial what is the purpose of m1[][], m2[][] and idx[][].

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    m1- global minimum for i-th for pretty k, m2- the second minimum, idx-where we can find it!

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Thanks, one more doubt, what does idx[][] = -2 mean? Does it mean that 2 minimum paths are possible so we can always pick minimum?

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

My submission on B got wrong answer on test case 32. But I still don't understand what was wrong with my logic. I calculate D=max(all rowsum,all columnsum,diagonal1,diagonal2)-min(all rowsum,all columnsum,diagonal1,diagonal2) . Then I added D(if D>0) to empty cell and updated all sums. Then I recheck if(max(all rowsum,all columnsum,diagonal1,diagonal2)-min(all rowsum,all columnsum,diagonal1,diagonal2) == 0) then print D, else -1. I have also taken care of n==1 case. Here's my Submission. Can someone point out my mistake?

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it
    if(q==w)
     d1+=d;
     if(n-w-1==q)
     d2+=a; // <-- What is this? Why not d?
    
    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Ohh! What a silly mistake! Thanks a lot for pointing it out.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Hey guys, I'm stuck at Problem D test 7 for 2 hours. I'don't know where did I get wrong.

can somebody help please? I can't appreciate you more. Here's my code: 20279080

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

how can i draw the 3 dimentional dp array in a piece of paper for problem C

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    No paper won't work, try using a cube.

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      that's right, but how can i apply in reality?

      I feel comfortable to use recursive dp. but drawing recursive dp table in paper is harder than drawing iterative dp table

  • »
    »
    3 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    You don't need to think in 3 dimensions for this problem. When you're considering the i'th tree, all you need to know is the states from the previous tree i - 1.

    All you need to keep in your head is 2 planes. Each plane contains all the possible states for the corresponding tree. We only need to know how these planes relate to each other, namely, how can we compute i'th plane from i - 1 plane.

    I imagine planes made from pixels (like monitor). These planes are almost transparent, so that I can put one screen on top of the other and see the screen on the bottom through the screen on top. I know the values of the pixels of the bottom screen and I need to calculate the values of the pixels of the top screen.

    To traverse all the pixels of the screen on top we need 2 loops:

    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++)
    

    To compute the value of the single pixel of the top_screen[x][y] we need to consider all of the points of the bottom_screen[][]. So, each pixel in the bottom screen contributes it's value to the pixel top_screen[x][y]. To traverse all of the pixels of the bottom screen we also need 2 nested loops:

    for (int xx = 0; xx < width; xx++)
        for (int yy = 0; yy < height; yy++)
    

    When we combine these thoughts together, we get a general structure for the program:

    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++)
            for (int xx = 0; xx < width; xx++)
                for (int yy = 0; yy < height; yy++)
                    top_screen[x][y] = contribution_of(bottom_screen[xx][yy]);
    
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I have a question regarding the C problem: could you please explain what does the "last colour" and "current colour" mean?

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    last color means the previous tree color and current color means you working which tree right now

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

The comment is hidden because of too negative feedback, click here to view it.

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Problem E is very interesting.

Is there an intuitive way to deduce the Legendre's Formula? I couldn't figure it out, I had to read a Wikipedia article :/

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    I thought like this: we have numbers 1,2,3,...,k-1 and we want to divide by 2 as many of them as possible and as many times as possible. First we count all even numbers, we divide them by 2: there are floor((k - 1) / 2) even numbers there. Then we count numbers divisible by 4. We have already divided them by 2, so we have to count them only once: floor((k - 1) / 4). Continuing, we get that the total product is 2t, with

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Hello. In problem E 'One way to resolve this is to reduce it modulo MOD - 1, since 2MOD - 1 ≡ 1 modulo MOD by Fermat's Little Theorem.' I could not understand this. Why we take MOD — 1? If anyone could explain in details I would be gratified. Many thanks in advance.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Fermat's Little Theorem states that ap ≡ a (mod p) // (if p is prime)

    In other words ap - 1 ≡ 1 (mod p) // (if a is not divisible by p)

    So instead of calculating xy (mod p) we can calculate x(y mod(p - 1)) (mod p). We can do that because of the second equation. We just substract p-1 from the exponent as long as we can since multiplication by 1 doesn't change the result.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In the solution of problem 711C, why the elements of dp array are being initialised to INF, when every time they are being updated by dp[i][j][a-1]+c[i][a-1]. Please clear my doubt

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    dp[][][] array is initialized to INF as we need to find minimum value, that is why you can see a min() function called each time when updating dp[][][] value

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

can the problem C be done using 2-D dp arraay ? if yes, how ?

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I think No, as the state requires 3 information to goto a unique next state. Anything less than 3 information per state will give us more than 1 state to goto, so we wont be able to decide which one to go for.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can somebody help take a look and point out what's wrong with my code for problem E? Thanks in advance. =D

http://codeforces.com/contest/711/submission/20341261

  • »
    »
    3 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    What do you do

    here

    ?

    Change it to

    this

    .

    If the real answer is you output p mod MOD and q mod MOD.

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Oh boy, that was dumb of considering p mod q instead of (p-q) mod MOD. :P

      Thanks for the help!

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

could somebody help me with question 711C
i am getting WA on test on test 12. I have applied the logic correctly, I guess so,
here is the code http://ideone.com/OW6sYs
Thnks

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I am afraid that INT_MAX is not good enough to be used as INF in this question.

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      thank you very much!!
      I changed it into 10^15+5 and it worked. ACCEPTED.
      But even LONG_MAX doesn't work and why does LONG_MAX gives negative values. Please suggest.

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        LONG_MAX = 2^32-1 is approximately 2e9, so it is still not good enough to be considered as INF.

        I am not sure about what did you type in your code to make LONG_MAX becomes negative, but chances are you are not doing something like (LONG_MAX + FOO) so it overflows and becomes negative.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem D, what if a road is part of two cycles. Say city 1->2->3->4->1 and 2->3->5->2 does the description above satisfy that case? Say, for 1,2,3,4 when we are considering the fact that 2->3 is flipped and becomes 3->2 we must not count the same case for the 2,3,5 component. Because they are the same. I am asking this question because there is no mention, that a road cannot be part of two different cycles.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    remind that in the input there is only ONE path from a city to another(or itself) city.

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hey,I was thinking the same thing you wrote above! Do we have to solve it assuming that a road cannot be part of two different cycles?

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem E : doubts regarding MOD: 1. in denom plz explaing the MOD-1 logic 2. in calculating numerator it should be multiplied by pow(2, -vtmp) right ?? But why it is multiplied by pow(pow(2, vtmp), MOD — 2 ) ?? 3. How does builtin_pop_count help here ?? vtmp stands for power of 2 in gcd right ?

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I assume that you know neither Fermat's Little Theorem nor Euler's extension on it. I strongly recommend you to Google it, it's is fundamental for handling modulus equations.

    1. why power to the MOD-1?

    This is because a^(p-1) = 1 mod p (where p is a prime), that means that we can reduce a^x to a^(x%(p-1)) since a^n has a cycle of p-1.

    1. Why pow(pow(2,vtmp), MOD-2)?

    This is the inverse function, this is a pretty common thing so I would leave it for Google to explain the meaning of a^-1 MOD b.

    1. I took a different way to calculate the bits so I can't answer it... :/
    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I got the theorem Just cant figure out the reduction done in the power :(

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        nvm Got it :)) Pen and paper work helped clear it out :))

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Hey friends!

I've recently learned Kosaraju's algorithm for finding SCC's(needed for problem D), i understood that algorithm reguires from us to make transpose graph and do second DFS on it using vertexes from a stack which we added during first DFS on regular graph.I was thinking about it alot trying to figure out why it works and i came up with an idea to reverse the Stack and just do traversal on regular (non traversed graph) and it AC'd.I am extremly confused now..Does Kosaraju's algorithm really needs an traverse of a graph as i really cant find any cases where my "twist" doesn't work...Can someone hand me an test case where it will fail or point me to right direction?

Cheers !

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem C , What do idx , m1 and m2 mean in the solution of O(n*k*m) Complexity ?

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

zscoder can you tell me my mistake in test case 7 on Div2 D http://codeforces.com/contest/711/submission/20560676

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Could anyone explain me the following:

ll cnt = n;
for(int i = 0; i < cycles.size(); i++)
{
    cnt -= cycles[i]; // Why?
    ans = (ans*(dp[cycles[i]]-2+MOD))%MOD; // Why adding MOD and then %MOD?
}
ans = (ans*dp[cnt])%MOD; // What is the goal?
  • »
    »
    3 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Adding MOD then %MOD is to make sure the answer is always positive.

    I'm doing what I explained in the editorial, for each cycle with length cycles[i] I multiply the answer by 2cycles[i] - 2 and for each remaining edge that aren't in any of the cycles I'm multiplying the answer by 2.

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      But adding MOD and then %MOD won't change the result.

      Let's define some variables:

      a := ans
      d := dp[]-2
      m := m
      
      [a * (d+m)] % m <=> (a*b)%m // b := d+m
      <=> [(a % m)*(b % m)] % m <=> [(a % m)*((d+m) % m] % m
      <=> [(a % m)*([(d % m)+ (m % m)] % m)] % m
      <=> [(a % m)*([(d % m)+ 0] % m)] % m
      <=> [(a % m)*((d % m)% m)] % m
      <=> [(a % m)*(d % m)] % m
      <=> (a*d) % m

      So, there is no need to add MOD, because it does not make any difference. I used some of these rules.

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      In which case could the answer be negative?

»
2 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Hello ! I am getting a runtime error on problem D(Case 7). I think the main culprit is the exponential part. Can someone please look into the code as according to me I am using modular exponentiation with memoisation .

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(i,n) for(ll i=0;i<n;i++)
#define leep(i,n) for(ll i=1;i<=n;i++)
#define mp make_pair
const ll INF = 1000000007;
const ll M=1e9+7;
vector<ll> adj[1112345];
pair<ll,ll>data;//c:p
ll vis[112345];
pair<ll,ll> comp[112345];
ll currNode;
ll path,cycle;
ll tcomp=0;
ll parr[2123450];

void dfs(ll node,ll dep,ll parent){
	vis[node]=dep;
	path++;
	loop(i,adj[node].size()){
		currNode=adj[node][i];
		if(currNode!=parent){
			if(vis[currNode]==0){
				dfs(currNode,dep+1,node);
			}else{
				cycle=abs(vis[node]-vis[currNode])+1;
			}
		}
	}
}

ll mex(ll a,ll n){
	if(parr[n]!=0){
		return parr[n];
	}
	else{
	if(n==0){
		parr[0]=1;
		return parr[n];
	}
	if(n==1){
		parr[1]=(a%M);
		return parr[n];
	}
		
	else{
		
		if(n%2==0){
			parr[n]=((mex(a,n/2)%M)*(mex(a,n/2)%M))%M;
			return parr[n];
		}
		else{
			parr[n]=((mex(a,n/2)%M)*(mex(a,n/2+1)%M))%M;
			return parr[n];
		}
		
	}
	}
}

int  main(){
	ll n,x;
	cin>>n;
	loop(i,n){
		cin>>x;
		adj[i+1].push_back(x);
		adj[x].push_back(i+1);
	}
		



	leep(i,n){
		if(vis[i]==0){

			dfs(i,1,-1);
			comp[tcomp]=mp(cycle,path-cycle);

			tcomp++;
			path=0;cycle=0;
		}
	}
	ll fin=1;
	loop(i,tcomp){
		
		
		cycle=comp[i].first;
		path=comp[i].second;
		ll cmul=mex(2,cycle);
		cmul-=2;
		
		ll emul=mex(2,path);
		fin=((fin%M)*(((cmul%M)*(emul%M))%M))%M;
		
	}
	cout<<fin<<endl;



return 0;


}
  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Got the problem , one array was out of bounds!

»
21 month(s) ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Hi everyone,

could anyone explain how the answer of this test of problem D would be as explained in the editorial

7

1 2

2 3

3 1

2 4

4 3

3 5

5 1