[BZOJ 1081] [SCOI2005] 超级格雷码 【找规律】
2015-01-31 15:56
330 查看
题目链接:BZOJ - 1081
备注:此题BZOJ上貌似没有 spj ,要把一般顺序的每个格雷码倒着输出...比如 0102 输出为 2010
生成方法:以 3 位 3 进制为例
0 0 0
0 0 1
0 0 2
0 1 2 //中位写 1 ,后面镜像复制
0 1 1
0 1 0
0 2 0 //中位写 2,后面镜像复制
0 2 1
0 2 2
1 2 2 //高位写 1 ,后面镜像复制
1 2 1
........
备注:此题BZOJ上貌似没有 spj ,要把一般顺序的每个格雷码倒着输出...比如 0102 输出为 2010
题目分析
就是按照 Gray 码的生成方法写前几个出来找找规律就好了。生成方法:以 3 位 3 进制为例
0 0 0
0 0 1
0 0 2
0 1 2 //中位写 1 ,后面镜像复制
0 1 1
0 1 0
0 2 0 //中位写 2,后面镜像复制
0 2 1
0 2 2
1 2 2 //高位写 1 ,后面镜像复制
1 2 1
........
代码
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; const int MaxN = 20 + 5; int n, m, Tot; int Pow[MaxN]; void PrintCh(int x) { if (x < 10) printf("%d", x); else printf("%c", 'A' + x - 10); } int main() { scanf("%d%d", &n, &m); Pow[0] = 1; for (int i = 1; i <= n; ++i) Pow[i] = Pow[i - 1] * m; Tot = Pow ; int x, y; for (int i = 0; i < Tot; ++i) { for (int j = n - 1; j >= 0; --j) { x = i / Pow[n - j]; y = (i / Pow[n - j - 1]) % m; if (x & 1) PrintCh(m - y - 1); else PrintCh(y); } printf("\n"); } return 0; }
相关文章推荐
- BZOJ 1081: [SCOI2005]超级格雷码 神他妈规律,DFS
- 【bzoj1081】[SCOI2005]超级格雷码
- 【bzoj1081】[SCOI2005]超级格雷码
- [BZOJ1081][SCOI2005]超级格雷码
- bzoj1081[SCOI2005]超级格雷码
- 【bzoj1075】【scoi2005】【超级格雷码】【找规律】
- bzoj1081: [SCOI2005]超级格雷码
- bzoj1081: [SCOI2005]超级格雷码
- BZOJ1081: [SCOI2005]超级格雷码
- [BZOJ1081][SCOI2005]超级格雷码(深搜)
- bzoj1081: [SCOI2005]超级格雷码
- [bzoj 1081--SCOI2005]超级格雷码
- bzoj1081 [SCOI2005]超级格雷码
- 【bzoj1081】[SCOI2005]超级格雷码
- [bzoj1081][找规律][dfs]超级格雷码
- BZOJ1081(SCOI2005)[超级格雷码]--找规律+暴搜
- 【BZOJ】【P1081】【SCOI2005】【超级格雷码】【题解】【乱搞】
- BZOJ1081 超级格雷码 [找规律]
- 1081: [SCOI2005]超级格雷码
- BZOJ 1088: [SCOI2005]扫雷Mine 模拟,规律,脑洞