1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//http://www.compuphase.com/hilbert.htm
#include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define pb push_back
#define mp make_pair
#define boost ios_base::sync_with_stdio(false)
#define eb emplace_back
#define OUT(x) {cout << x; exit(0); }
#define scanf(...) scanf(__VA_ARGS__)?:0
#define FOR(i, a, b) for(int i=(a); i<=(b); ++i)
typedef long long ll;
typedef unsigned int ull;
typedef pair <int, int> PII;
typedef pair <PII, int> PPI;
typedef pair <ll, int> PLI;
typedef pair <ll, ll> PLL;
typedef pair <PII, PII> PP;
typedef unsigned int ui;
const int inf = 1e9+9;
const int mod = 1e9+7;
const ll MOD = 1e9+696969;
const ll INF = 1e18;
#define UP 0
#define RIGHT 1
#define DOWN 2
#define LEFT 3
bitset <33000> tab[33000]; int DL;
PII moves[5], skosy[5], pos;
PII operator +(PII &a, PII &b) {
	return mp(a.first + b.first, a.second + b.second);
}
inline void move(int kier) {
	///cout << pos.e1 << ' ' << pos.e2 << endl;
	pos = pos + moves[kier];
	tab[pos.e1].set(pos.e2, 1);
	///cout << pos.e1 << ' ' << pos.e2 << endl;
	pos = pos + moves[kier];
	tab[pos.e1].set(pos.e2, 1);
}

PII position = {1, 1};
void hilbert_level(int level,int direction=UP)
{
  if (level==1) {
    switch (direction) {
    case LEFT:
      move(RIGHT);      /* move() could draw a line in... */
      move(DOWN);       /* ...the indicated direction */
      move(LEFT);
      break;
    case RIGHT:
      move(LEFT);
      move(UP);
      move(RIGHT);
      break;
    case UP:
      move(DOWN);
      move(RIGHT);
      move(UP);
      break;
    case DOWN:
      move(UP);
      move(LEFT);
      move(DOWN);
      break;
    } /* switch */
  } else {
    switch (direction) {
    case LEFT:
      hilbert_level(level-1,UP);
      move(RIGHT);
      hilbert_level(level-1,LEFT);
      move(DOWN);
      hilbert_level(level-1,LEFT);
      move(LEFT);
      hilbert_level(level-1,DOWN);
      break;
    case RIGHT:
      hilbert_level(level-1,DOWN);
      move(LEFT);
      hilbert_level(level-1,RIGHT);
      move(UP);
      hilbert_level(level-1,RIGHT);
      move(RIGHT);
      hilbert_level(level-1,UP);
      break;
    case UP:
      hilbert_level(level-1,LEFT);
      move(DOWN);
      hilbert_level(level-1,UP);
      move(RIGHT);
      hilbert_level(level-1,UP);
      move(UP);
      hilbert_level(level-1,RIGHT);
      break;
    case DOWN:
      hilbert_level(level-1,RIGHT);
      move(UP);
      hilbert_level(level-1,DOWN);
      move(LEFT);
      hilbert_level(level-1,DOWN);
      move(DOWN);
      hilbert_level(level-1,LEFT);
      break;
    } /* switch */
  } /* if */
}
int n, zap, t, dir, pot;

void ruszaj() {
	pos = pos + skosy[dir];
	if (tab[pos.e1].test(pos.e2)) {
		//czy to pionowa sciana -> sprawdzamy igreki
		bool pion = 1;
		if (pos.e2 > 0 && pos.e2 < pot && tab[pos.e1].test(pos.e2 + 1) && tab[pos.e1].test(pos.e2 - 1));
		else pion = 0;
		if (!pion) {
			if (dir == 0) dir = 1;
			else if (dir == 1) dir = 0;
			else if (dir == 2) dir = 3;
			else if (dir == 3) dir = 2;
		}
		else {
			if (dir == 0) dir = 3;
			else if (dir == 3) dir = 0;
			else if (dir == 2) dir = 1;
			else if (dir == 1) dir = 2;
		}
	}
}
int main() {
	moves[0] = {0, -1}; moves[2] = {0, 1};
	moves[1] = {1, 0}; moves[3] = {-1, 0};
	skosy[0] = {1, 1}; skosy[1] = {1, -1};
	skosy[2] = {-1, -1}; skosy[3] = {-1, 1};
	boost;
	cin >> n >> zap;
	pot = (1 << (n + 1));
	pos = {1, 1}; tab[1].set(1, 1);
	hilbert_level(n);
	
	FOR(i, 0, pot) tab[i].set(0, 1), tab[i].set(pot, 1);
	FOR(i, 0, pot) tab[0].set(i, 1), tab[pot].set(i, 1);
	/*for (int j=pot; j>=0; --j){
		FOR(i, 0, pot) printf("%4d ", tab[i].test(j));
		cout << endl;
	}
	*/
	pos = {1, 0};
	int RightNow = 0; dir = 0;
	//dir parzyste -> skos y = x. zero -> lewy górny róg, dalej zgodnie z zegarem
	FOR(qq, 1, zap) {
		cin >> t;
		while (RightNow < t) ruszaj(), ++RightNow;
		cout << pos.e1 << ' ' << pos.e2 << "\n";
	}
}