G. Next Number

Codeforces
IDCF10219G
Time2000ms
Memory256MB
Difficulty
English · Original
Formal · Original
You are given a number with n digits written in base b. For example, our monetary system is written in base 10 (i.e. 926 JOD) and a binary number is written in base 2 (i.e. 10101110). Your task is to find the next greater number that consists of distinct digits (no digit is repeated twice). It is *guaranteed* that there is an answer for the given number. The first line of input contains two integers n and b (1 ≤ n ≤ 3 × 105) (2 ≤ b ≤ 3 × 105), the number of digits of the number and the base it is written in. The second line of input contains n integers ai (0 ≤ ai < b), where ai is the ith digit in the number. It is *guaranteed* that the number has no leading zeros. Output a single line with the next greater number that consists of distinct digits. Separate the digits by a single space. ## Input The first line of input contains two integers n and b (1 ≤ n ≤ 3 × 105) (2 ≤ b ≤ 3 × 105), the number of digits of the number and the base it is written in.The second line of input contains n integers ai (0 ≤ ai < b), where ai is the ith digit in the number. It is *guaranteed* that the number has no leading zeros. ## Output Output a single line with the next greater number that consists of distinct digits. Separate the digits by a single space. [samples]
**Definitions** Let $ n, b \in \mathbb{Z} $ with $ 1 \leq n \leq 3 \times 10^5 $ and $ 2 \leq b \leq 3 \times 10^5 $. Let $ A = (a_1, a_2, \dots, a_n) $ be a sequence of digits with $ a_i \in \{0, 1, \dots, b-1\} $, $ a_1 \neq 0 $, representing a number in base $ b $. **Constraints** 1. All digits in $ A $ may not be distinct initially. 2. The number has no leading zeros: $ a_1 \neq 0 $. 3. There exists at least one number greater than $ A $ in base $ b $ with exactly $ n $ digits and all distinct digits. **Objective** Find the lexicographically smallest sequence $ B = (b_1, b_2, \dots, b_n) $ such that: - $ B > A $ (as base-$ b $ numbers), - $ b_i \in \{0, 1, \dots, b-1\} $ for all $ i $, - $ b_1 \neq 0 $, - All digits in $ B $ are distinct: $ b_i \neq b_j $ for $ i \neq j $. Output $ B $ as $ b_1, b_2, \dots, b_n $ separated by spaces.
API Response (JSON)
{
  "problem": {
    "name": "G. Next Number",
    "description": {
      "content": "You are given a number with n digits written in base b. For example, our monetary system is written in base 10 (i.e. 926 JOD) and a binary number is written in base 2 (i.e. 10101110).  Your task is t",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10219G"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a number with n digits written in base b. For example, our monetary system is written in base 10 (i.e. 926 JOD) and a binary number is written in base 2 (i.e. 10101110). \n\nYour task is t...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, b \\in \\mathbb{Z} $ with $ 1 \\leq n \\leq 3 \\times 10^5 $ and $ 2 \\leq b \\leq 3 \\times 10^5 $.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of digits with $ a_i \\in \\{0,...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments