A. Chips

Codeforces
IDCF92A
Time2000ms
Memory256MB
Difficulty
implementationmath
English · Original
Chinese · Translation
Formal · Original
There are _n_ walruses sitting in a circle. All of them are numbered in the clockwise order: the walrus number 2 sits to the left of the walrus number 1, the walrus number 3 sits to the left of the walrus number 2, ..., the walrus number 1 sits to the left of the walrus number _n_. The presenter has _m_ chips. The presenter stands in the middle of the circle and starts giving the chips to the walruses starting from walrus number 1 and moving clockwise. The walrus number _i_ gets _i_ chips. If the presenter can't give the current walrus the required number of chips, then the presenter takes the remaining chips and the process ends. Determine by the given _n_ and _m_ how many chips the presenter will get in the end. ## Input The first line contains two integers _n_ and _m_ (1 ≤ _n_ ≤ 50, 1 ≤ _m_ ≤ 104) — the number of walruses and the number of chips correspondingly. ## Output Print the number of chips the presenter ended up with. [samples] ## Note In the first sample the presenter gives one chip to the walrus number 1, two chips to the walrus number 2, three chips to the walrus number 3, four chips to the walrus number 4, then again one chip to the walrus number 1. After that the presenter runs out of chips. He can't give anything to the walrus number 2 and the process finishes. In the third sample the presenter gives one chip to the walrus number 1, two chips to the walrus number 2, three chips to the walrus number 3, then again one chip to the walrus number 1. The presenter has one chip left and he can't give two chips to the walrus number 2, that's why the presenter takes the last chip.
有 #cf_span[n] 只海象围坐成一圈,它们按顺时针方向编号:编号为 #cf_span[2] 的海象坐在编号为 #cf_span[1] 的海象的左边,编号为 #cf_span[3] 的海象坐在编号为 #cf_span[2] 的海象的左边,...,编号为 #cf_span[1] 的海象坐在编号为 #cf_span[n] 的海象的左边。 主持人有 #cf_span[m] 块糖果。主持人站在圆圈中央,从编号为 #cf_span[1] 的海象开始,按顺时针方向分发糖果。编号为 #cf_span[i] 的海象会得到 #cf_span[i] 块糖果。如果主持人无法给当前海象所需的糖果数量,则主持人拿走剩余的糖果,过程结束。给定 #cf_span[n] 和 #cf_span[m],请确定主持人最终能得到多少块糖果。 第一行包含两个整数 #cf_span[n] 和 #cf_span[m] (#cf_span[1 ≤ n ≤ 50], #cf_span[1 ≤ m ≤ 104]),分别表示海象的数量和糖果的数量。 请输出主持人最终剩下的糖果数量。 在第一个样例中,主持人给编号为 #cf_span[1] 的海象 1 块糖果,给编号为 #cf_span[2] 的海象 2 块糖果,给编号为 #cf_span[3] 的海象 3 块糖果,给编号为 #cf_span[4] 的海象 4 块糖果,然后再次给编号为 #cf_span[1] 的海象 1 块糖果。此时主持人糖果用尽,无法再给编号为 #cf_span[2] 的海象分发糖果,过程结束。 在第三个样例中,主持人给编号为 #cf_span[1] 的海象 1 块糖果,给编号为 #cf_span[2] 的海象 2 块糖果,给编号为 #cf_span[3] 的海象 3 块糖果,然后再次给编号为 #cf_span[1] 的海象 1 块糖果。此时主持人还剩 1 块糖果,但无法再给编号为 #cf_span[2] 的海象分发 2 块糖果,因此主持人拿走最后一块糖果。 ## Input 第一行包含两个整数 #cf_span[n] 和 #cf_span[m] (#cf_span[1 ≤ n ≤ 50], #cf_span[1 ≤ m ≤ 104]),分别表示海象的数量和糖果的数量。 ## Output 请输出主持人最终剩下的糖果数量。 [samples] ## Note 在第一个样例中,主持人给编号为 #cf_span[1] 的海象 1 块糖果,给编号为 #cf_span[2] 的海象 2 块糖果,给编号为 #cf_span[3] 的海象 3 块糖果,给编号为 #cf_span[4] 的海象 4 块糖果,然后再次给编号为 #cf_span[1] 的海象 1 块糖果。此时主持人糖果用尽,无法再给编号为 #cf_span[2] 的海象分发糖果,过程结束。 在第三个样例中,主持人给编号为 #cf_span[1] 的海象 1 块糖果,给编号为 #cf_span[2] 的海象 2 块糖果,给编号为 #cf_span[3] 的海象 3 块糖果,然后再次给编号为 #cf_span[1] 的海象 1 块糖果。此时主持人还剩 1 块糖果,但无法再给编号为 #cf_span[2] 的海象分发 2 块糖果,因此主持人拿走最后一块糖果。
Let $ n $ be the number of walruses, and $ m $ be the total number of chips. The presenter distributes chips in rounds, starting from walrus 1, proceeding clockwise, giving walrus $ i $ exactly $ i $ chips in the $ k $-th round if $ i $ is the $ i $-th walrus in the sequence $ 1, 2, \dots, n $, and repeating the sequence cyclically. Define the chip requirement for the $ t $-th distribution step (starting at $ t = 1 $) as: $$ c_t = ((t - 1) \bmod n) + 1 $$ Let $ s $ be the number of complete distribution steps performed before running out of chips. Then $ s $ is the largest integer such that: $$ \sum_{t=1}^{s} c_t \leq m $$ The total chips given out is: $$ \sum_{t=1}^{s} c_t $$ The number of chips the presenter ends up with is: $$ m - \sum_{t=1}^{s} c_t $$ Alternatively, define the cumulative chip requirement after $ k $ full cycles and $ r $ additional steps ($ 0 \leq r < n $) as: $$ \text{Total given} = k \cdot \sum_{i=1}^{n} i + \sum_{i=1}^{r} i = k \cdot \frac{n(n+1)}{2} + \frac{r(r+1)}{2} $$ Find the maximum $ k \geq 0 $ and $ 0 \leq r < n $ such that: $$ k \cdot \frac{n(n+1)}{2} + \frac{r(r+1)}{2} \leq m $$ Then the remaining chips are: $$ m - \left( k \cdot \frac{n(n+1)}{2} + \frac{r(r+1)}{2} \right) $$ **Objective:** Compute the above remainder.
Samples
Input #1
4 11
Output #1
0
Input #2
17 107
Output #2
2
Input #3
3 8
Output #3
1
API Response (JSON)
{
  "problem": {
    "name": "A. Chips",
    "description": {
      "content": "There are _n_ walruses sitting in a circle. All of them are numbered in the clockwise order: the walrus number 2 sits to the left of the walrus number 1, the walrus number 3 sits to the left of the wa",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF92A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There are _n_ walruses sitting in a circle. All of them are numbered in the clockwise order: the walrus number 2 sits to the left of the walrus number 1, the walrus number 3 sits to the left of the wa...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "有 #cf_span[n] 只海象围坐成一圈,它们按顺时针方向编号:编号为 #cf_span[2] 的海象坐在编号为 #cf_span[1] 的海象的左边,编号为 #cf_span[3] 的海象坐在编号为 #cf_span[2] 的海象的左边,...,编号为 #cf_span[1] 的海象坐在编号为 #cf_span[n] 的海象的左边。\n\n主持人有 #cf_span[m] 块糖果。主持人站在圆圈...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n $ be the number of walruses, and $ m $ be the total number of chips.\n\nThe presenter distributes chips in rounds, starting from walrus 1, proceeding clockwise, giving walrus $ i $ exactly $ i $...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments