Codeforces celebrates 10 years! We are pleased to announce the crowdfunding-campaign. Congratulate us by the link https://codeforces.com/10years. ×

### Baba's blog

By Baba, history, 23 months ago, , I recently obtained an open cup ID and started practicing with the Grand Prix contest series. However, when I try to attempt the previous Grand Prix contests, I just get a message saying The virtual contest is in progress. You are not allowed to participate

I can view the problems of the last 2 GP but not of any before them. For eg, please see the screenshot attached.   By Baba, history, 3 years ago, , ## 757A — Gotta Catch Em' All!

Author: tanujkhattar
Testers: r.arora born2rule

Expected complexity: Main idea: Maintain counts of required characters.

Since we are allowed to permute the string in any order to find the maximum occurences of the string "Bulbasaur", we simply keep the count of the letters 'B', 'u', 'l', 'b', 'a', 's', 'r'. Now the string "Bulbasaur" contains 1 'B', 2'u', 1 'l', 2'a', 1 's', 1'r' and 1 'b', thus the answer to the problem is Min(count('B'), count('b'), count('s'), count('r'), count('l'), count('a')/2, count('u')/2). You can maintain the counts using an array.

Corner Cases:
1. Neglecting 'B' and while calculating the answer considering count('b')/2.
2. Considering a letter more than once ( 'a' and 'u' ).

Tester's code:

## 757B — Bash's Big Day

Author: architrai
Testers: mprocks, deepanshugarg

Expected complexity: Main idea: Square-root factorization and keeping count of prime factors.

The problem can be simplified to finding a group of Pokemons such that their strengths have a common factor other that 1. We can do this by marking just the prime factors, and the answer will be the maximum count of a prime factor occurring some number of times. The prime numbers of each number can be found out using pre-computed sieve or square-root factorization.

Corner Cases : Since a Pokemon cannot fight with itself (as mentioned in the note), the minimum answer is 1. Thus, even in cases where every subset of the input has gcd equal to 1, the answer will be 1.

Tester's code:

## 757C — Felicity is Coming!

Author: shivamkakkar
Testers: codelegend

Expected complexity: Main idea: Divide pokemon types into equivalence classes based on their counts in each list.

Consider a valid evolution plan f. Let c[p, g] be the number of times Pokemon p appears in gym g. If f(p) = q then .

Now consider a group of Pokemon P such that all of them occur equal number of times in each gym (i.e. for each ). Any permutation of this group would be a valid bijection.

Say we have groups s1, s2, s3, ..., then the answer would be |s1|! |s2|! |s3|! ...  mod 109 + 7.

For implementing groups, we can use vector < vector < int >  >  and for i-th pokemon, add the index of the gym to i-th vector.

Now we need to find which of these vectors are equal. If we have the sorted vector < vector < int >  > , we can find equal elements by iterating over it and comparing adjacent elements.

Consider the merge step of merge sort. For a comparison between 2 vectors v1 and v2, we cover at least min(v1.size(), v2.size()) elements. Hence work is done at each level. There are levels.

Bonus : Try proving the time complexity for quicksort as well.

Authors's code:

## 757D — Felicity's Big Secret Revealed

Author: saatwik27
Testers: imamit,abhinavaggarwal,Chipe1

Expected complexity: This problem can be solved using Dynamic Programming with bitmask.

The important thing to note here is that the set of distinct numbers formed will be a maximum of 20 numbers, i.e. from 1 to 20, else it won't fit 75 bits(1*(1 bits) + 2*(2 bits) + 4*(3 bits) + 8*(4 bits) + 5*(5 bits) = 74 bits). So, we can use a bitmask to denote a set of numbers that are included in a set of cuts.

Let's see a Top-Down approach to solve it :

Lets define the function f(i, mask) as : f(i, mask) denotes the number of sets of valid cuts that can be obtained from the state i, mask. The state formation is defined below.

Author's code:

## 757E — Bash Plays with Functions

Author: satyam.pandey
Testers: Superty,vmrajas

Expected complexity: Main idea: Multiplicative Functions.

We can easily see that f0 = 2(number of distinct prime factors of n). We can also see that it is a multiplicative function.

We can also simplify the definition of fr + 1 as: Since f0 is a multiplicative function, fr + 1 is also a multiplicative function. (by property of multiplicative functions)

For each query, factorize n.

Now, since fr is a multiplicative function, if n can be written as:

n = p1e1p2e2 ... pqeq

Then fr(n) can be computed as:

fr(n) = fr(p1e1) * fr(p2e2) * ... * fr(pqeq)

Now observe that the value of fr(pn) is independent of p, as f0(pn) = 2. It is dependent only on n. So we calculate fr(2x) for all r and x using a simple R * 20 DP as follows: And now we can quickly compute fr(n) for each query as:

fr(n) = dp[r][e1] * dp[r][e2] * ... * dp[r][eq]
Author's code:

## 757F — Team Rocket Rises Again

Author: tanujkhattar
Testers: shubhamvijay, tanmayc25, vmrajas

Expected complexity: Main idea: Building Dominator tree on shortest path DAG.

First of all, we run Dijkstra's shortest path algorithm from s as the source vertex and construct the shortest path DAG of the given graph. Note that in the shortest path DAG, the length of any path from s to any other node x is equal to the length of the shortest path from s to x in the given graph.

Now, let us analyze what the function f(s, x) means. It will be equal to the number of nodes u such that every path from s to u passes through node x in the shortest path DAG, such that on removing node x from the DAG, there will be no path from s to u.

In other words, we want to find the number of nodes u that are dominated by node x considering s as the sources vertex in the shortest path DAG. This can be calculated by building dominator tree of the shortest path DAG considering s as the source vertex.
A node u is said to dominate a node w wrt source vertex s if all the paths from s to w in the graph must pass through node u.

Once the dominator tree is formed, the answer for any node x is equal to the number of nodes in the subtree of x in the tree formed.

Author's code:

Another approach for forming dominator tree is by observing that the shortest path directed graph formed is a DAG i.e. acyclic. So suppose we process the nodes of the shortest path dag in topological order and have a dominator tree of all nodes from which we can reach x already formed. Now, for the node x, we look at all the parents p of x in the DAG, and compute their LCA in the dominator tree built till now. We can now attach the node x as a child of the LCA in the tree.
For more details on this approach you can look at moejy0viiiiiv's solution here.

## 757G — Can Bash Save the Day?

Author: tanujkhattar

Expected complexity: Main idea: Making the Centroid Tree Persistent.

### Simpler Problem

First let's try to solve a much simpler problem given as follows.

Question: Given a weighted tree, initially all the nodes of the given tree are inactive. We need to support the following operations fast :
Query v : Report the sum of distances of all active nodes from node v in the given tree.
Activate v : Mark node v to be an active node.

Solution: The above problem can be easily solved by a fairly standard technique called Centroid Decomposition. You can read more about here

### Solution Idea

• Each query of the form (L R v) can be divided into two queries of form (1 R v)  -  (1 L - 1 v). Hence it is sufficient if we can support the following query: (i v) : Report the answer to query (1 i v)
• To answer a single query of the form (i v) we can think of it as what is the sum of distance of all active nodes from node v, if we consider the first i nodes to be active.
• Hence initially if we can preprocess the tree such that we activate nodes from 1 to n and after each update, store a copy of the centroid tree, then for each query (i v) we can lookup the centroid tree corresponding to i, which would have the first i nodes activated, and query for node v in time by looking at it’s ancestors.
• To store a copy of the centroid tree for each i, we need to make it persistent.

### Persistent Centroid Tree : Key Ideas

• Important thing to note is that single update in the centroid tree affects only the ancestors of the node in the tree.
• Since height of the centroid tree is , each update affects only other nodes in the centroid tree.
• The idea is very similar to that of a persistent segment tree BUT unlike segtree, here each node of the centroid tree can have arbitrarily many children and hence simply creating a new copy of the affected nodes would not work because linking them to the children of old copy would take for each affected node and this number could be as large as N, hence it could take time in total !

### Binarizing the Input Tree

• To overcome the issue, we convert the given tree T into an equivalent binary tree T' by adding extra dummy nodes such that degree of each node in the transformed tree T' is  <  = 3, and the number of dummy nodes added is bounded by .
• The dummy nodes are added such that the structure of the tree is preserved and weights of the edges added are set to 0.
• To do this, consider a node x with degree d > 3 and let c1, c2...cd be it's adjacent nodes. Add a new node y and change the edges as follows :
• Delete the edges (x - c3), (x - c4) ... (x - cd) and add the edge (x - y) such that degree of node x reduces to 3 from d.
• Add edges (y - c3), (y - c4) ... (y - cd) such that degree of node y is d - 1. Recursively call the procedure on node y.
• Since degree of node y is d - 1 instead of original degree d of node x, it can be proved that we need to add at most new nodes before degree of each node in the tree is  <  = 3.

### Conclusion

Hence we perform centroid decomposition of this transformed tree T'. The centroid tree formed would have the following properties.

• The height of the centroid tree is • Each node in the centroid tree has  ≤ 3 children.

Now we can easily make this tree persistent by path-copying approach.

• Way-1 : Observe that swapping A[i] and A[i + 1] would affect only the i'th persistent centroid tree, which can be rebuilt from the tree of i - 1 by a single update query. In this approach, for each update we add new nodes. See author's code below for more details.
• Way-2 : First we go down to the lca of A[x] and A[x + 1] in the x'th persistent tree, updating the values as we go. Now, let cl be the child of lca which is an ancestor of A[x], and let cr be the child which is an ancestor of A[x + 1]. Now, we replace cr of x'th persistent tree with cr of (x + 1)'th persistent tree. Similarly, we replace cl of x + 1'th persistent tree with cl of x'th persistent tree. So now A[x + 1] is active in x'th persistent tree and both A[x] and A[x + 1] are active in (x + 1)'th persistent tree.To deactivate A[x] in x'th persistent tree we replace cl of x'th persistent tree with cl of (x - 1)'th persistent tree. Hence in this approach we do not need to create new nodes for each update. See testers's code below for more details.
Author's code:
Testers's code:

Hope you enjoyed the problemset! Any feedback is appreciated! :)  391,
By Baba, history, 3 years ago, , Hello Everyone!

I would like to invite you to CodeCraft-17, which will take place on Thursday 12th January 2017, 9:05PM IST. The round will be a Div1 + Div2 combined round and is rated.

Felicity, IIIT Hyderabad presents Threads ‘17, the magnificent annual technical fest, is finally here in its 13th edition! With a plethora of intellectually engaging online contests in various fields of programming, mathematics and general knowledge, Threads is a celebration of the spirit of computing and engineering. The grand algorithmic sprint called CodeCraft, parallel programming in Kernel Cruise, mathematical enigmas in Gordian Knot, jeopardy style CTF event called Break In, overall Threads '17 has something to offer for every kind of computing enthusiast! I would like to thank Nikolay Kalinin (KAN) for helping us in preparing the contest,to Mike Mirzayanov (MikeMirzayanov) for the great Codeforces and Polygon platform and the Threads Team for helping in preparation of the problemset.

## Prizes

• Top 25 participants win Codeforces T-shirts.
• Additionally, 25 randomly chosen participants from top-500 will also win a Codeforces T-shirt.

You will be given seven problems and three hours to solve them. The scoring distribution will be announced later. Good luck everyone!

UPD: The constraint of registration for eligibility to prizes has been removed. Everyone who participates in the round is eligible for prizes. Sorry for the inconvenience caused!

UPD2: Scoring is 500 — 1000 — 1500 — 2000 — 2500 — 2750 — 3500

UPD3:
The contest has ended. Thank you all for participating. Hope you liked the problems.
It was our first codeforces round and it was really exciting for us to see how the contest progressed!
Editorials and list of T-shirt winners will be published soon.

Congratulations to the winners!

UPD4: Editorial has been posted.

UPD5: The random t-shirts winners will be announced tomorrow according to the algorithm described in this comment.  391,
By Baba, history, 4 years ago, , Hi!
I have tried to explain the concept and algorithm for building the Dominator Tree of a directed graph with respect to a source vertex in this article :

The algorithm was published by Robert Tarjan in his original research paper here. There are not much resources on the topic, except the original research paper , on the internet. The topic is fairly advanced and involves a lot of theory.

Happy Coding :) By Baba, 5 years ago, , Any feedback/suggestions are most welcome. :)

By Baba, 5 years ago, ,  