Yagnik_Dhameliya's blog

By Yagnik_Dhameliya, 2 years ago, In English

Epiphany 13.0

A: Gifts

jaineshmachhi

Tutorial
Code

B: Hulk Smash

jaineshmachhi

Tutorial
Code

C: Fight Between Friends

Yagnik_Dhameliya

Tutorial-1
Tutorial-2
Code-1
Code-2

D: SOS Signal

jaineshmachhi

Tutorial
Code

E1: New Year Games-1

rutvik_1234

Hint 1
Hint 2
Tutorial-1
Code-1

Yagnik_Dhameliya

Tutorial-2
Code-2

E2: New Year Games-2

rutvik_1234

Tutorial-1
Code-1

Yagnik_Dhameliya

Tutorial-2
Code-2

F: Money Distribution

Yagnik_Dhameliya

Hint-1
Hint-2
Hint-3
Hint-4
Tutorial
Code-1

Idea: shiven

Code-2
  • Vote: I like it
  • +17
  • Vote: I do not like it

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

Thanks for the fast editorial!! Nice Contest although wated 6 submissions on C because of not taking fast i/o.

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

If you are using HLD in problem F then it is possible to solve it in $$$O(q \log^2n)$$$ time. Construct HLD and segtree on top of tree. For each node, a range query can be done with range addition on a segtree(store $$$ax + b$$$ for each node and apply range addition appropriately). Path queries can be answered by walking on the segtree. Complexity for both is $$$O(log^2n)$$$.

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

    I was thinking the same but didn't get much clarity on whether this would work. If you have a solution then you can share that. It will be useful for others to understand the approach.

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

      Consider a segtree with following operations —

      1) Range add $$$(a,b)$$$
      2) Find range sum of $$$ax + b$$$

      Once you make this segtree just apply it on the tree. HLD will have additional $$$\log n$$$ factor making each operation $$$\log^2 n$$$. A nice implementation of HLD is here — https://mirror.codeforces.com/blog/entry/81317

      Alternatively you can flatten the tree and create fenwick trees of size $$$2*n$$$ to store sum of $$$a$$$ and $$$b$$$ from root to current node. Each query of type 1 can be broken into queries of $$$(a,b)$$$ from root to node and $$$(-a,-b)$$$ from root to LCA. For query of type 2, just iterate over all nodes from node to LCA. Value of current node is $$$sum_a(Enter[node],Exit[node]) \cdot x + sum_b(Enter[node],Exit[node])$$$. Unlike last method, this needs sum of path sums over all queries to be small.

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

        Nicely explained. But in this approach also the query of type 2 running for all the nodes from node to LCA so that K factor will be still present here.

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

          Yeah the alternative method will walk one by one. But if you build a HLD then you can break any path on the tree into $$$O(\log n)$$$ heavy chains and $$$O(\log n)$$$ light edges.

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

            Okay, so we will add (a, b) in segtree with seg[node].sum and seg[node].size and for each a and b it can be done in O(logn). Is this the way you are talking about? I'll try it.

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

i solved e1 and e2 using small to large merging But I will look for dsu also

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

in problem d, i find (x,y) in d but dont know how it works if just making x>=0 or y>=0 gives us optimal answer

Any proof Yagnik_Dhameliya shadow9236

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

    Tintin will keep sending signal on time = a+c,a+2c, a+3c, ... a+c*x and the ship will read for the position signal on time=b+d,b+2*d,b+3*d,... ,b+yd. So keeping the smallest positive x,y>=1 satisfying cx+a=dy+b will give us the smallest time.

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

      i was thinking like, is it always exist that at least one of the pair in which both are positive?

      i was thinking like one is incrementaed by a fac and other is decremented by a factor Is it possible to have some answer in other pairs also?

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

        Yes you are correct, for equation ax + by = c we have the general equation x=x∘ ± t⋅(n/gcd(m,n))and y=y∘ ∓ t⋅(m/gcd(m,n)),t∈Z where when one increases then the other decreases. But for problem D equation is cx — dy = k, in this case general solution is

        x=x∘ ± t⋅(n/gcd(m,n))and

        y=y∘ ± t⋅(m/gcd(m,n)),t∈Z

        so both x and y increases or both decreases, so we can guarantee there will be a pair with both of them positive.

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

Alternate solution for D,

WLOG, assume that $$$a+c \lt b+d$$$. First ship will send signals at every time t for which $$$t$$$ mod $$$c$$$ $$$\equiv$$$ $$$a$$$ mod $$$c$$$ and $$$t \geq a + c$$$. Therefore we need to find smallest $$$x \geq 0$$$ for which $$$(b + d + d \cdot x)$$$ mod $$$c \equiv a$$$ mod $$$c$$$. Let $$$y = gcd(c, d)$$$. $$$d \cdot x$$$ mod $$$c \equiv (a-b-d)$$$ mod $$$c$$$

Therefore, possible values for $$$x$$$ exist only if $$$y | (a-b-d)$$$ mod $$$c$$$, and can be found by,

$$$(d/y) \cdot x$$$ mod $$$(c / y) \equiv ((a-b-d) / y)$$$ mod $$$(c / y)$$$

Since, $$$(d/y)$$$ and $$$(c/y)$$$ are coprime, we can find $$$(d/y)^{-1} mod (c/y)$$$ using Euclidean theorem or Euler's theorem (will need to use Pollard Rho to use that). Then, their signals will occur simultaneously for all values of x for which $$$x$$$ mod $$$(c / y) \equiv ((a-b-d) / y) \cdot ((d/y)^{-1} mod (c/y)) $$$ mod $$$(c / y)$$$

so the answer would be $$$b + d + d*x$$$

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

    Great approach thanks for sharing 

    C++ Implementation