qwexd's blog

By qwexd, 3 weeks ago, In English

Codeforces Round 1121 — Editorial

Thanks for participating!

2264A - Rumb Needs a Hand

Problem by qwexd.

Hint
Solution
Code (GNU C++17)

2264B - Knife's Pill Farm

Problem by qwexd.

Hint 1
Hint 2
Solution
Code (GNU C++17)

2264C - Madamant's Skating Dynasty

Problem by qwexd.

Hint 1
Hint 2
Solution
Code (GNU C++17)

2264D - Dr. Agos's Dark Mode

Problem by qwexd.

Hint 1
Hint 2
Hint 3
Solution
Code (GNU C++17)

2264E1 - A Prime Flood (Easy Version)

Problem by qwexd.

Hint 1
Hint 2
Hint 3
Solution
Code (GNU C++17)

2264E2 - A Prime Flood (Hard Version)

Problem by qwexd and solved for bigger constraints by jeroenodb.

Hint 1
Hint 2
Hint 3
Solution
Code (GNU C++17)

2264F - Deranged Calculator

Problem by jeroenodb. Testing tool created by turska

Hint 1
Hint 2
Hint 3
Solution
Code (PyPy 3)
Code (PyPy 3) simplified and golfed
  • Vote: I like it
  • +149
  • Vote: I do not like it

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

F can be solved in 1661 characters

Solution by ChatGPT: 390658725

»
3 weeks ago, hide # |
← Rev. 2  
Vote: I like it -37 Vote: I do not like it

Deleted

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

Got it

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

As an author, thanks for participating! I am pretty curious how small expressions for problem F you can get. The editorial is in no way the best approach, but it was the original way I solved the problem. Testers already found some different approaches.

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

Sleepforces

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

COMBINAforces

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

Great problems!

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

I am a bit surprised my approach 390666998 upsolving F was not the intended one, since I think mine relies on less esoteric knowledge. The closed-form formula $$$D_k = k!\sum_{i=0}^k \frac{(-1)^i}{i!}$$$ (which I think is standard/well-known?) can be rewritten $$$\sum_{i=0}^k (-1)^{k-i}\frac{k!}{(k-i)!}$$$ which expands to $$$(-1)^k\left(1-k+k(k-1)-k(k-1)(k-2)+\cdots\right)$$$ and crucially we can extend this to any number $$$ \gt k$$$ terms, since any term with $$$k-k$$$ is zero. We can group terms to make this $$$(-1)^k\left(1-k(1-(k-1)(1-(k-2)(\cdots)))\right)$$$ which for the first $$$n+1$$$ terms makes a string of size $$$O(n^2)$$$. The $$$(-1)^k$$$ is the only part that actually needs rounding here, in order to extract the bottom bit.

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

My solution for D:

if (n == 1) cout << "1" << '\n';
    else if (n == 2) cout << "11" << '\n';
    else if (n == 3) cout << "101" << '\n';
    else if (n == 4) cout << "0101" << '\n';
    else if (n == 5) cout << "10101" << '\n';
    else {
        string ans;
        string x((n - n % 6) / 3 - 1, '0');
        if (n % 6 == 0) {
            ans = x + "1" + x + "1" + x + "1";
        }
        else if (n % 6 == 1) {
            ans = x + "1" + x + "1" + x + "01";
        }
        else if (n % 6 == 2) {
            ans = x + "01" + x + "1" + x + "01";
        }
        else if (n % 6 == 3) {
            ans = x + "001" + x + "1" + x + "01";
        }
        else if (n % 6 == 4) {
            ans = x + "1" + x + "001" + x + "001";
        }
        else if (n % 6 == 5) {
            ans = x + "01" + x + "001" + x + "001";
        }
        cout << ans << '\n';
    }

I did brute-force for first 20 n and notice the pattern...

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

    my solution:

    i had already known till 6 (which is trivial). then understood that if total number of 0s for case of 1 is bigger than 3 then one of the 1 will be at the end. then just compare the optimal 2 vs optimal 3 solution. ~~~~~ void solve() { int n;

    cin >> n;

    if (n < 7) {

    fora(i, 0, (n / 2) * 2, 2) { cout << "10"; }
    
    if (n & 1)
    
      cout << "1";
    
    return;

    } else {

    int z2 = n &mdash; 2;
    
    int div2 = z2 / 3;
    
    int rem2 = z2 % 3;
    
    ll two = 0;
    
    al(3) arr2{div2, div2, div2};
    
    rep(i, rem2) arr2[i]++;
    
    if (rem2 == 0 and div2 % 2 == 0) {
    
      arr2[0]--, arr2[1]++;
    
    }
    
    rep(i, 3) two += (arr2[i] * (arr2[i] + 1)) / 2;
    
    int z3 = n &mdash; 3;
    
    int div3 = z3 / 3;
    
    int rem3 = z3 % 3;
    
    ll thr = 0;
    
    al(3) arr3{div3, div3, div3};
    
    if (rem3 == 0 and div3 % 2 == 0) {
    
      arr3[1]--, arr3[2]++;
    
    } else if (rem3 == 0 and div3 % 2 == 1) {
    
      arr3[0]--, arr3[2]++;
    
    } else {
    
      rep(i, rem3) arr3[i]++;
    
      if (div3 % 2 == 0) {
    
        swap(arr3[0], arr3[2]), swap(arr3[1], arr3[2]);
    
      } else {
    
        swap(arr3[1], arr3[2]);
    
      }
    
    }
    
    rep(i, 3) thr += (arr3[i] * (arr3[i] + 1)) / 2;
    
    thr += arr3[1] + 1;
    
    if (two < thr) {
    
      rep(i, arr2[0]) cout << '0';
    
      cout << '1';
    
      rep(i, arr2[1]) cout << '0';
    
      cout << '1';
    
      rep(i, arr2[2]) cout << '0';
    
    } else {
    
      cout << '1';
    
      rep(i, arr3[0]) cout << '0';
    
      cout << '1';
    
      rep(i, arr3[1]) cout << '0';
    
      cout << '1';
    
      rep(i, arr3[2]) cout << '0';
    
    }

    }

    }

    ~~~~~

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

I had a different approach to C:

You can build the answer iteratively, processing values from largest to smallest. Maintain the sum of all values already added ($$$sum$$$) and the number of valid trees that can be formed so far ($$$ways$$$).

When considering the $$$k$$$th largest node (assume 0-indexing), it can be attached below any of the $$$k$$$ existing nodes. Therefore, whatever the running answer was before should be multiplied by $$$k$$$, since every previous tree now produces $$$k$$$ new trees distinguished only by node $$$k$$$'s parent.

Furthermore, across those $$$k$$$ possible parent choices, node $$$k$$$ contributes $$$\sum\limits_{v}{(v - a_k)} = sum - k(a_k)$$$. Multiply this quantity by $$$ways$$$ (before updating it) to account for making these attachments in every previously valid tree.

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

In D — anyone care for explaining why we didn't trynna break the blocks into 4 chunks with 3 ones?, and even when we did use 3 ones we put the last one at the end, essentially just creating 3 blocks of zeroes.

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

    look at the prefix sum modulo 3, it is either 0 or 1 or 2. So if you create 4 blocks, there must be two block with the same modulo. Their contribution to the number of subarray is still number of pair (l, r) which have the same prefix sum...

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

      Yeah i get the repeating-modulo logic but could not confirm that if it made "the breaking into 4 parts" a worse solution or not. Hence i did the MATHS

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

    look at the prefix sum modulo 3, it is either 0 or 1 or 2. So if you create 4 blocks, there must be two block with the same modulo. Their contribution to the number of subarray is still number of pair (l, r) which have the same prefix sum...

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

Another solution for F: Consider the recurrence $$$D_i = i \times D_{i-1} + (-1)^i$$$, which can be adapted as $$$D_i = (1 + (i-1) \times [n \geq i]) \times D_{i-1} + (-1)^i \times [n \geq i]$$$. A naive implementation of this approach (representing each number as $$$\frac{n+n+\cdots+n}{n}$$$) can result in a solution with a little bit more than 10000 characters. To get AC, we can use DP to minimize the length of the string used to represent each number. See: 390750464

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

i dont get it bruh , i am stuck at 1000-1100 and it sucks doing permutation ques everyday and still not able to get them right, somehow the way of solving or the concept is totally different....no perfect ladder to climb man!

»
13 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

A difficult div2, but nice problem

»
8 days ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

I have a dp solution for problem C more or less it's a contribution technique . well we can define dp[i] as the sum of all valid dinasty trees with nodes containing n , n-1 , .. , i .

talking about transition when we are adding the ith node to the tree we can have n , n-1 , n-2 , .. i+1 as its parents assuming the array is in sorted order .

so dp[i] = (n-i)*dp[i+1] + factorial(n-1-i) * (pref[j] — pref[i] — (n-1)*a[i]) ; where pref array is the partial sum of array a . (n-i) is the no of all the nodes which are greater than ith node and factorial(n-1-i) is the number of different trees we have formed so far so each node a[j] — a[i] would contribute to factorial(n-1-i) times when adding ith node .