### PikMike's blog

By PikMike, history, 5 days ago, translation, ,

1238A - Prime Subtraction

Idea: BledDest

Tutorial
Solution (BledDest)

1238B - Kill Em All

Idea: Ne0n25

Tutorial
Solution (Ne0n25)

1238C - Standard Free2play

Tutorial

1238D - AB-string

Idea: Roms

Tutorial
Solution (Roms)

1238E - Keyboard Purchase

Idea: Roms

Tutorial
Solution (Roms)

1238F - The Maximum Subtree

Idea: Roms

Tutorial
Solution (Roms)

1238G - Adilbek and the Watering System

Idea: Ne0n25

Tutorial
Solution (Ne0n25)

• +40

 » 5 days ago, # |   0 typo in D. (n-1)*n/2
•  » » 5 days ago, # ^ | ← Rev. 2 →   +9 No that's not typo .Total count of possible substrings is n*(n+1)/2 (including substring of length one)
•  » » » 5 days ago, # ^ | ← Rev. 3 →   -33 No, god. Please, no. No!!!
•  » » » » 5 days ago, # ^ |   0 See , substrings of length 1 are bad , so they will be automatically counted in cntBad .So n(n+1)/2 is count of all substrings and cntBad = number of bad substrings .Hence n(n+1)/2 — cntBad is number of good substrings .For example consider "AAA" , here n(n+1)/2 = 6 and number of bad substrings i.e cntBad = 3 (Three substrings "A" of length 1 ) , thus total number of good substrings is 6 — 3 = 3 i.e the number we get after removing bad substrings from all possible strings .
•  » » » » » 5 days ago, # ^ |   -9 We ignore them in the beginning, check the author’s solution.
•  » » » » » » 5 days ago, # ^ |   0 If we take total substring as n*(n+1)/2 then we have to initialize cntBad = n at first.And if we take total substring as n*(n-1)/2 then we have to initialize cntBad = 0. Authors solution took cntBad = 0.So both are right.
 » 5 days ago, # | ← Rev. 3 →   -25 Problem A Challenge: Assume, that you can substract a prime from x at most 3 times. SolutionThe solution is the same as to the original problem, because of Goldbach's conjecture (https://en.wikipedia.org/wiki/Goldbach%27s_conjecture).
 » 5 days ago, # |   0 I couldn't understand the explanation of problem C. Can someone describe it?
•  » » 5 days ago, # ^ |   +13 For explanation I'm using a test case: 9 8 5 4 3 1Do you know what does this mean? Let me explain. It means that the height of the cliff is 9. So initially at 9, there is a platform where the player is standing. At height 8 there is a platform too. But at height 7 and 6 there is no platform by now. And then at height 5 and 4 and 3 there we have platforms at a stretch. Height 2 have no platform. Height 1 has platform.At first you are at the highest point means at 9. From here we'll will see three kinds of moves. Let's see those:Case 1: Do both of the next two height have platforms? If yes, then we can and should move to the second using 0 crystal.Case 2: Do the next height have a platform? if yes, then it is optimal to move to that next height using 1 crystal.Case 3: Now it means that there is at least one gap between now and the next platform. It doesn't matter if there is one or multiple gap. You can move to the very last gap using no crystal that means 0 crystal.Now solve our test case: 9 8 5 4 3 1.We are now at 9. Here we see case 2 because only the next height 8 has a platform. So we move to 8 using 1 crystal. So now we are at 8. Here we see case 3 because we have gap at heights 7 and 6. So we move to 6 using 0 crystal. So now we are at 6. Here we see case 1 because both of the next two heights 5 and 4 contains platform. so we move to height 4 using 0 crystal.So now we are at 4. Here we see case 2. So we move to height 3 using 1 crystal.So now we are at 3. Here we see case 3. So we move to height 2 using 0 crystal.Now notice one thing. At Ground that means at height 0 we can assume that there is always a platform but it doesn't move at all. Because it's ground :DSo now we are at 2. Here we see case 1. So we move to ground using 0 crystal. So in total 2 crystal is used.We need a crystal only when we meet case 2.I hope you understand.
•  » » » 5 days ago, # ^ | ← Rev. 2 →   0 Thank you very much. I understanded it now.
•  » » 4 days ago, # ^ |   0 Question is just observations:Observation 1 -> if u have 0 below u, you are good Observation 2 -> if u have 1 below u and 0 below that 1 u are goodObservation 3 -> if u are on starting block then length of contigous 1s should be odd. eg 1 1 1 1 1u will land on last 1, if it was 1 1 1 1 0 1 then u will fall.Observation 4 -> if u are not on starting block u want even length contigous 1s. Why ? coz uwill land on the 0 just above u. for eg. 1 1 1 0 0 0 0 1 1 1 1 0 0 here u will land on 7th 0 and from there on u will land on last 1. 1 1 1 0 0 0 0 1 1 1 0 0 0 this is invalid bocz here uwill fall after getting on 2nd last last 1. Observation 5 -> if the last stone is on 1st position then u don't need to worry about anythingcoz last cell is ground anyway so 1 1 1 0 1 1 1 1 0 and 1 0 0 1 1 1 0 both are right in 1st caseu will land on last 1 and go to ground and in 2nd case u will land on 2nd last 1 but u won'tfall coz last place is ground anyway.This problem should have contrsuctive tag also :thinking:
•  » » » 4 days ago, # ^ |   0 Thank you very much
 » 5 days ago, # |   -6 In problem D is tagged as DP .Can some one tell the method to solve it using DP ? We can calculate bad strings using 4 different for loops (corresponding to type of bad string) , any other method to do that with less typing ?
 » 5 days ago, # | ← Rev. 3 →   0 For problem D, how can there be only 4 patterns? For example the string $ABBAB$ is also a bad string but its pattern is not the same as the 4 given patterns.
•  » » 5 days ago, # ^ | ← Rev. 2 →   +5 ABBA...BAB`=> string is good
•  » » » 5 days ago, # ^ | ← Rev. 2 →   0 Oh thanks. I didn't read the question carefully, my bad.
 » 5 days ago, # |   0 Constraints are really deceiving in E. Could somebody explain an O($a*2^a$) solution?
•  » » 5 days ago, # ^ |   0 The $a^2$ factor inside the dp is for computing the sums of $cnt_{x,y}$ for $x \in mask$ and $y \not\in mask$. You can improve this with sos dp, for each mask you compute how much should be added to.
•  » » » 5 days ago, # ^ |   0 The standard dp sos i'm thinking about is $a2^a$ for each character, which makes it $a^{2} 2^a$ in total. Could you explain further the recurrence of your dp?
•  » » » » 5 days ago, # ^ |   0 I solved in $O(2^a * a^2)$ in the contest. I just coded the same ideia in $O(2^a * a)$ with sos dp.
•  » » 5 days ago, # ^ |   +10 If you calculate the cost in $DP$ by using the newly added letter's position:Let $co[i][j]$ be the number of times letters $i$ and $j$ appeared next to each other. You can build an array $arr$ where $arr[i][mask]=\sum_{k\in mask} co[i][k]$ in $O(m*2^m)$, by using this trick: $arr[i][mask]=arr[i][mask\oplus 2^j] + co[i][j]$ for $mask>0$, where $j$ is any set bit's position in $mask$ (can be the lowest significant bit for example).
•  » » » 4 days ago, # ^ |   +5 Got it. Thanks!
•  » » » 4 days ago, # ^ |   0 Thank you.
•  » » » » 4 days ago, # ^ |   0 You're welcome! :)But in my code I just used another for in calculating my dp every time I fix the c, so my code order is O(m^2.2^m), too! but you can easily change it.
 » 5 days ago, # |   0 In Problem D, I'm having trouble counting the number of such substrings. I read a lot of different codes but I don't seem to get it. Can somebody please explain their own method to me? Thanks a lot in advance.
•  » » 5 days ago, # ^ | ← Rev. 2 →   0 let our string is AABBAAABAA here we have to find answer for AABB and BBAAA and AAAB and BAA. total of there will be answer for our string . And answer for string AAABB will be 3+2-1. We are subtracting 1 because we are adding the conjution (AB) twice . so answer for string AABBAAABAA will be (2+2-1)+(2+3-1)+(3+1-1)+(1+2-1)=12.
•  » » » 2 days ago, # ^ |   0 Helped a lot, thanks!
 » 5 days ago, # |   +11 A proof for why the greedy algorithm works in $C$:First note that for any $v>1$, you have to stand on at least $v$ or $v-1$, because if you didn't stand on both this means that you descended from $a>v$ to $b2$ where $x-1$ is moved out and $x-2$ is hidden. Now you must use at least $1$ crystal to continue. You can use it either to: move out the platform at $x-2$, then descend to it directly. hide the one at $x-1$ and move to it, where you will still have the opportunity to continue to $x-2$ without extra crystals, so this option is better. The other scenario you can follow starting from $h$, is that which would end on $x-1$ (without going to $x$), and this scenario will cost at least $1$ crystal. So we deduce that the best option is option $2$ in the previous scenario. Starting from $x-1$, you can continue to use the same followed logic starting from $h$.
 » 5 days ago, # |   0 in problem E, when iterating to new character x then how we calculate dp[mask|(1<
 » 5 days ago, # |   0 I have tried an O(m^2*2^m) solution for problem E but it is giving TLE.
 » 5 days ago, # |   0 I cannot understand the solution of problem E. Specially, "If letter y is already on current keyboard, then we should add to answer cntx,y(posx−posy), and cntx,y(posy−posx) otherwise" sentence. Why should we consider the letter y which is not included in the keyboard? And on last formula,∑y∈msk(cntx,yposx)−∑y∉msk(cntx,yposx), why there is no posy? Where do they go?
•  » » 4 days ago, # ^ |   0 Ah Sorry, I understand.
•  » » » 4 days ago, # ^ |   0 How do you erase cntx,y*posy? Can you explain? I think it is only possible when the two summations have the same range.
•  » » » 4 days ago, # ^ |   0 care to explain please
 » 4 days ago, # |   +24 Can any one explain how to solve D if there are 26 character?
 » 4 days ago, # |   +1 Can someone explain how to solve D with 'binary search'?
 » 4 days ago, # |   +5 How to solve D using DP?
 » 4 days ago, # |   +1 Just a side fact: The resultant tree that gets formed in the problem F is also called Caterpillar Tree.
 » 4 days ago, # | ← Rev. 2 →   0 can someone please explain the structure of tree formed in problem F? I didn't get it from editorial completely.
•  » » 4 days ago, # ^ |   0 It's a caterpillar: https://en.wikipedia.org/wiki/Caterpillar_tree
•  » » » 3 days ago, # ^ |   0 Thank you for your reply! But I am asking can you please explain how we land on this structure?
•  » » » » 3 days ago, # ^ | ← Rev. 2 →   +5 One major point here is that an integer can't be contained in three or more segments, since that would correspond to a cycle in the described graph, but we're given a tree, so that's ruled out. With this knowledge, let's construct some intervals so that we can maximise the number of intervals. We'll try to have two segments containing an integer as long as it is possible, since we're trying to maximise the size of the required subgraph. So we have a continuous sequence of intersecting intervals, and then for points that are not yet covered by two intervals, we're trying to find as many non-intersecting intervals as possible to cover those points (you may even think of them as degenerate intervals, where l = r, if you want, since we're free to size our intervals as we please). The only thing that limits us is the given tree, where the smaller intervals can only be as many as the count of neighbours except the next interval in the path. I hope you see why this is optimal, and why this would correspond to the caterpillar graph.
•  » » » » » 45 hours ago, # ^ |   0 Can You help mo to solve it. I can visualize the theoretical part but I am unable to write code or create proper logic.I tried to figure out Roms code given in editorial but i am not understanding his dfs clearly? And one more question.Why should we choose a non leaf vertex as root??Thanks In Advance
 » 4 days ago, # |   0 Why is there a "Binary Search" Tag on Problem D? How to solve it using Binary Search?
 » 4 days ago, # |   +3 How to understand the meaning of the dp array in E problem?
 » 4 days ago, # | ← Rev. 2 →   0 Problem fixed.Sorry for inconvenience.
•  » » 4 days ago, # ^ |   +10 Read the statement carefully.When you will explosion on x=3 you will get 6 7 7 7Then every monster is either killed by explosion or pushed away.
•  » » 4 days ago, # ^ |   +10 if you'll fire at c=3 then positions will become 6,7,7,7
 » 3 days ago, # | ← Rev. 6 →   0 PikMike, in problem E, could you explain how you arrived at the final formula
•  » » 3 days ago, # ^ |   0 got it now..
•  » » » 47 hours ago, # ^ |   0 can u explain it here plz
•  » » » » 46 hours ago, # ^ |   0 thnx . got it now
 » 2 days ago, # |   0 Can Someone Explain What d[M][N] represents and how we used it to calculate dp[M] in the editorial for problem E?