题目传送门
这个构造太一眼了。差不多就是类似这样一直排下去。
1 2 3 4 5 6
| 000 001 002 003 013 012 011 010 020 021 022 023 033 032 031 030 130 131 132 133 ...
|
这个直接递归构造就可以。但是 AI 写得太好看了所以还是要放个 AI 写的代码。
复杂度瓶颈在输出,时间复杂度 O(nbn)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <iostream> #include <vector> using namespace std; char ord(int x) { if(x>=0&&x<=9) return (char)(x+'0'); return (char)('A'+x-10); } int b, n; char ch[100005];
void dfs(int depth, bool reverse) { if (depth == n) { for (int i=0;i<n;i++) cout << ch[i]; cout << "\n"; return; }
int start = reverse ? b - 1 : 0; int end = reverse ? -1 : b; int step = reverse ? -1 : 1;
for (int i = start; i != end; i += step) { ch[depth] = ord(i); bool next_reverse = (i % 2 != 0) ^ reverse; dfs(depth + 1, next_reverse); } }
int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> b; dfs(0, 0);
return 0; }
|