poj1392 解題報告

來源:互聯網
上載者:User
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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.