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
#include <iostream>
#include <iomanip>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

namespace {

enum class Wall {
  none, x, y,
};

Wall wall(int n, int x, int y)
{
  int s = 1 << n;
  if (x == 0 || x == 2*s) return Wall::x;
  if (y == 0 || y == 2*s) return Wall::y;

  Wall wx = Wall::x;
  Wall wy = Wall::y;

  while (n > 0) {
    if ((x == 1 || x == 2*s-1) && y == s) return wx;
    if (x == s && y == s+1) return wy;
    if (x == s || y == s) return Wall::none;

    if (y > s) {
      if (x > s) x -= s;
      y -= s;
    } else {
      if (x > s) x = 2 * s - x;
      swap(wx, wy);
      swap(x, y);
    }
    --n;
    s /= 2;
  }
  return Wall::none;
}

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, z;
  cin >> n >> z;

  long long s = 1 << n;

  int x = 1;
  int y = 0;
  int vx = 1;
  int vy = 1;

  long long t;

  --z;
  cin >> t;

  for (long long d = 0; d <= 4*s*s; ++d) {
    while (d == t) {
      cout << x << ' ' << y << endl;
      if (z <= 0) return 0;
      cin >> t;
      --z;
    }
    x += vx;
    y += vy;
    auto w = wall(n, x, y);
    if (w == Wall::x) vx *= -1;
    else if (w == Wall::y) vy *= -1;
  }

  return 0;
}