Please read the new rule regarding the restriction on the use of AI tools. ×

nishkarsh's blog

By nishkarsh, history, 5 hours ago, In English

A. Find Min Operations

By nishkarsh

Hint 1
Hint 2
Solution
Code

B. Brightness Begins

By nishkarsh

Hint 1
Hint 2
Solution
Code 1
Code 2

C. Bitwise Balancing

By P.V.Sekhar

Hint 1
Hint 2
Solution
Code

D. Connect the Dots

By P.V.Sekhar

Hint 1
Hint 2
Solution
Code

E. Expected Power

By nishkarsh

Hint 1
Hint 2
Solution
Code

F. Count Leaves

By nishkarsh

Hint 1
Solution
Code
  • Vote: I like it
  • -119
  • Vote: I do not like it

»
2 hours ago, # |
  Vote: I like it +108 Vote: I do not like it

whats the point of allowing $$$O(n \cdot 1024)$$$ solutions in E

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it -39 Vote: I do not like it

    well, we reduced the bound on A to make sure nlog^2A passes.

    We didn't knew that these solutions existed.

    • »
      »
      »
      116 minutes ago, # ^ |
        Vote: I like it +14 Vote: I do not like it

      know*

    • »
      »
      »
      103 minutes ago, # ^ |
        Vote: I like it +2 Vote: I do not like it

      I think $$$a_i < 2^{14}$$$ or something would've been the optimal choice if i am not mistaken $$$O(n \log^2 A)$$$ should pass as long as there is no bad constant factor and the more naive solution wouldn't have passed but i am sure it was hard to predict that such solutions would pop up + thanks for the amazing contest.

      • »
        »
        »
        »
        82 minutes ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        It's not hard to expect that the dp 1024n is too easy and obvious to anyone who has solved dp+ev before

        • »
          »
          »
          »
          »
          59 minutes ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Yes it is straight forward but $$$n$$$ is still upto $$$2 \cdot 10^5$$$ so passing shouldn't seem smooth, some people even got TLE.

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

    my O(n * 1024) solution passed (4000ms), so I optimized to O(min(n, 1024) * 1024) and it passed 2500ms.

    • »
      »
      »
      54 minutes ago, # ^ |
        Vote: I like it +2 Vote: I do not like it

      This also didn't seem intended but yeah this solution also exists and it runs in $$$O(\text{max } a_i^2)$$$.

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

Can anyone explain how did we get the direct formula in B??

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Every number has an even number of factors, except square numbers.

    We can prove the first fact by finding a factor a of number n. Then, if n is not a square n/a is another factor of n.

    If n is a square, then we double count sqrt(n), as we count n/sqrt(n) and sqrt(n) which are the same value. This leads to an odd number of factors(every other factor + 1).

    Using this, if a switch is flipped an even number of times, it returns back to 1. If it flipped an odd number of times, it returns to 0.

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

      what is the explanation of n-sqrt(n)>=k?

      • »
        »
        »
        »
        109 minutes ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        sqrt(n) gives how many square numbers are there for n, since squares cant be On we need to subtract them to get total lights on and the RHS is since we need to check if ON is atleast k to get the binary search going.

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

      Check the solution discution on YT.

    • »
      »
      »
      56 minutes ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      This does not answer the question which was about how to get the direct formula: $$$n=⌊k+\sqrt{k}+0.5⌋$$$

»
2 hours ago, # |
  Vote: I like it -14 Vote: I do not like it

This was a good round, unless I FST ofc, then it was a horrible round. I think that there should be more problems like this — problems that don't require so much IQ, but $$$do$$$ require coding. These problems are what might be called chill problems.

»
2 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

Now after seeing the solution of B problem.... I just want to cry ):

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

I was able to figure out that number of ON bulbs after n operations is related to sqrt(n) but was going in opposite track. Here only perfect squares are off and other are ON.

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

I understood the binary search solution for problem B. But, I am not able to figure out how direct formula $$$n = \lfloor k + \sqrt{k} +0.5 \rfloor$$$ came. Can anyone please explain it?

  • »
    »
    17 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    dont think abt that farmula number of number of perfect squares<=k and add to k(ie (int)sqrt(k)) now we can only cross atmost one more square in b/w [k,k+(int)sqrt(k)] which is ((int)sqrt(k)+1)^2 check for that and add 1 the farmula given directly checks it mathematically

»
118 minutes ago, # |
  Vote: I like it +6 Vote: I do not like it

In problem B, how do we derive the formula $$$( n = \left\lfloor k + \sqrt{k} + 0.5 \right\rfloor )$$$ from the equation $$$( n - \left\lfloor \sqrt{n} \right\rfloor = k )$$$?

»
116 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

If maxa is sth like 2^20, E will be a good problem. What a pity.

»
116 minutes ago, # |
  Vote: I like it +26 Vote: I do not like it

A O(1) solution for C exists its just b ^ d or -1. This comes from simplifying the truth table.

  • »
    »
    101 minute(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    O(1) solution for B exists toocout << k + (int)sqrtl(k + (int)sqrtl(k)) << endl;

    • »
      »
      »
      93 minutes ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      I don't think this is O(1) since sqrt is log(n) time complexity

      • »
        »
        »
        »
        90 minutes ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        It's considered to be constant in bit complexity, just as how we consider other arithmetic operations to have constant complexity.

  • »
    »
    50 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Amazing

»
116 minutes ago, # |
  Vote: I like it +11 Vote: I do not like it

In problem C simply set a to b xor d and check if it works

»
115 minutes ago, # |
  Vote: I like it +1 Vote: I do not like it

You put image of editorial C to editorial D. By accident I suppose

»
113 minutes ago, # |
  Vote: I like it +4 Vote: I do not like it

I got the idea of problem B fast because of 1909E - Multiple Lamps.

The idea
»
112 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

How was I supposed to solve that B problem, I literally had 0 idea that I will have to build a formulae for that, plus the observation required for the problem isn't normal at all.

»
108 minutes ago, # |
  Vote: I like it +9 Vote: I do not like it

contest is too math

»
106 minutes ago, # |
  Vote: I like it +7 Vote: I do not like it

In B no need for a formula, you can just binary search for the answer, it only needs that observation

  • »
    »
    100 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    That observation is too much for me, tell me how much more I need to practice in order to get that B always right.

    • »
      »
      »
      85 minutes ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I saw that observation before in blogs but usually you just need practice to get observations fast, if you aren't sure how to practice correctly I think there's lots of cf blogs on it

  • »
    »
    7 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I personally guess that formula was a trap for chatgpt cheaters

»
106 minutes ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Can someone help me with my code for E. I am finding probability for each bit to be active or not then for all number adding it's contribution to expectation.

Code

z[i][0][j] represent prob in first i element our subset's xor will have j'th bit set

z[i][1][j] represent prob in first i element our subset's xor will not have j'th bit set

then for each number(0-1023) i add contribution number 5's contribution would be simply (z[n-1][0][0]*z[n-1][1][1]*z[n-1][2][0])*5*5

  • »
    »
    103 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Squaring is not linear so you can't handle bit by bit without doing the editorial's trick, what works is z[i][j] represents probability in first i element that the xor is equal to j

    • »
      »
      »
      101 minute(s) ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      i do understand editorial's way but can't find mistake in my own approch. can you may be give me little example

      • »
        »
        »
        »
        77 minutes ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Oh I didn't understand your solution, it looks like it should work, there might be an implementation mistake somewhere

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

    At first I thought of something similar for this problem. The issue is that the probability of getting a 1 on the ith bit is not independent to the probability of getting a 1 on the jth bit. An example of this may be that given the case:

    1

    3

    5000

    I can either take the 2^0 bit and the 2^1 bit or I can take neither, yet your implementation may give some probability to 2's contribution.

    • »
      »
      »
      74 minutes ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Makes sense I think that's the problem with the solution

    • »
      »
      »
      74 minutes ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Ahhh!! you are absolutely right i see problem now.

»
106 minutes ago, # |
  Vote: I like it +14 Vote: I do not like it

The editorial solution for E is too complicated. There is a much easier O(1024 * n) dynamic programming solution which comfortably fits within time limit.

»
103 minutes ago, # |
  Vote: I like it +7 Vote: I do not like it

Why is Carrot not working !?

»
99 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it
»
90 minutes ago, # |
  Vote: I like it +5 Vote: I do not like it

Seems like you didnt consider two much easier solutions in D and E

»
77 minutes ago, # |
Rev. 2   Vote: I like it +6 Vote: I do not like it

Problem D has a much easier $$$O(nd^2 + m)$$$ solution.

»
74 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

Hello can anyone help me why B submission is getting WA on Test case 8?

»
58 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone tell me what I am doing wrong looking at my profile, I am unable to reach pupil

»
51 minute(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

$$$O(nd+m)$$$ solution to D: 283633809 We can brute force all edges and run dfs for connected components, since there are at most $$$2*d$$$ edges per node.

»
49 minutes ago, # |
Rev. 4   Vote: I like it +1 Vote: I do not like it

Can anyone help me debug my code, It failed test case 8. I tried so hard today but not enough :(

Submission:283668245

»
47 minutes ago, # |
  Vote: I like it +2 Vote: I do not like it

For C, can someone explain why it is bit independent? I am unable to wrap my head around it

  • »
    »
    21 minute(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Assume it is not bit independent. Then there must be some i, for which (a|b)'s i-th bit is not set and (a&c)'s i-th bit is set. If (a&c)'s i-th bit is set, then a's i-th bit must also be set. But if a's i-th bit is set, then (a|b)'s i-th set must also be set, which is a contradiction. Therefore it is bit independent.

»
44 minutes ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

An alternative solution to the problem D in $$$O(max_d * n + m)$$$

Consider the point $$$x$$$, note that it can only be connected to the $$$max_d$$$ points that go in front of it. We will support $$$dp[x][d] = k$$$, where $$$x$$$ is the coordinate of a point on a numeric line, $$$d$$$ is the length of the reverse step, $$$k$$$ is the maximum number of steps that will be taken from point $$$x$$$ with step $$$d$$$. Then note that we can move from point $$$x - d$$$ to point $$$d$$$ only if $$$dp[x - d][d] > 0$$$. In this case, we will draw an undirected edge between the points $$$x - d$$$ and $$$d$$$. Recalculate the dynamics for point $$$x$$$ as follows $$$dp[x][d] = max(dp[x][d], dp[x - d][d] - 1)$$$

Dynamics base, initially for all $$$x$$$ and $$$d$$$, $$$dp[x][d] = 0$$$. Now for all m triples $$$a_i, d_i, k_i$$$ we get $$$dp[a_i][d_i] = max(dp[a_i][d_i], k_i)$$$

At the end, using dfs, we will find the number of connectivity components

Code of this solution: 283668708

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

In the editorial of F, I think it should be $$$f(p^{ik},d)$$$ instead of $$$f(p^{i}k,d)$$$

»
36 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it
  • »
    »
    32 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    might be a rounding issue. generally a good idea to avoid non-integer types unless absolutely necessary

»
21 minute(s) ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Where's the originality?

Problem Codeforces 976 Div2 $$$F$$$ be directly copied from AtCoder Beginner Contest 370 G link: https://atcoder.jp/contests/abc370/editorial/10906

The core idea of both problems is absolutely identical, including the approach of solving them with a convolution technique. The only noticeable difference between the two problems lies in how the function $$$f(prime)$$$ and $$$f(prime^{ki}, b)$$$ is computed. Other than that, the rest of the structure, including the logic and solution techniques, are the same. This raises concerns about originality and fair practice in problem setting across competitive programming platforms.

Problem Codeforces 976 Div2 $$$E$$$ has solution with the most basic dp idea with $$$O(n \cdot 1024)$$$.

As someone who placed officially 12-th in Div 2, I’m absolutely disappointed with how Codeforces 976 Div2 F turned out.

»
21 minute(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

for problem b, Doing the same idea but initializing right bound as 2*k instead of 2e18 is considered wrong answer on test 8. how helpful

»
18 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

An easier alternate solution to D which doesn't use DP: Similar to editorial solution I used a DSU to keep track of components, but to check whether elements $$$i$$$ and $$$i+d_i$$$ are connected, I simply need to check among operations $$$j$$$ where $$$d_j = d_i$$$, and $$$a_j \% d_j = i\%d_i$$$. I used a map to store sets of operations together based on $$$d_j$$$ and $$$a_j\%d_j$$$, and I stored each operation as a pair $$$[ a_j, a_j + k_j \cdot d_j ]$$$. Since each operation can now simply be represented as an interval, I used a datastructure which I called an IntervalSet which internally uses an std::set to efficiently a store intervals in amortized $$$O(\text{log n})$$$ and query whether an interval is completely included in the set in $$$O(\text{log n})$$$ where $$$n$$$ is the number of intervals in the set. This allows me to simply query whether $$$[i,i+d_i]$$$ is included in the among the operations with $$$d_j = d_i$$$, and $$$a_j \% d_j = i\%d_i$$$ which makes the code very simple.

Code

My submission: 283669482

PS: I used ChatGPT to help in implementing the IntervalSet class so its not in a great state rn;) and I haven't seen any implementations of such an IntervalSet class, so I would love to learn about any other implementations you guys know about.