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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include<bits/stdc++.h>
#define PII pair<int,int>
#define f first
#define s second
#define VI vector<int>
#define LL long long
#define MP make_pair
#define LD long double
#define PB push_back
#define ALL(V) V.begin(),V.end()
#define abs(x) max((x),-(x))
#define PDD pair<LD,LD> 
#define VPII vector< PII > 
#define siz(V) ((int)V.size())
#define FOR(x, b, e)  for(int x=b;x<=(e);x++)
#define FORD(x, b, e) for(int x=b;x>=(e);x--)
#define REP(x, n)     for(int x=0;x<(n);x++)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
using namespace std;
const int MXD = 15;
const int MXN = 1<<13;
int memo[MXN+1][MXN+1];

int MEMO = 13;

namespace GE
  {
  int x = -1, y = 0;
  int i = 0; 
  void step(int dir) {
     switch(dir & 3) {
        case 0: x = x + 1; break; 
        case 1: y = y + 1; break; 
        case 2: x = x - 1; break; 
        case 3: y = y - 1; break; 
      } 
//      cerr<<x<<" "<<y<<endl;
      memo[x][y] = i;
     i = i + 1;
  } 

  void hilbert(int dir, int rot, int order) {
     if (order == 0) return; 
   
     dir += rot; 
     hilbert(dir, -rot, order - 1); 
     step(dir); 
     dir -= rot; 
     hilbert(dir, rot, order - 1); 
     step(dir); 
     hilbert(dir, rot, order - 1); 
     dir -= rot; 
     step(dir); 
     hilbert(dir, -rot, order - 1); 
  } 

  void generate(int order)
    {
    x = -1;
    y = 0;
    i = 0;
    step(0); 
    hilbert(0, 1, order); 
    }
  }
namespace brute
  {
  inline int nagranicy(int n, int i, int j)
    {
    if(i == 0 || i == (1<<(n+1)))return 2;
    if(j == 0 || j == (1<<(n+1)))return 1;
    return 0;
    }
  int nakrzywej(int n, int i, int j)
    {
    if(i % 2 == 0 && i > 0)
      {
      i>>=1;
      j>>=1;
      int a = memo[i][j];
      int b = memo[i-1][j];
      if(abs(a - b) == 1)return 1;
      }
    else if(j % 2 == 0 && j > 0)
      {
      i>>=1;
      j>>=1;
      int a = memo[i][j];
      int b = memo[i][j-1];
      if(abs(a - b) == 1)return 2;
      }
    return 0;
    }
    
  void uderz_poziomo(PII &k)
    {
    k.s *= -1;
    }
  void uderz_pionowo(PII &k)
    {
    k.f *= -1;
    }

  void go(int n, int &a, int &b, PII &k)
    {
    a += k.f;
    b += k.s;
    int nag = nagranicy(n, a, b);
    int nak = nakrzywej(n, a, b);
    if(nag == 1 || nak == 1)
      {
      uderz_poziomo(k);
      return;
      }
    else if(nag == 2 || nak == 2)
      {
      uderz_pionowo(k);
      return;
      }
    }
    
  void brute(int n, int q)
    {
    PII k = MP(1, 1);
    int a = 1;
    int b = 0;
    int idx = 0;
    GE::generate(n);

    REP(i, q)
      {
      LL x;
      scanf("%lld", &x);
      while(idx < x)
        {
//        cerr<<a<<" "<<b<<endl;
        go(n, a, b, k);
        idx++;
        }
      printf("%d %d\n", a, b);           
      }
    exit(0);
    }
  }

// *********************       BRUTE END          ************************

void rot(int n, int &x, int &y, int rx, int ry) {
    if (ry == 0) {
        if (rx == 1) {
            x = 2 * n-1 - x;
            y = 2 * n-1 - y;
        }

        //Swap x and y
        swap(x, y);
    }
}

//int xy2d(int n, int x, int y

inline int xy2d (int n, int x, int y) {
   if(n == -1)return 0;

    if(n == MEMO)return memo[x][y];

    int cpx = x;
    int cpy = y;
//    bool k = (n < MXD && x < MXN && y < MXN);

//    if(k)
//      if(memo[x][y][n])return memo[x][y][n];

    int rx, ry;
    int s = (1<<(n-1));
    
    rx = (x & s) > 0;
    ry = (y & s) > 0;
    int d = (s*s) * ((3 * rx) ^ ry);
    rot(s, x, y, rx, ry);

    x&=s-1;
    y&=s-1;

    int res = d + xy2d(n-1, x, y);
//    if(k)
//      memo[cpx][cpy][n] = res;
    return res;

}
/*
void prawo(int n, int &x, int &y)
  {
  swap(x, y);
  }
void lewo(int n, int &x, int &y)
  {
  x = n - 1 - x;
  y = n - 1 - y;
  swap(x, y);
  }
int myx2d(int n, int x, int y)
  {
  int s = (1<<(n-1));
  if(x < s)
    {
    if(y < s)
      {
      prawo(n, x, y);
      return s * s + myx2d(n-1, x, y);
      }
    else
      {
      
      }
    }
  
  }*/

//convert d to (x,y)
void d2xy(int n, int d, int *x, int *y) {
    int rx, ry, s, t=d;
    *x = *y = 0;
    for (s=1; s<n; s*=2) {
        rx = 1 & (t/2);
        ry = 1 & (t ^ rx);
        rot(s, *x, *y, rx, ry);
        *x += s * rx;
        *y += s * ry;
        t /= 4;
    }
}

int tod(int n, int i, int j)
  {
  //n = 1<<n;
  i/=2;
  j/=2;
  
  return xy2d(n, i, j);
  }
int toxy(int n, int d, int &i, int &j)
  {
  n = 1<<n;
  d2xy(n, d, &i, &j);
  i = i * 2 + 1;
  j = j * 2 + 1;
  }
  
int nagranicy(int n, int i, int j)
  {
  if(i == 0 || i == (1<<(n+1)))return 2;
  if(j == 0 || j == (1<<(n+1)))return 1;
  return 0;
  }
int k = 7;
int nakrzywej(int n, int i, int j) // bez granicy
  {
  if(i % 2 == 0 && j % 2 == 0)return 0;
  if(i % 2 == 0 && i > 0 && j >= 0)
    {
    int a = tod(n, i+1, j);
    int b = tod(n, i-1, j);
    //    cerr<<"   "<<i<<" "<<j<<" "<<a<<" "<<b<<endl;
    if(abs(a - b) == 1)return 1;
    
    }
  if(j % 2 == 0 && j > 0 && i >= 0)
    {
    int a = tod(n, i, j+1);
    int b = tod(n, i, j-1);
//    cerr<<"   "<<i<<" "<<j<<" "<<a<<" "<<b<<endl;
    if(abs(a - b) == 1)return 2;
    
    }
  return 0;
  }
/*
int main2()
  {
  printf("%d\n", xy2d(3, 1, 1));
  FOR(i, 0, 4)
    FOR(j, 0, 4)
      {
      cout<<i<<" "<<j<<" "<<tod(3, i, j)<<endl;
      }

    FOR(j, 0, 16)
      {
      int x = 0;
      int y = 0;
      toxy(3, j, x, y);
      cout<<j<<" "<<x<<" "<<y<<endl;
      }
  }*/
void uderz_poziomo(PII &k)
  {
  k.s *= -1;
  }
void uderz_pionowo(PII &k)
  {
  k.f *= -1;
  }
int x = 0;
void go(int n, int &a, int &b, PII &k)
  {
//  while(1)
    {
    
    a += k.f;
    b += k.s;
//    cerr<<a<<" "<<b<<" "<<k.f<<" "<<k.s<<endl;
    
    int nag = nagranicy(n, a, b);
    
//    cerr<<"gra: "<<nag<<" krz: "<<nak<<endl;
    if(nag == 1)
      {
      uderz_poziomo(k);
      return;
      }
    if(nag == 2)
      {
      uderz_pionowo(k);
      return;
      }
    int nak = nakrzywej(n, a, b);
    if(nak == 1)
      {
      uderz_poziomo(k);
      return;
      }
    if(nak == 2)
      {
      uderz_pionowo(k);
      return;
      }
    }
  }

int main()
  {
  int n, q;
  int a = 1;
  int b = 0;
  PII k = MP(1, 1);
  int idx = 0;
  
  scanf("%d%d", &n, &q);
  
  if(n <= MEMO)
    {
    brute::brute(n, q);
    }
  GE::generate(MEMO);
  REP(i, q)
    {
    LL x;
    scanf("%lld", &x);
    while(idx < x)
      {
//      cerr<<a<<" "<<b<<endl;
      go(n, a, b, k);
      idx++;
      }
    printf("%d %d\n", a, b);
    
    
    
    }/*
  REP(i, 1000)
    {
    cerr<<"                         "<<i<<" "<<a<<" "<<b<<endl;
    go(n, a, b, k);
  
    }*/
  }