Ouroboros Snake
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 889 |
|
Accepted: 428 |
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
其實,000到001,其實00是不變的,可以抽象成點,而000,001都是只有一個,可以抽象成邊,那麼這題就可以做出來了,以0——2^(n-1)-1為編號建立一棵樹,共2^(n-1)個節點,如果在某個節點後面添加一個0或者1,再去掉最高位,得到下一個節點,兩節點之間連一條有向邊。圖中每條邊就表示了一個數,共有2^n個數,各不相同!題目要求字典序最小,則從2^(n-1-1節點開始,並且每個節點先連加0的邊,後連加1的邊,這樣的話,就能保證最終的字典序最小。還是歐拉迴路,都是一個模型,演算法也就那個一個。只是這裡,為什麼,我模數了,提交OJ反而會快10幾Ms,這一點,我不明白的!
#include <iostream>#include<stdio.h>#include<vector>using namespace std;struct tree{ int v; int id; int to; int flag;};int step;vector<tree > p[35000];bool re[35000];int path[35000];void dfs(int u){ for(int i=0;i<p[u].size();i++) { if(!re[p[u][i].id]) { re[p[u][i].id]=true; dfs(p[u][i].to); path[step++]=p[u][i].flag; } }}int main(){ int i,k,n,tt,m; tree temp; while(~scanf("%d%d",&k,&m)) { if(k==0&&m==0) break; n=1<<(k-1); // printf("%d n\n",n); for(i=0;i<=n;i++) p[i].clear(); for(i=0;i<n;i++) { tt=((i<<1)-((i&(1<<(k-2)))<<1)); // printf("%d tt\n",tt);+0後得到的 //tt=tt%n; temp.to=tt; temp.id=i<<1; temp.v=i; temp.flag=0; p[i].push_back(temp); re[temp.id]=false; tt+=1; //printf("%d tt\n",tt); //tt=tt%n;+1後得到的 temp.v=i; temp.to=tt; temp.id=i<<1|1; temp.flag=1; re[temp.id]=false; p[i].push_back(temp); } step=0; dfs(n-1); int sum,j; sum=0; for(i=step-m-1,j=0;j<k;j++,i--) { sum=sum*2+path[i]; } printf("%d\n",sum); } return 0;}