poj 1392 Ouroboros Snake
2016-02-01 12:04
337 查看
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1120 | Accepted: 587 |
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
题意:让你找个字典序最小的序列使得排成环旋转后取n个,最后可以取到[0-2^n)的所有数。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int path[2<<16],h,n,k,vis[2<<16],ans[2<<16];
void dfs(int x)
{
int t1=(x<<1)&((1<<n)-1),t2=t1+1;//t1为x去掉最高位,末尾加0;t2为x去掉最高位,末尾加1;
if(!vis[t1])//注意2个if的先后顺序。
{
vis[t1]=1;
dfs(t1);
path[h++]=0;//记录字典序最小的01序列,
}
if(!vis[t2])
{
vis[t2]=1;
dfs(t2);
path[h++]=1;
}
}
int main()
{
int i,j,sum;
while(~scanf("%d %d",&n,&k))
{
if(n==0&&k==0)break;
h=0;
for(i=0;i<n-1;i++)//进行初始化
path[h++]=0,ans[i]=0;
memset(vis,0,sizeof(vis));
dfs(0);
j=n-1;
for(i=h-1;i>=n-1;i--)//注意倒序一下。
ans[j++]=path[i];
sum=0;
for(i=k;i<k+n;i++)
sum=sum*2+ans[i];
cout<<sum<<endl;
}
return 0;
}
相关文章推荐
- Window Phone
- MYSQL设计优化
- Mac操作系统如何添加启动项
- 将数组,表的某一列转换为string字符串的方法
- javascript分页
- Objective-C简介
- Camera.Parameters 参数
- windows下删除所有 .svn文件及文件夹
- HTML5+CSS3 权威指南读书笔记
- 安卓开发——相机:拍照并处理图片
- 华为机试题:向升序单向链表中插入一个节点
- BLE-NRF51822教程10-手机动态修改设备名
- js 验证码 倒计时60秒
- 安卓开发——存储与加载本地文件
- [Spring实战系列](4)Spring IOC容器
- Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id)等
- BLE-NRF51822教程9—动态密码(配对码)
- Android获取到wifi的信息
- linux内核基础(系统调用,简明)
- hdu 1016 Prime Ring Problem(深度优先搜索)