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

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

Recently, I came upon this problem:

Given: A, C, and that ceil(A/B) < C

Find: the smallest B possible

My solution was using binary search. However, the official solution came up with this clever closed form formula:

B = ceil(A/(C-1))

I'm completely boggled by this equation. I've seen quite a few closed form equations involving floor and ceil before, but I can't find how they are derived.

Any help, explanation, or pointers appreciated!

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

»
15 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

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

i suppose a mod b=0 so if a/b=k so a/k=b and if b is min so k is max k<c so k max is c-1 so b min is a/(c-1) (i think so )

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

I am not completely sure, but here is what I found:

First. we can see that ceil(a/b) = ((a-1)/b)+1(using integer division) because ((a-1)/b) will always result in ceil(a/b)-1. Now we have (a-1)/b + 1 < c, and from this we can get (a-1)/b < c — 1 and (a-1)/(c-1)<b. Here, if a,b and c are all positive integers, $$$a\geq c$$$ must hold, so $$$\frac{a}{c-1}$$$ will be smaller than $$$\frac{a-1}{c}$$$. And finally, we have ceil($$$\frac{a}{c-1}$$$) to make sure that the result is bigger than $$$\frac{a-1}{c-1}$$$.

The last part was mostly based on intuition though, and I'm new to using mathematical notation on codeforces, so I'm sorry if this was confusing.

»
15 месяцев назад, # |
Rev. 3   Проголосовать: нравится +64 Проголосовать: не нравится

I'll assume that $$$A, B, C$$$ are all required to be integers (else the claims don't make sense or are incorrect).

For any real number $$$x$$$ and integer $$$n$$$, note that the following two statements are equivalent by definition of ceil:

  • $$$x \le n$$$
  • $$$\lceil x \rceil \le n$$$

Also note that since $$$\lceil A/B \rceil$$$ and $$$C$$$ are both integers, $$$\lceil A/B \rceil < C$$$ is equivalent to $$$\lceil A/B \rceil \le C - 1$$$.

Combining these equivalences means that the original condition is equivalent to $$$A/B \le C - 1$$$.

We can't proceed further unless we know something about the sign of $$$C - 1$$$ and the restrictions on $$$B$$$. Indeed, if $$$C > 1$$$ and $$$B$$$ is allowed to be negative, then $$$B = -\infty$$$ works (more precisely, a smallest solution doesn't exist). Similarly, there might be no solutions when $$$C < 1$$$. In what follows, we will assume that $$$C > 1$$$ and $$$B$$$ is restricted to positive integers.

Now the derived inequality is equivalent to $$$B \ge A/(C-1)$$$. Again using the earlier equivalence gives that $$$B \ge \lceil A/(C-1) \rceil$$$.

Since all of these are equivalences, the smallest $$$B$$$ we're looking for is indeed what is claimed.

All in all, the main idea is to be familiar with properties of ceil and floor and to be comfortable with algebra and manipulating inequalities.

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

    Lifesaver!! This is the best explanation I have come across. Thank you so much, I truly appreciate it!