E. Restore Array

Codeforces
IDCF1028E
Time2000ms
Memory256MB
Difficulty
constructive algorithms
English · Original
Chinese · Translation
Formal · Original
While discussing a proper problem A for a Codeforces Round, Kostya created a cyclic array of positive integers $a_1, a_2, \ldots, a_n$. Since the talk was long and not promising, Kostya created a new cyclic array $b_1, b_2, \ldots, b_{n}$ so that $b_i = (a_i \mod a_{i + 1})$, where we take $a_{n+1} = a_1$. Here $mod$ is the [modulo operation](https://en.wikipedia.org/wiki/Modulo_operation). When the talk became interesting, Kostya completely forgot how array $a$ had looked like. Suddenly, he thought that restoring array $a$ from array $b$ would be an interesting problem (unfortunately, not A). ## Input The first line contains a single integer $n$ ($2 \le n \le 140582$) — the length of the array $a$. The second line contains $n$ integers $b_1, b_2, \ldots, b_{n}$ ($0 \le b_i \le 187126$). ## Output If it is possible to restore some array $a$ of length $n$ so that $b_i = a_i \mod a_{(i \mod n) + 1}$ holds for all $i = 1, 2, \ldots, n$, print «_YES_» in the first line and the integers $a_1, a_2, \ldots, a_n$ in the second line. All $a_i$ should satisfy $1 \le a_i \le 10^{18}$. We can show that if an answer exists, then an answer with such constraint exists as well. It it impossible to restore any valid $a$, print «_NO_» in one line. You can print each letter in any case (upper or lower). [samples] ## Note In the first example: * $1 \mod 3 = 1$ * $3 \mod 5 = 3$ * $5 \mod 2 = 1$ * $2 \mod 1 = 0$
在讨论 Codeforces 比赛的一个合适的问题 A 时,Kostya 创建了一个正整数的循环数组 $a_1, a_2, dots.h, a_n$。由于讨论冗长且无望,Kostya 创建了一个新的循环数组 $b_1, b_2, dots.h, b_n$,使得 $b_i = (a_i \mod a_{i + 1})$,其中我们取 $a_{n + 1} = a_1$。这里 $\mod$ 是取模运算。当讨论变得有趣时,Kostya 完全忘记了数组 $a$ 原来的样子。突然,他想到从数组 $b$ 恢复数组 $a$ 将会是一个有趣的问题(可惜不是 A)。 第一行包含一个整数 $n$($2 \leq n \leq 140582$)——数组 $a$ 的长度。 第二行包含 $n$ 个整数 $b_1, b_2, dots.h, b_n$($0 \leq b_i \leq 187126$)。 如果存在某个长度为 $n$ 的数组 $a$,使得对所有 $i = 1, 2, dots.h, n$ 都满足 $b_i = a_i \mod a_{(i \mod n) + 1}$,则在第一行输出 «_YES_»,并在第二行输出整数 $a_1, a_2, dots.h, a_n$。所有 $a_i$ 应满足 $1 \leq a_i \leq 10^{18}$。我们可以证明,如果存在答案,则一定存在满足此约束的答案。 如果无法恢复任何有效的 $a$,则在一行中输出 «_NO_»。 你可以以任意大小写形式输出每个字母。 ## Input 第一行包含一个整数 $n$($2 \leq n \leq 140582$)——数组 $a$ 的长度。第二行包含 $n$ 个整数 $b_1, b_2, dots.h, b_n$($0 \leq b_i \leq 187126$)。 ## Output 如果存在某个长度为 $n$ 的数组 $a$,使得对所有 $i = 1, 2, dots.h, n$ 都满足 $b_i = a_i \mod a_{(i \mod n) + 1}$,则在第一行输出 «_YES_»,并在第二行输出整数 $a_1, a_2, dots.h, a_n$。所有 $a_i$ 应满足 $1 \leq a_i \leq 10^{18}$。我们可以证明,如果存在答案,则一定存在满足此约束的答案。如果无法恢复任何有效的 $a$,则在一行中输出 «_NO_»。你可以以任意大小写形式输出每个字母。 [samples] ## Note 在第一个例子中: $1 \mod 3 = 1$ $3 \mod 5 = 3$ $5 \mod 2 = 1$ $2 \mod 1 = 0$
**Definitions** Let $ n \in \mathbb{Z} $ with $ n \geq 2 $. Let $ B = (b_1, b_2, \dots, b_n) $ be a given sequence of integers with $ 0 \leq b_i \leq 187126 $. Let $ A = (a_1, a_2, \dots, a_n) $ be an unknown sequence of positive integers with $ 1 \leq a_i \leq 10^{18} $, satisfying the cyclic constraint: $$ b_i \equiv a_i \mod a_{i+1}, \quad \text{for all } i \in \{1, \dots, n\}, $$ where indices are taken modulo $ n $, i.e., $ a_{n+1} = a_1 $. **Constraints** 1. $ 2 \leq n \leq 140582 $ 2. $ 0 \leq b_i \leq 187126 $ for all $ i \in \{1, \dots, n\} $ 3. $ 1 \leq a_i \leq 10^{18} $ for all $ i \in \{1, \dots, n\} $ 4. For each $ i $, $ b_i = a_i \bmod a_{i+1} $, which implies: $$ a_i = q_i \cdot a_{i+1} + b_i \quad \text{for some integer } q_i \geq 0, \quad \text{and} \quad b_i < a_{i+1}. $$ **Objective** Determine whether there exists a sequence $ A = (a_1, \dots, a_n) $ satisfying the above constraints. - If yes, output "YES" followed by any such valid sequence $ A $. - If no, output "NO".
Samples
Input #1
4
1 3 1 0
Output #1
YES
1 3 5 2
Input #2
2
4 4
Output #2
NO
API Response (JSON)
{
  "problem": {
    "name": "E. Restore Array",
    "description": {
      "content": "While discussing a proper problem A for a Codeforces Round, Kostya created a cyclic array of positive integers $a_1, a_2, \\ldots, a_n$. Since the talk was long and not promising, Kostya created a new ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1028E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "While discussing a proper problem A for a Codeforces Round, Kostya created a cyclic array of positive integers $a_1, a_2, \\ldots, a_n$. Since the talk was long and not promising, Kostya created a new ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "在讨论 Codeforces 比赛的一个合适的问题 A 时,Kostya 创建了一个正整数的循环数组 $a_1, a_2, dots.h, a_n$。由于讨论冗长且无望,Kostya 创建了一个新的循环数组 $b_1, b_2, dots.h, b_n$,使得 $b_i = (a_i \\mod a_{i + 1})$,其中我们取 $a_{n + 1} = a_1$。这里 $\\mod$ 是取模运算。...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ with $ n \\geq 2 $.  \nLet $ B = (b_1, b_2, \\dots, b_n) $ be a given sequence of integers with $ 0 \\leq b_i \\leq 187126 $.  \nLet $ A = (a_1, a_2, \\dots, a_n) $...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments