C. Anton and Fairy Tale

Codeforces
IDCF785C
Time1000ms
Memory256MB
Difficulty
binary searchmath
English · Original
Chinese · Translation
Formal · Original
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale: "Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..." More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of _n_ grains was full. Then, every day (starting with the first day) the following happens: * _m_ grains are brought to the barn. If _m_ grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account). * Sparrows come and eat grain. In the _i_\-th day _i_ sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing. Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day! ## Input The only line of the input contains two integers _n_ and _m_ (1 ≤ _n_, _m_ ≤ 1018) — the capacity of the barn and the number of grains that are brought every day. ## Output Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one. [samples] ## Note In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens: * At the beginning of the first day grain is brought to the barn. It's full, so nothing happens. * At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain. * At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it. * At the end of the second day two sparrows come. 5 - 2 = 3 grains remain. * At the beginning of the third day two grains are brought. The barn becomes full again. * At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain. * At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain. * At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty. So the answer is 4, because by the end of the fourth day the barn becomes empty.
Anton 喜欢听童话故事,尤其是当他的最好朋友 Danik 讲述时。现在 Danik 正在给 Anton 讲一个童话: "很久以前,有一位皇帝。他非常富有,拥有大量谷物。一天,他下令建造一个巨大的谷仓,用来存放他所有的谷物。最好的工匠们花了三天三夜建造了这个谷仓。但他们疏忽了,谷仓上留下了一个小洞,每天都有麻雀从洞中飞进来。一只麻雀飞进来,叼走一粒谷物,然后飞走了……" 更正式地说,童话中的情况如下:在第一天开始时,容量为 $n$ 粒谷物的谷仓是满的。然后,每天(从第一天开始)都会发生以下事情: - 白天:$m$ 粒谷物被运送到谷仓中,但如果谷仓已满,则多余的谷物会被丢弃。 - 晚上:第 $k$ 天晚上,会有 $k$ 只麻雀飞来,每只麻雀吃掉一粒谷物。 Anton 听腻了 Danik 描述每一只吃谷物的麻雀。他不知道童话何时结束,因此他请你确定谷仓第一次变空是在哪一天结束时。请帮助 Anton,编写一个程序来确定这一天的编号! 输入的唯一一行包含两个整数 $n$ 和 $m$ $ (1 ≤ n, m ≤ 10^{18}) $ —— 谷仓的容量和每天运来的谷物数量。 输出一个整数 —— 谷仓第一次变空的那一天的编号。天数从 1 开始编号。 在第一个样例中,谷仓容量为五粒谷物,每天运来两粒谷物。过程如下: 因此答案是 $4$,因为在第四天结束时谷仓变空了。 ## Input 输入的唯一一行包含两个整数 $n$ 和 $m$ $ (1 ≤ n, m ≤ 10^{18}) $ —— 谷仓的容量和每天运来的谷物数量。 ## Output 输出一个整数 —— 谷仓第一次变空的那一天的编号。天数从 1 开始编号。 [samples] ## Note 在第一个样例中,谷仓容量为五粒谷物,每天运来两粒谷物。过程如下: - 第一天开始时:谷物被运来。谷仓已满,无变化。 - 第一天结束时:一只麻雀飞来,吃掉一粒谷物,剩余 $5 - 1 = 4$ 粒谷物。 - 第二天开始时:两粒谷物被运来。谷仓变满,一粒谷物无法容纳。 - 第二天结束时:两只麻雀飞来,吃掉两粒谷物,剩余 $5 - 2 = 3$ 粒谷物。 - 第三天开始时:两粒谷物被运来。谷仓再次变满。 - 第三天结束时:三只麻雀飞来,吃掉三粒谷物,剩余 $5 - 3 = 2$ 粒谷物。 - 第四天开始时:两粒谷物被运来,剩余 $2 + 2 = 4$ 粒谷物。 - 第四天结束时:四只麻雀飞来,吃掉四粒谷物,剩余 $4 - 4 = 0$ 粒谷物。谷仓变空。 因此答案是 $4$,因为在第四天结束时谷仓变空。
**Definitions** Let $ n, m \in \mathbb{Z} $ be the initial capacity of the barn and the daily grain addition, respectively, with $ 1 \leq n, m \leq 10^{18} $. **Constraints** 1. $ n \geq 1 $, $ m \geq 1 $ 2. $ n, m \in \mathbb{Z} $ **Objective** Find the smallest day $ d \in \mathbb{Z}^+ $ such that the barn becomes empty by the end of day $ d $, where: - On day $ 1 $, the barn starts full with $ n $ grains. - Each day $ i \geq 1 $, $ m $ grains are added at the end of the day, **after** sparrows have eaten $ i $ grains during the day. Thus, the net grain change on day $ i $ is $ -i + m $. The barn is empty at the end of day $ d $ if: $$ n + \sum_{i=1}^{d} (m - i) \leq 0 $$ Simplify: $$ n + d \cdot m - \frac{d(d+1)}{2} \leq 0 $$ Find the minimal $ d \in \mathbb{Z}^+ $ satisfying: $$ n + dm - \frac{d(d+1)}{2} \leq 0 $$
Samples
Input #1
5 2
Output #1
4
Input #2
8 1
Output #2
5
API Response (JSON)
{
  "problem": {
    "name": "C. Anton and Fairy Tale",
    "description": {
      "content": "Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale: \"Once upon a time, there lived an emperor. He was very rich and",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF785C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:\n\n\"Once upon a time, there lived an emperor. He was very rich and...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Anton 喜欢听童话故事,尤其是当他的最好朋友 Danik 讲述时。现在 Danik 正在给 Anton 讲一个童话:\n\n\"很久以前,有一位皇帝。他非常富有,拥有大量谷物。一天,他下令建造一个巨大的谷仓,用来存放他所有的谷物。最好的工匠们花了三天三夜建造了这个谷仓。但他们疏忽了,谷仓上留下了一个小洞,每天都有麻雀从洞中飞进来。一只麻雀飞进来,叼走一粒谷物,然后飞走了……\"\n\n更正式地说,童话中的...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, m \\in \\mathbb{Z} $ be the initial capacity of the barn and the daily grain addition, respectively, with $ 1 \\leq n, m \\leq 10^{18} $.\n\n**Constraints**  \n1. $ n \\geq 1 $, $ m...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments