#P2328. [SCOI2005] 超级格雷码

    ID: 3109 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2005四川各省省选Special Judge枚举进制

[SCOI2005] 超级格雷码

Problem Description

The well-known Gray code refers to a permutation of 2n2^n distinct nn-bit binary numbers (i.e., 02n10 \sim 2^n - 1, with leading zeros added if necessary to ensure nn bits). This permutation satisfies the condition that any two adjacent binary numbers differ by at most one bit (e.g., 003003 and 001001 differ by one bit, while 003003 and 030030 differ by two bits, which does not meet the requirement). For instance, when n=2n=2, the sequence (0000, 0101, 1111, 1010) is a valid Gray code. The so-called super Gray code refers to the arrangement of BB distinct nn-digit BB-ary numbers that satisfies the above conditions. Given nn and BB, find a Gray code that meets the conditions. For numbers greater than 99, use AZA\sim Z to represent them (103510\sim 35).

Input Format

Only one line, containing two integers nn and BB.

Output Format

A total of BnB^n lines, each line is a base-BB number, representing the ordering that meets the required conditions.

2  2
00
01
11
10

Hint

2B361Bn655352\leq B\leq 36,1\leq B^n\leq 65535

Thanks to @Night_Aurora for the SPJ.

Translated by ChatGPT 5