Блог пользователя Timosh

Автор Timosh, 23 месяца назад, По-английски

In the recent National Olympiad in Informatics in our country, there was a problem, that many struggled to solve, it was worth 16 points, for being "the hardest problem". I got a great result then, for solving it fast. However, I forgot the actual solution. Anyways, here is the problem statement, ignoring the stories and drama:

You are given 3 integers, $$$a$$$, $$$b$$$ and $$$n$$$. Your task is to find any integers $$$x$$$ ($$$x \ge a$$$) and $$$y$$$ ($$$y \ge b$$$), such that $$$xy \ge n$$$, and $$$xy-n$$$ is minimised

I remember using a $$$O(\sqrt{n})$$$ solution, but not what it actually was. Anyone has ideas?

  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

If $$$n \leq a.b$$$ then $$$x=a$$$ and $$$y=b$$$. Otherwise either $$$x$$$ or $$$y$$$ should be lower than $$$\sqrt{n}$$$, so we can bruteforce it by fixing either $$$x$$$ or $$$y$$$.

  • »
    »
    23 месяца назад, скрыть # ^ |
    Rev. 3  
    Проголосовать: нравится 0 Проголосовать: не нравится

    It is not necessary that they be less than $$$\sqrt{n}$$$.

    UPD: Oh wait it is, never mind, I'm stupid... ugh I wrote this long as hell solution for nothing

    UPD2 (to reply to below comment because CF doesn't let me post): Yeah, I realize that now. Pretty nice, much better than my way at least

  • »
    »
    23 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится +4 Проголосовать: не нравится

    Surprisingly a simple and working approach. Eventhough, my solution does something with numbers from $$$n$$$ to $$$n+ \sqrt{n}$$$, lol. Anyways, thanks.

»
23 месяца назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

I guess this might be correct:

if(n<=a*b){

x=a;

y=b;

}

else{

if(a<b){

x=a;

y=ceil(n/a);

}

else{

x=ceil(n/b);

y=b;

}

}

Edit: This approach has been proved wrong through a counterexample.

»
23 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится +9 Проголосовать: не нравится

If $$$ab \ge n$$$, we have $$$xy \ge ab \ge n$$$ anyway, so we can just choose $$$x = a$$$ and $$$y = b$$$ for the optimal answer. Now, assume that $$$ab \lt n$$$ and consider fixing $$$x$$$. Then, we have $$$y \ge \max(\lceil \frac{n}{x} \rceil, b)$$$, and it's clearly optimal to choose $$$y = \max(\lceil \frac{n}{x} \rceil, b)$$$, because $$$x$$$ is fixed, and we want to minimize $$$xy - n$$$. Now, if $$$\max(\lceil \frac{n}{x} \rceil, b) = b,$$$ this means we have $$$\lceil \frac{n}{x} \rceil \le b$$$. Also, $$$y = b$$$ is fixed (remember that we found the optimal $$$y$$$ to be $$$\max(\lceil \frac{n}{x} \rceil, b)$$$), so it suffices to minimize $$$x$$$, subject to the condition $$$\lceil \frac{n}{x} \rceil \le b$$$. You can do this with some math (or binary search, if you aren't in the mood for thinking). Now, the other case: if $$$\max(\lceil \frac{n}{x} \rceil, b) = \lceil \frac{n}{x} \rceil$$$, we have $$$b \le \lceil \frac{n}{x} \rceil$$$, which means $$$b - 1 \le \frac{n}{x}$$$, which means $$$a \le x \le \frac{n}{b - 1}$$$. In particular, we have only $$$\mathcal{O}(\sqrt n)$$$ values of $$$\lceil \frac{n}{x} \rceil$$$, and we can check them all in roughly $$$\mathcal{O}(1)$$$ time (it will be a little tedious with three or four conditions to check, but pretty doable), so we are done (also I just realized the first part was kinda unnecessary... oh well).

UPD: After seeing the first comment, I realized this entire thing was unnecessary... oh well.