#P11181. Minimizing Illustration Pages
Minimizing Illustration Pages
Minimizing Illustration Pages
An ancient book is discovered during an archaeological expedition. The book’s pages are of two types: text pages and illustration pages. Each page is either a text page, where the page number is printed, or an illustration page with no page number. It is known that the first \(k\) pages are all illustration pages. The remaining pages might be either text or illustration pages.
If a page is a text page, its printed number equals its position in the book. The sum of all printed page numbers (i.e. those on text pages) is \(s\). However, the total number of pages in the book and the arrangement of text and illustration pages (after the first \(k\) pages) are unknown.
Your task is to determine the minimum possible total number of illustration pages in the book, given \(k\) and \(s\). Note that if \(s=0\), then there are no text pages and the book consists solely of the first \(k\) illustration pages.
Hint: Suppose there are \(t\) text pages among the pages after the first \(k\) pages. The minimum possible sum of the printed page numbers on the text pages is achieved if these pages are numbered consecutively as \(k+1, k+2, \ldots, k+t\). In that case the sum is
[ T_{\text{min}} = t,k + \frac{t(t+1)}{2}. ]
If the actual sum \(s\) is larger than \(T_{\text{min}}\), then extra illustration pages must be inserted among the later pages, increasing the page numbers of some text pages. If you denote \(N\) as the total number of pages and note that all text pages come from the range \(k+1\) to \(N\), then the number of illustration pages (beyond the first \(k\)) is \(N-t\). Our goal is to choose \(t\) and \(N\) such that the sum of text page numbers equals \(s\) and \(N-t\) is minimized. It turns out that the minimum total number of illustration pages is given by
[ \text{Answer} = k + \left\lceil \frac{s - \left(t,k + \frac{t(t+1)}{2}\right)}{t} \right\rceil, ]
where \(t\) is the maximum integer satisfying
[ t,k + \frac{t(t+1)}{2} \le s. ]
inputFormat
The input consists of two space‐separated integers \(k\) and \(s\) on a single line.
- \(k\) (\(0 \le k \le 10^9\)) is the number of initial illustration pages.
- \(s\) (\(0 \le s \le 10^{18}\)) is the sum of printed page numbers.
outputFormat
Output a single integer, the minimum possible total number of illustration pages in the book.
sample
0 14
1