Friendiks's blog

By Friendiks, 3 months ago, translation, In English

Thank you for participating in the round! We hope you enjoyed the problems. We worked very hard while preparing this contest :)

Hints for the problems will be published a bit later.

Rate the round

2238A - Another Puzzle from Papyrus

Idea: Friendiks

Solution
Code
Rate the problem

2238B - Crimson Triples

Idea: KotlechkovEgor

Solution
Code
Rate the problem

2238C - Village Guilds

Idea: lewc

Solution
Code
Rate the problem

2238D - Storming Arasaka

Idea: Friendiks

Solution
Code
Rate the problem

2238E - Cake Trial

Idea: lewc

Solution
Code
Rate the problem

2238F - Infinite Work

Idea: KotlechkovEgor

Solution
Code
Rate the problem
  • Vote: I like it
  • +80
  • Vote: I do not like it

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

Couldn't solve C,D on time, but the problems were great

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

Thanks for the problems. It was one of the most fun Div. 2 rounds in quite some time.

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

Problem E was easier than usual, but I think a lot of people would fail the testcase where every character is T, which results the answer being 1 rather than 0. The given solution also produced the same wrong answer.

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

    oops, I forgot to read the part that mentioned choosing an empty range, my bad.

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

    Easier than usual, maybe, but for a slightly more complicated dp problem there are way too many solves in contest.

    I guess there's only one explanation for this :/

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

Somehow D(by greedy logic) got accepted. Here for the expected solution.

Nice contest : )

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

Friendiks

Solution for A,B,C isn't visible, it says loading

UPD: It's finally visible, Thank you!

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

c can be solved with bfs and lca too

first of all for eg there lies a level which is 2 5 6 7

let the parents of 2 and 5 be 3 and parents of 6 and 7 be 4 then

the total number of different lcas you get is how your answer increases from n

for example

lca of 2 and 5 is 3 then lca of 5 and 6 is basically lca of 3 and 4 so let that be 1, then lca of 6 and 7 is 4 so 3 different lcas you got for this level so increase your answer by 3

do this for all levels

this works because naively you can run bfs for every node and count distinct levels, but if you observe then you'll see each level gets merged into one level which is a guild for an ancestor

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

I solved (not in time unfortunately) E in $$$O(n^2)$$$. Consider the following subproblem for fixed $$$D$$$: minimize the number of T under the constraint that $$$\sum_{i=l}^r x_i \le D$$$ for all intervals $$$[l, r]$$$. The subproblem can be solved greedily in $$$O(n)$$$ by starting setting all N->F and going left-to-right and maintaining the current prefix sum and maximum prior prefix sum of $$$x_i$$$; each time we violate the constraint, flip the rightmost (before our current position) possible N->F to be N->T. Solve the subproblem for each possible $$$D\in[0,n]$$$.

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

Yet Again D<C

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

Why contradiction proof for B? Direct proof is more educational and straightforward.

1) By definition of LCM:

$$$lcm(a,b) = kb$$$

and

$$$ lcm(b, c) = k'b$$$

2) Then:

$$$gcd(kb, k'b) = b \cdot gcd(k, k')$$$

3) Therefore b divides the left hand side, and so

$$$b | gcd(a, c)$$$

meaning b divides both a and c, as desired.

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

    a bit more on explanation if someone is looking for it..

    let gcd(a,c) = x then by definition x divides a and c and since b also divides x it is indirectly stated that b also divides a and c. :3

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

    better explanation than that of editorial, at least from my point of view.

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

Great contest! But I feel like cheating seemed much more prominent in today's contest because I didn't expect C to get that many solves to be honest.

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

    honestly C isnt a hard question by any means, im sure simulating the testcase is enough to get the intuition for it, but still implementation wise I dont expect much people to solve graphs, and seeing my rating dropped much lower than it normal does around the same rank im guessing a lot of lower rated people were able to solve it, this prob means there might have been more cheating involved here

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

      well just after the contest ended, I tried asking gemini some questions in Pro mode and it showed that the model was in high demand lmao, probably not coincidence

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

B is pretty OEISable, but great set overall!

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

This was one of the best Div. 2 rounds I've ever done!

Unfortunately didn't have time to solve E or F

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

Has anyone more clean explanation of task D? I just don't understand

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

    All primes have to be in different set else gcd condition is violated. Now the other sets will be like product of 2 primes, 3 primes goes on till (sum of exponents) primes. These all should form separate groups, u cant put them together as the former will be the divisor of later. So minimum possible no of sets will be no of primes + (sum of exponents) — 1 (cause we already considered the primes(product of 1 prime))

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

      good explanation, you can check my solution (sort of dp) also and please give upvote if u liked

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

    For every prime factor all of it's factors would have to go in different layers Consider 120,

    So 2 2×3×5 2^2×3×5 2^3×3×5 All in different buckets Similarly 3 3×5 And 5 Now any remaining factor can be put in one of these layers

    You can simulate this it is similar to sieve

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

      oh during contest i forgot that i can just simulate. But now that's not enough and i wanna understand proof

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

      for 120 , answer is 7 layer , so please give me 7 layer , which layer contain which divisor, i try lots of time but failed to manage it in 7 layer.

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

    `````````

    include <bits/stdc++.h>

    include

    using namespace std;

    using ll = long long int; const ll mod = 998244353;

    ll dp[1000000+1]{0}; ll prime [1000000 + 1]; vector<vector> factors(1e6+1); void solv(){ ll n ; cin>>n; // for(const auto & x : factors[n]) cout << x <<" ";cout<<endl; cout << dp[n] <<endl; // cout<<endl; } int main (){ cin.tie(0); ios_base::sync_with_stdio(0); dp[1] = 0; for(int i = 0 ; i <= 1e6;i++) prime[i] = 1; for(int i = 2; i<=1e6;i++){ if( prime[i] == 0){ continue; } for(int j = 2*i; j <=1e6;j+=i){ prime[j] = 0; factors[j].push_back(i); } } // cout<<prime[2] <<endl; for(int i = 2; i<= 1e6;i++){ if( prime[i]){ // prime no dp[i] = 1; continue; } ll ans = 0; for(const auto & x : factors [i ] ){ ll otherno = i/x; if(otherno%x)ans = max(ans , 1 + 1 + dp[i/x]); else ans = max(ans , 1+dp[i/x]); } dp[i] = ans; } // for(int i = 1;i<=10;i++) cout << // cout <<dp[2] <<endl; ll t ; cin>>t; while(t--){ solv(); } }

    ````````

    DP Solution for D, if you are comfortable with DP you can ask for proof from GPT

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

      edit i got to now dp[i] = 2 + dp[i/x] if x is only one in primefactor of n else dp[i] = 1 + dp[i/x] and we can take any prime x that is factor of i; so max is just useless as dp is just cool think

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

    I had the wrong idea implementing this, and somehow ran into a accepted solution.

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

Math-heavy questions always feel charming. Although I overcomplicated B

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

I know B is possible in $$$O(\sqrt{n})$$$ with square root algorithm combining or using blocks of equal values, but is there a better time complexity obtainable?

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

Hello Codeforces, I know I have low rating and I was unable to solve B in the contest. But here is my approach to proving the idea behind B, I feel this approach is less arbitrary and easier to come up with, please let me know if there are any flaws in my reasoning: gcd(lcm(a, b), lcm(b, c)) = gcd(a, c)

lcm(a, b) = a * (b / gcd(a, b))
lcm(b, c) = c * (b / gcd(b, c))

gcd(b / gcd(a, b), b / gcd(c, b)) = 1, this is a MUST for the condition to hold


this means these two terms share no common prime factors

X = b / gcd(a, b)
Y = b / gcd(c, b)

gcd(X, Y) = 1

a, b, c -> t, u, v (the corresponding exponents for each prime factor p)

X: u - min(t, u)
Y: u - min(v, u)

min(u - min(t, u), u - min(v, u)) = 0 <-> u - min(t, u, v) = 0

u = min(t, u, v) -> t, v >= u

since t, v >= u, for every prime factor p, b MUST be a factor of a and c
»
3 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Funnily enough, $$$O(t \sqrt{n})$$$ is allowed for D

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

Nice contest! Problem F is really cool.

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

All divisors with the same $$$\Omega \ge 2$$$ can be ordered so that adjacent ones have a common prime divisor.

This assertion is not trivial. I cannot prove it strictly in contest. It is intuitively correct, though.

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

    I agree. Here's how I proved it in round:

    Suppose we're doing the strategy where we place all numbers with $$$2^x 3^y 5^z$$$ where $$$x + y + z = L$$$ for level $$$L$$$ and $$$L \gt = 2$$$. (I'm using 2, 3, 5, but this works WLOG for any primes)

    The abstract strategy is to put (pure powers of 2), (at least one number containing both 2 and 3), (pure powers of 3), (at least one number containing both 3 and 5), (pure powers of 5). If a number fits into multiple categories then we can file it under any of them, e.g. it doesn't hurt to put $$$3^2 5^1$$$ under "pure powers of 3".

    Core idea: if $$$2^a$$$ for any value $$$a$$$ exists at this level, then $$$a = L$$$ trivially, and so must $$$2^{a-1} 3^1$$$. This is true as long as $$$a \ge 2$$$, which is true as long as $$$L \ge 2$$$.

    If there is no $$$2^a$$$, then there does not need to be a $$$2^b 3^c$$$ and we can go on to $$$3$$$. If there is a pure power of 3, then $$$3 \cdot 5$$$ is guaranteed to exist, and so on.

    Unrelated but I feel like D is a bit easy to cheese if you guess and don't prove, I wonder if it would be harder if we had to give a valid construction


    Edit: Actually I don't think this is fully rigorous. Maybe a better way is

    Suppose again $$$2^x 3^y 5^z$$$ where $$$x + y + z = L$$$. Then:

    • Write all numbers that contain factors of 2.
    • Now recurse on the subproblem $$$3^y 5^z$$$ where $$$y + z = L$$$. Works by induction.
    • In order to make sure that the transition works, we need to show that we can place a $$$2^a 3^b$$$ at the "border" at the end of the factors-of-2-region. I think this is always trivially true; in order to prove this you can take a random number from the $$$2$$$-region. It will either have an exponent of not-2 that you can decrement, or the power of 2 will be at least 2 so you can decrement it. Decrement that exponent and then multiply by $$$3$$$. Then clearly either the $$$3$$$-region will be empty, or it will start with something that is a multiple of 3.

    I think this one is much more rigorous and easy to implement.

    def solve(factors, p):
      a = list of all numbers with a factor of p in them
      place a number with a factor of next(p) in it at the end of a [in O(n)]
      a += solve(remaining factors, next(p))
      return a
    
    • »
      »
      »
      3 months ago, hide # ^ |
       
      Vote: I like it +8 Vote: I do not like it

      I had a similar idea, but instead worked from the other direction, by grouping divisors of $$$n$$$ by two properties:

      1. Their largest prime factor.
      2. Whether they are divisible by the previous prime factor.

      Then we output the groups in the following order:

      • divisors with largest factor equal to $$$p_1$$$
      • divisors with largest factor equal to $$$p_2$$$ and divisible by $$$p_1$$$
      • divisors with largest factor equal to $$$p_2$$$ and not divisible by $$$p_1$$$
      • divisors with largest factor equal to $$$p_3$$$ and divisible by $$$p_2$$$
      • divisors with largest factor equal to $$$p_3$$$ and not divisible by $$$p_2$$$
      • ...
      • divisors with largest factor equal to $$$p_k$$$ and divisible by $$$p_{k-1}$$$
      • divisors with largest factor equal to $$$p_k$$$ and not divisible by $$$p_{k-1}$$$

      Clearly these sets form a partition of all the divisors of $$$n$$$, and within each group all values share the highest factor. When the number of factors is at least 2, then while some of the early sets might be empty, when one set is nonempty, all subsequent sets are nonempty too. This provides the necessary connection between sets.

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

    I came up with this inductive construction

    Base case: With only one distinct prime, every Ω-layer has at most one element, so the claim is trivial.

    Induction hypothesis: Assume for m-1 distinct primes, every Ω-layer can be arranged such that adjacent elements have gcd > 1, and the last element of every Ω-layer contains the newest prime.

    Now add a new prime p.

    • For Ω = 2, extend the existing chain by adding all new pairs containing p.
    • For Ω = k > 2, every new divisor containing p is uniquely obtained by multiplying every element from Ω = k-1 layer from previous instance by p. This gives exactly all new elements of the Ω = k layer.
    • Since multiplying every element by the same prime preserves gcd, this new block is already internally valid.
    • Reverse this new block and append it to the old Ω = k chain. By the induction hypothesis, the last element of the old Ω = k chain and the last element of the previous Ω = k-1 chain both contain the previously added prime, so after reversing, the bridge also has gcd > 1.

    Hence every Ω-layer can be constructed for m primes, completing the induction.

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

My post contest discussion stream here and hints here

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

guys im getting really demotivated my rating isnt improving. How should I grind? I used to solve 2 now idk if i have lost skill or smthing

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

great contest!

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

For C, there is a better way to solve the question, which looks cleaner on the implementation side.

Instead of tracking absolute depths from the root and subtracting them later, the bottom-up DFS can directly return the maximum relative height of the subtree. Since every node contributes 1 to the answer we can initialize our answer from n. During the single DFS traversal, we can directly accumulate the unique guilds formed by the branching channels by adding the 2nd largest child height to the answer. Also to add on the official answer used 4 arrays, but it can be solved only using one.

you can check my submission here Submission

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

    Yeah I did this way and I think it is more intuitive than any crazy data structure solution

    380484282

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

    Very intuitive solution, thanks!

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

    I had done it without using dfs. Had to use 3 arrays one for finding the largest height of the child for each node, one for finding the second largest height and another one just to find the height of each node. Now we can see that for each node i; to find the second largest height of subtree the parents of the nodes<i are irrelevent as for the parent of a node i has to be less than i. So considering this it is just required to run a for loop from n-1 to 0, initialising the required value to 0 and every time adding the second largest child height + 2(one for the child and one for the node itself). Now it is just required to put a condition to check if the height of the present node is greater than the largest or second largest child's height w.r.t the parent of the present node and based on that make changes to the first and second arrays.

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

All problems are interesting!

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

Here’s an explanation for D which may be easier to understand:

First, find the prime factorisation of $$$n=p_1^{\alpha_1}\dots p_m^{\alpha_m}$$$

For example, let’s say $$$n=2^5\cdot 3^5 \cdot 5^5$$$.

Consider the divisor chain $$$[2], [2^2],\dots, [2^5],[2^5\cdot 3],[2^5\cdot 3^2]\dots [2^5\cdot 3^5\cdot 5^5]$$$

Since each divisor $$$d_i$$$ is a proper divisor of all other divisors $$$d_j$$$ for $$$j \gt i$$$, they each have to be in their separate layers.

Furthermore, all primes must be in their own layer, for which there are $$$m$$$ of them.

So $$$n$$$ must at least be in layer $$$A+m-1$$$, where $$$A=\sum_{i=1}^m \alpha_i$$$. We subtract 1 since we already considered $$$2$$$ as a layer so we don’t double count it when counting the number of primes.

Then to prove that these layers created are sufficient to fill all other divisors, consider the condition that no two numbers in a layer can be proper divisors of each other.

An easy way to avoid adding a number which divides some other number already in the layer, is to remove a prime factor and add another.

For example, in the layer with $$$[2^5\cdot 3^2]$$$, an easy way to add a divisor is to remove one multiple of 2, and add a multiple of 3 or 5. We will never add a proper divisor since a proper divisor must have a multiplicity (i.e. its value of $$$\Omega(x)$$$) strictly less than the current layer.

Note that we can’t only add or only remove prime factors, since that might end up adding a multiple or divisor some number in the layer. But for numbers with the same value of $$$\Omega(n)$$$, they will never be proper divisors of one another.

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

    for 120 , answer is 7 layer , so please give me 7 layer , which layer contain which divisor, i try lots of time but failed to manage it in 7 layer.

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

My Solution for Problem B. And I think its unique. no one has commented it yet.

Firstly lets change the equation by a bit.

$$$lcm(a,b)$$$ and $$$lcm(b,c)$$$ must contain all factors of $$$b$$$, so their gcd certainly contains $$$b$$$.

Beyond $$$b$$$, the only extra prime powers that can appear in both numbers are those common to both $$$a$$$ and $$$c$$$. So we can say that $$$gcd(lcm(a,b),lcm(b,c)) = lcm(b,gcd(a,c))$$$. So now the equation becomes $$$lcm(b,gcd(a,c)) = gcd(a,c)$$$.

The equation will only hold when $$$b$$$ is a divisor of $$$gcd(a,c)$$$.

Now, to solve this problem, we can either fix $$$b$$$ or $$$gcd(a,c)$$$. I fixed $$$gcd(a,c)$$$. As $$$a,c \leq n$$$, we have $$$1 \leq gcd(a,c) \leq n$$$. Let $$$gcd(a,c)=x$$$. (We will iterate over all $$$x$$$, where $$$1 \leq x \leq n$$$.) Now we need to count the number of pairs $$$(a,c)$$$ such that $$$gcd(a,c)=x$$$.

Since both $$$a$$$ and $$$c$$$ are divisible by $$$x$$$, write $$$a=xi$$$ and $$$c=xj$$$.

Then $$$gcd(a,c)=x$$$ if and only if $$$gcd(i,j)=1$$$.

So, for every $$$x$$$, our task reduces to counting the number of coprime pairs $$$(i,j)$$$ with $$$i,j \leq \lfloor n/x \rfloor$$$.

This can be computed efficiently using the Euler Totient Function. We precompute all totient values beforehand to answer each $$$x$$$ quickly. Now let the number of such pairs be $$$y$$$.Also, let $$$d$$$ be the number of divisors of $$$x$$$.

Since $$$b$$$ must be a divisor of $$$gcd(a,c)=x$$$, there are exactly $$$d$$$ possible choices for $$$b$$$. Therefore, for a fixed value of $$$gcd(a,c)=x$$$, the contribution to the answer is $$$y \times d$$$.

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

I solved A by myself like solution,but could't know why:(