skye_'s blog

By skye_, history, 7 months ago, In English

Recently I wrote my very first problem (that was $$$>$$$ D2A and not just a standard application of an algorithm) that I both wrote and solved myself.

Link: https://codeforces.com/gym/104669/problem/L

Editorial
  • Vote: I like it
  • +119
  • Vote: I do not like it

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

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

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

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

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

Great d2E level problem. Anyone reading should try it out :)

»
7 months ago, # |
  Vote: I like it -21 Vote: I do not like it

"guys give me contribution"

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

    giving you negative contribution

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

      the poster had sent in a discord server this message and i thought it wads funny i am not asking for contribution

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

        you could have posted the ss instead otherwise how could people know what you were indicating

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

orz

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

    nou

    I still don't understand how you solved for $$$O(a^{\frac{3}{4}} + \text{factorization}(R))$$$.

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

      That sounds interesting, I only have $$$O(a + \sqrt{b})$$$ solution.

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

        I think his idea was something along the lines of 'we probably don't have to check that many factors if we check from greatest to least and break once we find one that works' so instead of checking each of $$$b - 1$$$ intervals, he instead checked each factor $$$f$$$ in $$$O(\frac{R}{f})$$$ because if the factors are big, this will take less time and somehow found a bound on the number of factors we have to check but I didn't completely understand what he did.

        Btw if you want to, could you share your solution/if you liked the problem?

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

          My optimisation is based on the fact that if we look at values we can get by using $$$b/2$$$ elements, the length of this segment of values will be $$$\frac{b^2}{4}$$$ (roughly). So for any number less than that we surely have something divisible by it inside this segment. So we only need to check divisors that are greater than $$$\frac{b^2}{4}$$$. $$$R = \frac{b(2a+b-1)}{2} = O(b(a+b))$$$. Let's enumerate big divisors of $$$R$$$ by trying $$$\frac{R}{k}$$$ for small integer $$$k$$$. We can see that we only need to iterate over $$$\frac{4R}{b^2} = O(\frac{a}{b} + 1)$$$ values of $$$k$$$. Since checking each one of them takes $$$O(b)$$$ time, we can check all in $$$O(a+b)$$$.

          The problem is nice.

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

          We also don't need to check any divisors less than $$$a+\frac{b-1}{2}$$$: put $$$a$$$ and $$$a+b-1$$$ in the first set, and the rest in the second set, which immediately gives that as the GCD. (This is the best we can do, given that $$$R=b(a+\frac{b-1}{2})$$$ and we can force $$$b$$$ to be prime.)

          Together with the idea from @above, it suffices to check divisors $$$\frac{R}{k}$$$ for $$$k$$$ up to $$$\frac{R}{\max(b^2/4, a+\frac{b-1}{2})} \approx \min(4a/b, b)$$$. Because $$$R \approx (4a/b)(b^2)$$$ this minimum is at most $$$R^{\frac{1}{3}}$$$.

          Checking if a divisor $$$\frac{R}{k}$$$ works can also be optimized to $$$O(k)$$$: enumerating the $$$k$$$ multiples of $$$\frac{R}{k}$$$ less than $$$R$$$, it suffices to calculate the largest $$$x$$$ such that $$$a + (a+1) + \cdots + (a+x) = \frac{x(x+1)}{2} + (x+1)a \leq C$$$ with quadratic formula, and check if $$$C \leq (a+b-x-1) + \cdots + (a+b-1)$$$. Each multiple can be done in $$$O(1)$$$.

          Combining both of these optimizations, the complexity is essentially $$$O(\text{sum of factors of R below }R^{\frac{1}{3}})$$$. Admittedly I don't have an asymptotic bound for this, but empirical data using highly composite numbers suggests it should be at most $$$R^{\frac{1}{2}}$$$ for reasonable $$$R$$$.

          Then fixing $$$a$$$ and varying $$$b$$$, the upper bound is maximized when $$$b \approx \sqrt{a} \implies R \approx a^{3/2}$$$, which gives the bound $$$a^{3/4}$$$, plus finding the largest divisor at most $$$\frac{b^2}{4}$$$ if required.

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

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

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

TankU