daftdove's blog

By daftdove, history, 14 months ago, In English

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!

  • Vote: I like it
  • +14
  • Vote: I do not like it

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

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

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

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 )

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

    Can you elaborate more? a mod b is not necessarily 0.

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

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.

»
14 months ago, # |
Rev. 3   Vote: I like it +64 Vote: I do not like it

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.

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

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