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
#include<bits/stdc++.h>
using namespace std;
# ifdef DEB
# define debug(...) fprintf(stderr, __VA_ARGS__)
# else
# define debug(...)
#endif
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define LL long long
#define LD long double
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define pb pop_back
#define VI vector<int> 
#define VPI vector<PII> 
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define FORD(i,a,b) for(int i = (a); i >= (b); i--)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define ALL(x) (x).begin(), (x).end()
#define siz(V) ((int)V.size())

int P[32], n, t;
vector<bool> B[15][33000];
vector<int> Q;
int X[]={1, -1, -1, 1}, Y[]={1, 1, -1, -1};
int NewD[][4][2]={{{0, 1}, {1, 0}, {2, 3}, {3, 2}}, {{0, 3}, {1, 2}, {2, 1}, {3, 0}}};
//0-vertical, 1-horizontal
//0-notwall, 1-wall

void build(int ind)
{
  FOR(i, P[ind]+1, P[ind+1]-1)
  {
    RE(j, P[ind-1])
    {
      B[ind][i][j]=B[ind-1][i-P[ind]][min(j, P[ind-1]-j+((i%2)^1))];
    }
  }
  FOR(i, 1, P[ind]-1)
  {
    RE(j, P[ind-1])
    {
      int h=j*2-((i%2)^1), d=(P[ind]-i+1)/2;
      B[ind][i][j]=B[ind][h+P[ind]][d];
    }
  }
  B[ind][P[ind]][1]=true;
  B[ind][P[ind]+1][P[ind-1]]=true;
  /*FORD(i, P[ind+1]-1, 1)
  {
    FOR(j, 1, P[ind-1])
    {
      //debug("%d ", B[ind][i][j]);
      cout<< B[ind][i][j] << ' ';
    }
   cout<< "\n";
  }
  cout<< "\n\n";*/
}

void solve()
{
  REP(i, t)
  {
    int a;
    scanf("%d", &a);
    Q.PB(a);
  }
  int dir=3, posx=1, posy=0, time=0;
  REP(i, t)
  {
    while(time<Q[i])
    {
      time++;
      bool iswall=B[n][posy][(min(posx, P[n+1]-posx)+1)/2];
      if((posy%2==0 && posy>0 && posy<P[n+1]) || posx==0 || posx==P[n+1])
        dir=NewD[0][dir][iswall];
      else
        dir=NewD[1][dir][iswall];
      //debug("time=%d dir=%d iswall=%d\n", time, dir, iswall);
      posx+=X[dir];
      posy+=Y[dir];
    }
    printf("%d %d\n", posx, posy);
  }
}

int main()
{
  //ios_base::sync_with_stdio(0);
  P[0]=1;
  RE(i, 31)
    P[i]=P[i-1]*2;
  scanf("%d%d", &n, &t);
  if(n>=15)
    return 0;
  FOR(i, 1, n)
  {
    FOR(j, 0, P[i+1])
    { 
      //debug("%d %d %d\n", i, j, P[i-1]);
      B[i][j].resize(P[i-1]+1, false);
      B[i][j].shrink_to_fit();
    }
  }
  B[1][2][1]=true;
  B[1][3][1]=true;
  FOR(i, 2, n)
    build(i);
  FOR(i, 1, n)
  {
    FOR(j, 0, P[i-1])
    {
      B[i][0][j]=true;
      B[i][P[i+1]][j]=true;
    }
    FOR(j, 0, P[i+1])
      B[i][j][0]=true;
  }
  solve();
  return 0;
}