CodeCraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) Editorial

Правка en1, от Baba, 2017-01-13 13:37:45

757A — Gotta Catch Em' All!

Author: Baba
Testers: virus_1010 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

Complexity : using square-root factorization and using a pre-computed sieve.

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 equivalent 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

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.

Let M be the maximum number among the numbers in mask. mask denotes a set of numbers that have been generated using some number of cuts, all of them before bi. Out of these cuts, the last cut has been placed just before bi. Now, first we check if the set of cuts obtained from mask is valid or not(in order for a mask to be valid, mask == 2X - 1 where X denotes number of set bits in the mask) and increment the answer accordingly if the mask is valid. And then we also have the option of adding another cut. We can add the next cut just before bx provided the number formed by "bi bi + 1...bx - 1" <= 20. Set the corresponding bit for this number formed to 1 in the mask to obtain newMask and recursively find f(x, newMask).

Author's code:

757E — Bash Plays with Functions

Author: satyam.pandey
Testers: Superty,vmrajas

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: Baba
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.
You can read more about dominator tree here.

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 jqdai0815's solution here.

Теги codecraft, round, 391, editorial, div1 + div2, felicity

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en9 Английский Baba 2017-01-13 17:02:06 7 Tiny change: 'nctions ](codeforces' -> 'nctions ](http://codeforces'
en8 Английский Baba 2017-01-13 16:11:52 1 Tiny change: ' appreciatd! :) \n' -> ' appreciated! :) \n'
en7 Английский Baba 2017-01-13 16:01:31 2 Tiny change: ' \nThis pro' -> ' \n\nThis pro'
en6 Английский Baba 2017-01-13 16:00:41 8 Tiny change: ' Bitmask. \nThis pr' -> ' Bitmask. \nThis pr'
en5 Английский Baba 2017-01-13 15:56:42 202
en4 Английский Baba 2017-01-13 15:49:10 66 Tiny change: 'Em' All!](codeforces' -> 'Em' All!](http://www.codeforces'
en3 Английский Baba 2017-01-13 15:46:59 51 (published)
en2 Английский Baba 2017-01-13 15:44:43 20649 Tiny change: ':">\n~~~~~ \n//Tanuj ' -> ':">\n~~~~~\n//Tanuj '
en1 Английский Baba 2017-01-13 13:37:45 19924 Initial revision (saved to drafts)