zoj 1130 poj 1392 Ouroboros Snake (欧拉路径)
2014-11-28 19:01
363 查看
Ouroboros Snake
Time Limit: 10 Seconds Memory Limit: 32768 KB
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��t 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
求一个环形的二进制字符串 可以组成0-2^n-1当中所有的数
欧拉路径的应用
Time Limit: 10 Seconds Memory Limit: 32768 KB
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��t 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
求一个环形的二进制字符串 可以组成0-2^n-1当中所有的数
欧拉路径的应用
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <string.h> #include <string> #define eps 1e-8 #define op operator #define MOD 10009 #define MAXN 100100 #define INF 0x7fffffff #define MEM(a,x) memset(a,x,sizeof a) #define ll __int64 const int M=(int)(1<<16); using namespace std; int list[M]; int ans[M]; int len; int m; void dfs(int v) { int w; // cout<<"vvv "<<v<<" list "<<list[v]<<endl; while(list[v]<2) { w=v*2+list[v]; list[v]++; // cout<<"wwww "<<w<<endl; // cout<<"w&m "<<(w&m)<<endl; dfs(w&m); // cout<<"len "<<len<<" ww "<<w<<endl; ans[len++]=w; } } int main() { //freopen("ceshi.txt","r",stdin); int n,k; while(scanf("%d%d",&n,&k)!=EOF) { if(n==0&&k==0) break; MEM(list,0); len=0; m=(1<<(n-1))-1; dfs(0); while(k--) len--; printf("%d\n",ans[--len]); } return 0; }
相关文章推荐
- zoj 1919 poj 2337 Catenyms(欧拉路径求解)
- 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)
- POJ 1392 Ouroboros Snake(数位欧拉:输出路径)
- POJ 2337 && ZOJ 1919--Catenyms 【有向图 && 欧拉路判断 && 欧拉路径】
- 打印欧拉路径问题 hihoCoder - 1182 POJ - 1392 POJ 1780 POJ - 2237
- POJ 2337 - Catenyms 按所需的字典序输出欧拉路径..
- POJ-1392-Ouroboros Snake
- POJ 2230 Watchcow 欧拉路径 DFS
- poj 1392 Ouroboros Snake
- 欧拉回路 poj-1392 Ouroboros Snake
- poj 1122 ZOJ 1053 输出路径的地杰斯特拉
- POJ 1392 - Ouroboros Snake 求0011这类序列欧拉回路的最小路径...
- ZOJ 2103 Marco Popo the Traveler(无向图欧拉路径)
- poj 1386 欧拉路径
- POJ 1386 Play on Words (有向图欧拉路径判定)
- POJ 1135/ZOJ 1298 Domino Effect (最短路径dijkstra)
- poj 2337 Catenyms(欧拉路径的最小字典序)
- poj 1386 Play on Words(欧拉路径存在的判断问题)
- POJ-1780 Code 欧拉路径
- POJ 2337 输出欧拉路径