Intellegent's blog

By Intellegent, 6 months ago, In English

Thank you for participating!

Special thanks to reirugan for writing the analysis for B, D, E and to Proof_by_QED and Friedrich for helping to optimise F1, leading to the creation of F2.

Rating Predictions

2154A - Notelock:

Hint 1
Solution
Implementation(C++)
Implementation(Minecraft)

2154B - Make it Zigzag:

Hint 1
Hint 2
Solution
Implementation(C++)

2154C1 - No Cost Too Great (Easy Version):

Hint 1
Hint 2
Solution
Implementation(C++)

2154C2 - No Cost Too Great (Hard Version):

Hint 1
Hint 2
Hint 3
Solution
Implementation(C++)

2154D - Catshock:

Hint 1
Hint 2
Solution
Implementation(C++)
Bonus

2154E - No Mind To Think:

Hint 1
Hint 2
Solution
Implementation(C++)

2154F1 - Bombing (Easy Version):

Hint 1
Hint 2
Solution
Implementation(C++)

2154F2 - Bombing (Hard Version):

Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8
Implementation(C++)

Bonus editorial by Friedrich:

Solution 2
  • Vote: I like it
  • +178
  • Vote: I do not like it

»
6 months ago, hide # |
 
Vote: I like it -61 Vote: I do not like it

first.

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Wowee C !!!

»
6 months ago, hide # |
Rev. 3  
Vote: I like it +4 Vote: I do not like it

FAST EDITORIAL! C + TLE :)

»
6 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

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

»
6 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

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

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

fast editorial!

»
6 months ago, hide # |
Rev. 2  
Vote: I like it +1 Vote: I do not like it

Let's say, we sort the array by increasing value of 'b' array.

then...

The second case is the more tricky case, we need to check the optimal way to make more than one operations for each element, and it is probably the crux of the problem

Above statement can be more optimised saying that we should never perform more than 1 operation on those elements, where b[i] != b[1](1 indexing for array rep. ) ( because, it is never optimal to perform two operation of some b[i], we can always perform one b[1] and one b[i] , which is bound to be less than 2 * b[i] ( because the array is already sorted by 'b' values ).

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it +4 Vote: I do not like it

    I figured out that after sorting b[0]+b[1] will be the answer if we can't do better with b0 alone... but I was confused what if there are multiple elements with same cost to update... I thought If I check all them then it will give TLE.. because I was using a set to maintain all the prime divisors and checking the closest prime multiple which I can get for corresponding a[I] of minimum b.. but I can't do that for all n numbers right? if all costs are same... can you please help me...

    • »
      »
      »
      6 months ago, hide # ^ |
      Rev. 3  
      Vote: I like it +6 Vote: I do not like it

      actually, if there are multiple elements of all same costs in the beginning... then you will need no more than 2 operations of the b[1] ... think about it... 2 operations are always sufficient...

      So, now what remains is ( with '2' operation, we are certain we will get it , as established bove ), we need to find if we can do with only one operation in only first part... that is simple, you can do it easily by maintaining prime factors of each numbers and check through map, whether corresponding entry exists or not.

»
6 months ago, hide # |
 
Vote: I like it +18 Vote: I do not like it

I was cooked by C2

»
6 months ago, hide # |
 
Vote: I like it +29 Vote: I do not like it

Amazing contest I think AI can't solve any question after C1 thats why this contest feels fair.

»
6 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

C2 is so good!

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

C1 was a nice problem. Unfortunately I got a tle on my submission I used the same approach as the editorial but didnt precompute the primes. Isn't N * sqrt(N) good enough?

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it +5 Vote: I do not like it

    You're making a new array of 2e5 size in each testcase.

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

      Thanks for pointing that out. What is a good enough upper bound for this? Is it maxPrime we encounter while factorising?

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

        I would make sure that the array is global, and that after you're done with it, you make sure that all the values that were set, are set back to 0.

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

          Thank you! I declared the array globally and used fill(all(),0) and it was accepted.

          correct submission

          • »
            »
            »
            »
            »
            »
            6 months ago, hide # ^ |
             
            Vote: I like it +9 Vote: I do not like it

            You're kind of lucky that it worked, because the fill is so fast. If there would be even more tests per file, it would still be too slow. A better way is to do the loop over prime factors twice, once to set, and once to put everything back to 0 again. Something like I did in my code with the cnt array: 344668807

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

      filling the array with 0 takes O(N) in every test case . so shouldn't it give TLE . because in every test case we go from 0 to 2e5 and there are 1e4 test cases .

»
6 months ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

C2 was such a nice problem !!!

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Loved implementation of A on Minecraft!!

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

My C1 FSTd on TC 21 (Runtime Error). Does anybody know what the error might have been. I must be overlooking something fairly obvious.

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

    Same bruh but what could be the reason

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

    you have initialized your smallest_pf_fact till 2e5 only, but what if arr[i] = 2e5 then, as per your code val2 = arr[i] + 1, i.e. now you will access smallest_pf_fact[2e5+1] which will return 0, as, it's uninitialized, then you perform division by val2 (i.e. 0), which will obviously give a RTE!

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it +22 Vote: I do not like it

    Its most likely an out of bounds error from where $$$a_i = 2 \cdot 10^5$$$, some participants were accessing the element at index $$$2 \cdot 10^5 + 1$$$ in an array of size $$$2 \cdot 10^5$$$.

    There are tests with $$$a_i = 2 \cdot 10^5$$$ but unfortunately I forgot to consider that participants might break early from finding an optimal solution and don't get Runtime Error for these cases.

    I apologise for anyone who was affected by this.

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

      I would have got my rank upgraded but anyways contest was actually goated. And I could have avoided checking evens as they can get paired up with anyone in 1 in that case too this can pass

    • »
      »
      »
      6 months ago, hide # ^ |
       
      Vote: I like it +1 Vote: I do not like it

      First, thank you for such great contest. Truly enjoyed the questions! Did you mean that the test case 21 was added after contest or during contest?

      • »
        »
        »
        »
        6 months ago, hide # ^ |
         
        Vote: I like it +9 Vote: I do not like it

        Hacks made by participants during the round are added to system tests, but not the pretests. Test case 21 is a result of this, so it was added after the contest.

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

      No worries. It was a great contest anyways!

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

nah that minecraft implementation is funny and crazy

»
6 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Last week in LeetCode biweekly in D que, got to know about the concept of SPF and today I used it in C1, hope +ve delta.

»
6 months ago, hide # |
Rev. 5  
Vote: I like it 0 Vote: I do not like it

Man! Sometimes just 1 number can mess up your contest.

Contest Submission:344688102

Normal Submission:344750449

Btw these are my friend's submissions messed his contest rank.

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

hey in the editorial for C1...

this part     for (int i = 0; i < n; i++){
        for (int x : pfac[a[i]])
            cnt[x]--;

        for (int x : pfac[a[i] + 1]){
            if (cnt[x] > 0)
                ans= min(ans, 1);
        }

        for (int x : pfac[a[i]])
            cnt[x]++;
    }

why do we need to decrease the cnt of prime factors I mean two consecutives numbers are always co-prime so there is no need to do that right?

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone help with the error? 344753515 I couldn't find the output bug, how the cat is not reaching to n? (Test Case 1)

»
6 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Intellegent the test cases on C2 seem weak, the submission passes C2 but throws tle on C1, can you try rejudging C2 with C1 test case 17.

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

С1 is a nice problem. Is there a way to solve it in Python? I got TLE on the 7th test.

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone check if my solution to C2 is hackable or not? I have suspicion that my solution will probably TLE: 344744359

»
6 months ago, hide # |
Rev. 3  
Vote: I like it +9 Vote: I do not like it

Bonus on D is funny because the moment I read the problem I asked myself how would someone implement a checker for such problem! I was 90% sure that instead of directly solving the problem, it's better to implement the checker and it'll naturally lead to the solution.

I came up with something like (spoiler: didn't work)

I really would like to know how is the checker implemented for this problem.

»
6 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Real fast editorial!

I really loved the problems, but I did leave a little heartbroken.

I made a mistake in C1 of having MAX = 2e5 + 1 instead of MAX = 2e5 + 2(at least), and that passed the pretests but failed later on. I'm a little heartbroken because I had a good run, especially because I solved C1 rather fast. A lot faster than some people, and friends. In the end I went from +50 to -45 :(

I'm never making that mistake again, but I am still happy as I got the idea and I really enjoyed the problems

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

My idea of D was, find the leaf nodes and the path from 1 to n, start from a leaf node and do operation 2 then 1 then 2 on it, this ensures the leaf node is removed safely, keep doing same on its adjacent node till you encounter a node which is either in the path from 1 to n or has a degree greater than 2, if it the latter case decrease degree of node by 1 and remove the edge connecting this node to previous node. repeat similar for all leaf node, this leaves us with only the path from 1 to n, now do as we did for leaf node starting from node 1 all the way to node n. so its kind of deleting the branches to main path safely then shortening the main path, takes exact 3n operation

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

So, i need ur help for C2. I understand that we need to check only the lowest bi, but what if all bi are equal. for example: a: 3 7 11 b: 1 1 1 we can see, that to solve this case we need to make a == 3 7 12 and for this we need to check all bi. It is O(n * (amount of common divisors)) where the second one is rather big. So i suppose this solution mustn't work

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    If the cheapest element is not unique, then you don't need to consider incrementing any element more than once, since incrementing the cheapest element by 2 already costs at least the same as making the two cheapest elements both even.

    More formally, assume the elements are sorted by $$$b_i$$$ ascending, then we can always get a solution at cost $$$b_1 + b_2$$$ by making the first two elements even (less if one of them is already even), so incrementing the first element more than once only makes sense when $$$b_1×2 \lt b_1 + b_2$$$, and when $$$b_1 = b_2$$$ (i.e., the cheapest element is not unique) then $$$b_1×2 = b_1 + b_2$$$.

    You can generalize this to: it only makes sense to increment $$$k$$$ times when $$$b_1×k \lt b_1 + b_2$$$, which implies $$$k \lt (b_1 + b_2)/b_1$$$ but this doesn't improve the worst case complexity so it's not really useful for this problem.

»
6 months ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

W contest. Learned a lot from problems and solutions

»
6 months ago, hide # |
 
Vote: I like it +45 Vote: I do not like it

Hey Intellegent, Please make more rounds!

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Upsolved D to find the exact same solution as the editorial. Very cool contest.

Can anyone help me with the time complexity analysis of my solution for C2? I know I missed the observation but I cannot figure out why mine TLEs. https://mirror.codeforces.com/contest/2154/submission/344750294

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Whats the intuition behind using bipartite nature of trees in D?

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

    Let $$$dist[u]$$$ = distance of vertex u from 1. The cat could be at any vertex $$$u$$$ after $$$m$$$ type 1 operations iff $$$dist[u] \le m$$$ and $$$dist[u]$$$ has the same parity as $$$m$$$. You can reason about why the parity should be the same. Now, considering the loose constraint of $$$3n$$$ operations, we don't need to care about the $$$dist[u] \le m$$$ condition. Any leaf vertex can be destroyed in at most 3 operations. So, we will choose to only destroy leaves.

    Suppose the previous operation was a type 2. Now, the next operation must be a type 1. After this, if the vertex we are interested in destroying has the same parity as the no. of type 1 operations so far then we can do another type 1 operation and the parity becomes different. Then a type 2 operation can be made to destroy that vertex.

    Edit: The reason why $$$dist[u]$$$ should have the same parity as $$$m$$$ is because if we take a simple path from 1 to $$$u$$$, then the path length will be $$$dist[u]$$$. Now, if the cat wants to reach $$$u$$$ in more than $$$dist[u]$$$ moves then it must wander off from this simple path and then come back to continue the path. Coming back retraces the moves made to wander off. So, this will increase the no. of moves taken to reach $$$u$$$ by some even number. This means the cat can reach from 1 to $$$u$$$ in $$$dist[u] + 2k$$$ type 1 moves, for $$$k \ge 0$$$. This is the same as the condition $$$dist[u] \le m$$$ and $$$dist[u]$$$ has the same parity as $$$m$$$.

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

      will this method work? find the leaf nodes and the path from 1 to n, start from a leaf node and do operation 2 then 1 then 2 on it, this ensures the leaf node is removed safely, keep doing same on its adjacent node till you encounter a node which is either in the path from 1 to n or has a degree greater than 2, if it the latter case decrease degree of node by 1 and remove the edge connecting this node to previous node. repeat similar for all leaf node, this leaves us with only the path from 1 to n, now do as we did for leaf node starting from node 1 all the way to node n. so its kind of deleting the branches to main path safely then shortening the main path, takes exact 3n operation

      • »
        »
        »
        »
        6 months ago, hide # ^ |
        Rev. 2  
        Vote: I like it 0 Vote: I do not like it
        1. Are you sure you want to do operations 2, 1, 2 on the leaf node?
        2. You cannot ignore the parity/color of the node that is getting destroyed. For example, if the tree has edges (1, 2), (1, 3), (1, 4), (4, 5), then the path from 1 to n will be 1 -> 4 -> 5, and leaves will be 2 and 3. You can remove either of the leaves at first, but then you cannot remove the other leaf immediately after. Because the cat might have jumped to that leaf node.
        3. You don't have to treat the path from 1 to n specially. If you never process the node n as a leaf, then in that case the node 1 will be the only remaining leaf. You can process it and the new leaf will be the next node on path from 1 to n. It helps to think of the path from 1 to n as a special case, but it is really not.

        You are on the right track.

        Edit: 2 1 2 is not feasible as for the next leaf node you cannot start it with a type 2 operation. |2 1 2|2 1 2| breaks the constraint that there cannot be two consecutive type 2 operations.

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

    The subset of nodes that the cat can be in at any even turn (0,2,4,...) are always the same, the same for the odd turns (1,3,5,...), so in each turn you can distroy any leaf that are in the opposite subset because the cat can't be in it!
    That is simply!
    A small corner case is when for example cat is in an even turn, and the are no leaves in the odd subset!
    you can simply make the cat move to the odd turn, and delete a leaf from the even turn.
    Re-root the tree and make n is the root, and run bfs till you distroy all nodes expect n.

    BFS code
    • »
      »
      »
      6 months ago, hide # ^ |
       
      Vote: I like it +3 Vote: I do not like it

      I tried deleting non-path nodes that may or may not be leaves, while keeping track of their mark (color) and ensuring correct alternation between turns.

      However, I’m getting WA (Wrong Answer). Can you suggest a test case where deleting a non-leaf node actually affects the final answer? Because from my reasoning, it shouldn’t — so I’d appreciate an example or insight proving otherwise.

      Thanks.

      My code
      • »
        »
        »
        »
        6 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        You can't pop back from ones if the zeros are empty, pop only if you in turn = 0, the same for zeros

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

In question D I have done somewhat the same as in the editorial (destroying leaf nodes while checking you are not standing on it using distances) My code is failing on some tc 344776961 ,can anyone help me with this .

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I am quite sure my solution for C2 can be hacked(TLE).

Can someone please verify?

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

3 19 17 14 7 17 18

in this above test case my output is correct meanwhile the judge say' answer should be 7 but your output is 24. Can anyone explain why is this happening ? my submission [link]

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    It should be 7 though. Increment 19 once at the cost of 7 and gcd(19+1, 14) becomes 2.

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

      my answer is coming 7 only in my compiler and even codechefs online compiler.. can u please check is it correct on your system too ?

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

        Yes, it comes out to be 7 in custom invocation too. Most probably it's a bug due to reusing global variables from the previous test case. The 11th test case on its own gives 7, but when running for the first 11 test cases at once it results in 24.

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

          you are right it might be causing due to global declaration..., btw thanks ^_^

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

Can someone please help me to understand for C2 why we are not able to do the operation on a number more than once? What if you have a really cheap operation and you just spam it to get to a number and this will be more optimal than any alternative.

I'm rlly confused because there is also an official test case that does more than one operation on a single element?

Testcase:

3
2 7 11
1 6 6

Correct Output:

5
  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    we do more than one operation for only that element with smallest b[i], so you did 5 operations on 2 since its b[i] is 1, we don't do this kind of operation on any other values as say you do 2 or more operations on a[i] with second smallest b[i], instead we could have done 1 or 0 operation on this a[i] and 1 or 0 operation on value with smallest b[i] making both even and gcd>=2.

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I think D is a fun problem. But I didn't solve C1, and I spent a lot of time on it. My point of D is even less than others' C1's.

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

The solution's F2, step 3 part have a very small problem:

$$$(r \le i\le r)$$$ should be modify to $$$(r \le i\le n)$$$

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

Alternative solution of D, without coloring.

Root the tree at vertex 1. Then, the following works:

Run DFS from the root, send instruction 1 when going down, send instruction 1 and then 2 x when going up from x. When running DFS in a node $$$x$$$ that can reach $$$n$$$, DFS the other childs that cannot reach $$$n$$$ first, then DFS the child that can reach $$$n$$$ and send instruction 1 and then 2 x. One actually see that this forms something like an euler tour so the steps won't go past $$$3n$$$. Obviously this is correct.

344706147

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

    Your solution is actually the same as editorial's and you using coloring implicitly.

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

    Thanks for sharing! I really like your view!

    Also, we can use $$$root = n$$$ (like in the editorial) and with your traversal idea code will simplify significantly — just one dfs! Essentially, we removing leaves while managing opposite color for current position, so the code will be correct.

    346889705

»
6 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

For D can somebody explain like why my parity approach that is whether the cat has covered odd or even distance doesnt give right ans when i delete farthest node (from root) of such parity instead of deleting some leaf of such parity(like the tutorial solution)

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

    like the farthest will obvi. be some or the other leaf in the complete process at all instances unless we are left with the main branch can somebody provide a counter case to this statement.

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

    What would be the farthest node in case of a chain tree like 1 -- 2 -- 3 -- 4?

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

    You must delete leaves to ensure connectivity, there will always be a leaf node that is not n, one way you can do this is by always deleting the farthest node from 6. Any other farthest from approach is flawed because you can't delete 6 and it may be the farthest node and then things get a bit icky.

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

[problem:2154] Cleaner and More Efficient Implementation 344796757

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

i just think c2 is much more difficult than D,but it's a interesting problem

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Might a O(n) solution to the problem E? 344800205

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

    Technically it's still O(N * logN) because of sorting. But interesting.

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

Ternary search solution was mentioned in tutorial for problem E. If somebody is interested, there is an implementation (using Fibonacci search, which is valid for integers): https://mirror.codeforces.com/contest/2154/submission/344804780

»
6 months ago, hide # |
Rev. 7  
Vote: I like it +16 Vote: I do not like it

Solution for problem D bonus (implement checker):

Spoiler
»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

344805176

Could someone tell me why it is causing TLE for C2, please?

»
6 months ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

In 2154C1, what was the need of first removing all the primes of a[i], then checking for a[i]+1 and then again including the primes of a[i]?

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    We remove the primes of a[i] to check if a[i] + 1 shares factors with any other number without interference of the current a[i], and then re-add them so those factors are available again for subsequent checks.

    • »
      »
      »
      6 months ago, hide # ^ |
       
      Vote: I like it +1 Vote: I do not like it

      you are right, but you know what, a[i] will never interfere with a[i]+1, as the least prime is 2, and if it divides a[i] then it can't divide a[i]+1. So, we don't have to do this removing and including of primes.

      My solution was accepted by just checking for primes of a[i]+1

»
6 months ago, hide # |
Rev. 3  
Vote: I like it -7 Vote: I do not like it

Intellegent I have questions in D, can't cat just go back to parent ( start from root 1 to 3 and then get back to 1) and also it is written that answer always exists for any tree but suppose root itself has 3 adjacent child and we can remove only one so cat can go to second child, how does answer exists here or am I missing a lot of things here?

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    You just use operation 1 to comeback to root after going to second child

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

we need minecraft implementation for D, as we have in A

»
6 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Easy explaination for C2

Spoiler
»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone please tell me why my submission for B is failing on pretest 7? The algorithm that I used is the same as that given in the editorial.

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

https://pastebin.com/22PxKa89 -- This is my submission for problem C2, can anyone please tell what is the issue with my code

»
6 months ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

is this ~~~~~ for (int i = 0; i < n; i++){ for (int x : pfac[a[i]]) cnt[x]--;

for (int x : pfac[a[i] + 1]){
        if (cnt[x] > 0)
            ans= min(ans, b[i]);
    }

    for (int x : pfac[a[i]])
        cnt[x]++;
}

~~~~~

in C2 std reasonable??

because gcd(a[i],a[i]+1)=1 so those cnt[x]++/cnt[x]-- cycle are useless

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

can anyone please tell me why this submisson 344840270 for C1 is giving tle

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

C2 is an amazing problem, I really liked it

combining Adhoc, Number Theory and some ez DS and implementation

Really a nice problem!! even it prevented me from being Expert :(

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

Can anyone explain me sample test cases output of C1

Input
2
1 1
1 1
Output
2

Reason we have to increase both 1's to 2 to make gcd(2,2) > 1

Input
5
1 1 727 1 1
1 1 1 1 1
Output:
2

Why answer is not 5, to make sure for each 1<=i,j<=n gcd(a[i], a[j) to be greater than 2 we have to increase all of the numbers by 1 making array as 2 2 728 2 2

Now, if we choose any (i,j) gcd(a[i], a[j]) > 1.

When we are saying answer is 2, which two indices values will be increased? How for each i,j gcd(a[i], a[j]) > 1?

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

was a bit too diffcult for someone like me

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

D is a nice problem.

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

can anyone tell me a case where my code is failing. 345020227

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

I used square root decomposition to solve problem E. My idea was that after a certain point, there would be a segment where the gain of the previous segment becomes greater than the current one — and those two segments would give the maximum gain for that point (or possibly the last segment).345001346

EDIT-> GPT code with comments so people can understand my messy code: 345025272

However, in the editorial, it mentioned using binary search. I thought about it for a while, but if for any point gain[mid] == gain[mid + 1], it seems like the binary search approach would fail. So I still can’t figure out exactly how it works. It would be great if someone could explain it.

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

    There may be such points. But you can discover that they only occur when gain[mid] is exactly the max.

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

for C2 i did not get intended solution for checking only index with lowest bi. So i made a solution to check for every pair- Solution — Instead of checking how far each number is from every prime, we can reason as follows:

1.If ai is not a prime number, then it must have a factor less than or equal to 500. Therefore, for any aj, there will always exist a number between aj and aj+500 such that gcd (ai,aj) > 1.

2.If ai is a prime number, we only need to consider ai itself rather than its factors. We can sort all numbers in increasing order, and for each prime, we iterate by multiplying it with an integer x. If aj*x< ai , we remove aj*x and insert aj*(x+1). Here, x will be at most 500.

//This is my code - 

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define input(a) for(auto &i:a)cin >> i;
#define srt(v) sort(v.begin(),v.end())
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)
#define vr vector
#define pb push_back
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define fr(i, n) for (ll i = 0; i < n; i++)
const int mod=1e9+7;
int main(){
    fastio;
    ll t;cin>>t;
    vector<vector<ll>>fre(200449+1);
    for(int i=2;i<=200449;i++){
        if(fre[i].size()>0){continue;}
        for(int j=i;j<=200449;j+=i){
            fre[j].pb(i);
        }
    }
    while(t--){
        ll n;cin>>n;
        vector<ll>v(n),v1(n);
        input(v);input(v1);
        vector<pair<ll,ll>>pri;fr(i,n){pri.pb({v[i],v1[i]});}srt(pri);
        fr(i,n){v[i]=pri[i].first;v1[i]=pri[i].second;}
        ll mn=1e18;
        ll fl=0;
        ll g=0;
       unordered_map<ll,ll>s;
        vector<ll>v2;
        fr(i,n){
            fr(j,fre[v[i]].size()){
                s[fre[v[i]][j]]++;
            }
            if((v[i]%2)!=0){v2.pb(v1[i]);}
        }
        if(v2.size()>1){srt(v2);mn=min(mn,v2[0]+v2[1]);}
        multiset<pair<ll,ll>>s1;fr(i,n){if(v[i]<500){continue;}s1.insert({v[i],v[i]});}
        fr(i,n){
            fr(j,fre[v[i]].size()){
                s[fre[v[i]][j]]--;
            }
            if(s1.size()>0){
                auto it=s1.begin();
                while((*it).first<v[i]){
                    ll p1=(*it).first,p2=(*it).second;
                    s1.erase(it);
                    s1.insert({((p1/p2)+1)*p2,p2});
                    it=s1.begin();
                }
                it=s1.find({v[i],v[i]});
                if(it!=s1.end()){s1.erase(it);}
                if(s1.size()>0){it=s1.begin();mn=min(mn,v1[i]*(((*it).first)-v[i]));}
                if(v[i]>=500){s1.insert({v[i],v[i]});}
            }
            ll p=449;
            for(int j=0;j<=p;j++){
                ll n1=v[i]+j;
                ll g1=0;
                if(n1==1){continue;}
                fr(k,fre[n1].size()){
                    if(s[fre[n1][k]]>0){
                        g1=1;mn=min(mn,j*v1[i]);break;
                    }
                }
                if(g1){break;}
            }
            fr(j,fre[v[i]].size()){
                s[fre[v[i]][j]]++;
            }
        }
        cout<<mn<<endl;
    }
    return 0;
}

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Guyz can anyone tell why this submission to problem C2 is failing :3 (please welp)

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

C2 was really intresting .

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

In D , was it necessary that only n must remain at the end , even if we can reach it beforehand ?

For example , for tree 1-3-2 , won't doing only operation 1 once sufficient ?

asking because I wrote solution considering that and got wrong answer on Pretest 2 , but later assumed that at last only n must remains , then it passes :(

Submission (pass) Submission (fail)

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

    The problem is that the checker is verifies the depth also. Your logic works correctly only when the cat and n are positioned in a straight linear path, for example: 1 -> 4, 4 -> 2, 4 -> 3.

    However, consider a case where the path is like 1 - 6 - 2. lets say 1 connects also to -> nodes 3, 4, and 5 . So, at the very least, you’d need to remove them so have to do like 2 3, 1 ,2 4, 1 ,2 5.

    That means 1 is used twice at list so the cat can reach point 2. This is why it’s giving a wrong answer (WA).

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

[del]

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

classic hollow knight references :)

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Problem D is beautiful.

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

i want to know why f2 step8 is correct,since the size of k is x

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hi, I'm working on C1 and I had the exact same idea as the editorial. However, I always get TLE on testcase 5. Can someone help me figure out what is the issue?

Spoiler
  • »
    »
    6 months ago, hide # ^ |
    Rev. 3  
    Vote: I like it 0 Vote: I do not like it

    In primes[] not only prime numbers

    Spoiler

    Also number of primes $$$\approx N / \ln N$$$. So for(int x : a) for(int p : primes) so long