標籤:des style blog http os io for 2014
Description
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 02 12 22 30 0
Sample Output
0132
題意:讓你找個字典序最小的序列使得排成環旋轉後取n個,最後可以取到[0-2^n)的所有數。
思路:歐拉迴路的應用,首先為了找最短,所有我們希望這個數的後n-1位和下一個數的前n-1位是相同的,只有他們的頭和尾不一樣,所以我們有每添加一位就可以構成一個新的數,那麼這兩個數就能通過這位來串連,我們可以先枚舉出所有的節點,然後對應產生邊,我們最後要跑完所有的邊且回到原點,而這就是歐拉迴路了
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <vector>using namespace std;const int maxn = 1<<16;struct Edge {int v, via;int vis;}; vector<Edge> ve[maxn];int path[maxn<<1];int n, k, cnt;void init() {for (int i = 0; i < maxn; i++)ve[i].clear();}void dfs(int cur) {for (int i = 0; i < ve[cur].size(); i++)if (!ve[cur][i].vis) {ve[cur][i].vis = 1;dfs(ve[cur][i].v);path[++cnt] = ve[cur][i].via + '0';}return;}int main() {while (scanf("%d%d", &n, &k) != EOF && n+k) {int len = n;n = (1<<(n-1)) - 1;init();Edge tmp;for (int i = 0; i <= n; i++) {int t = (i<<1) - ((i&(1<<(len-2)))<<1);tmp.v = t;tmp.via = 0;tmp.vis = 0;ve[i].push_back(tmp);t += 1;tmp.v = t;tmp.via = 1;tmp.vis = 0;ve[i].push_back(tmp);}cnt = -1;dfs(n);path[++cnt] = '\0';reverse(path, path+cnt);int ans = 0;for (int i = k, j = 1; j <= len; j++, i++) ans = (ans<<1) + (path[i%cnt]-'0');printf("%d\n", ans);}return 0;}