{"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 to find the next greater number that consists of distinct digits (no digit is repeated twice).\n\nIt is *guaranteed* that there is an answer for the given number.\n\nThe 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.\n\nThe 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. \n\nOutput a single line with the next greater number that consists of distinct digits. Separate the digits by a single space.\n\n## Input\n\nThe 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. \n\n## Output\n\nOutput a single line with the next greater number that consists of distinct digits. Separate the digits by a single space.\n\n[samples]","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, 1, \\dots, b-1\\} $, $ a_1 \\neq 0 $, representing a number in base $ b $.\n\n**Constraints**  \n1. All digits in $ A $ may not be distinct initially.  \n2. The number has no leading zeros: $ a_1 \\neq 0 $.  \n3. There exists at least one number greater than $ A $ in base $ b $ with exactly $ n $ digits and all distinct digits.\n\n**Objective**  \nFind the lexicographically smallest sequence $ B = (b_1, b_2, \\dots, b_n) $ such that:  \n- $ B > A $ (as base-$ b $ numbers),  \n- $ b_i \\in \\{0, 1, \\dots, b-1\\} $ for all $ i $,  \n- $ b_1 \\neq 0 $,  \n- All digits in $ B $ are distinct: $ b_i \\neq b_j $ for $ i \\neq j $.  \n\nOutput $ B $ as $ b_1, b_2, \\dots, b_n $ separated by spaces.","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF10219G","tags":[],"sample_group":[],"created_at":"2026-03-03 11:00:39"}}