Ouroboros Snake
Ouroboros is a mythical snake from ancient Egypt. It has its tail in its mouth and continously devours itself. The Ouroboros numbers are binary numbers of 2^n bits that have the property of "generating" the whole set of numbers from 0 to 2^n - 1. The generation works as follows: given an Ouroboros number, we place its 2^n bits wrapped in a circle. Then, we can take 2^n groups of n bits starting each time with the next bit in the circle. Such circles are called Ouroboros circles for the number n. We will work only with the smallest
Ouroboros number for each n.
Example: for n = 2, there are only four Ouroboros numbers. These are 0011;0110;1100; and 1001. In this case, the smallest one is 0011. Here is the Ouroboros circle for 0011:
The table describes the function o(n;k) which calculates the k-th number in the Ouroboros circle of the smallest Ouroboros number of size n. This function is what your program should compute.
Input
The input consists of several test cases. For each test case, there will be a line containing two integers n and k (1<=n<=15; 0<=k<2^n). The end of the input file is indicated by a line containing two zeros. Don抰 process that line.
Output
For each test case, output o(n;k) on a line by itself.
Sample Input
2 0
2 1
2 2
2 3
0 0
Sample Output
0
1
3
2
Source
Mid-Central European Regional Contest 2000
题目类型:欧拉回路
算法分析:这是经典的旋转鼓轮问题,构造出序列后直接从第k位二进制开始构造n位十进制即可
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
/************************************************* Author :supermaker Created Time :2016/3/18 14:05:43 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; struct Node { int v, w, nxt; }; Node edge[maxn]; int head[maxn], edgelen; void Init () { memset (head, -1, sizeof (head)); memset (edge, -1, sizeof (edge)); edgelen = 0; } void AddEdge (int u, int v, int w) { edge[edgelen].v = v, edge[edgelen].w = w; edge[edgelen].nxt = head[u]; head[u] = edgelen++; } bool vis[maxn]; int out[maxn], len, dfstack[maxn], slen, tout[maxn]; void SDfs (int val) { len = slen = 0, dfstack[slen++] = val; memset (vis, false, sizeof (vis)); while (slen > 0) { int tt = dfstack[--slen]; bool is_find = false; for (int i = head[tt]; i != -1; i = edge[i].nxt) if (!vis[edge[i].w]) { vis[edge[i].w] = true; dfstack[slen++] = tt; dfstack[slen++] = edge[i].v; is_find = true; break; } if (!is_find) out[len++] = tt; } } int main() { //CFF; //CPPFF; int n, k; while (scanf ("%d%d", &n, &k)) { if (n == 0 && k == 0) break; if (n == 1) { if (k == 0) puts ("0"); else puts ("1"); continue; } int nodenum = 1; for (int i = 1; i <= n - 1; i++) nodenum *= 2; Init (); for (int i = 0; i <= nodenum - 1; i++) { int tt = i % (nodenum / 2); for (int j = 1; j >= 0; j--) AddEdge (i, tt * 2 + j, i * 2 + j); } SDfs (0); for (int i = 1; i <= n - 2; i++) out[len++] = 0; for (int i = len - 1, j = 1; i >= 0; i--, j++) tout[j] = out[i] % 2; int res = 0; for (int i = k + 1, cnt = 1; cnt <= n; i++, cnt++) res = res * 2 + tout[i]; printf ("%d\n", res); } return 0; } |
- « 上一篇:poj1780
- zoj2770:下一篇 »