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
#include<initializer_list>
#include<iostream>
#include<queue>
#include<list>
#include<stdio.h>
#include<vector>
#include<map>
#include<set>
#include<cstdlib>

using namespace std;
#define ull long long int
#define ll long long int
#define FOR(i, n) for(int i=0;i<n;++i)
#define FORI(f, i, n) for(int i=(f);i<(n);++i)
#define FORD(i, n) for(int i=(n)-1;i>=0;--i)
#define znakow 26
#define dcout 0 && cout

int inline min(int a, int b) { return a > b ? b : a; }

int inline max(int a, int b) { return a < b ? b : a; }

class Vector {
public:
    ull x, y;

    Vector() {
        x = y = 0;
    }

    Vector(ull x, ull y) {
        this->x = x;
        this->y = y;
    }
};

typedef int Stage;
map<Stage,char*> rsd;


vector<Stage> **subStages[100];

Vector **translations[100];
ull *durations[100];
int *turnings[100];

int UP = 0;
int RIGHT = 1;
int LEFT = 3;
int DOWN = 2;

int TURNING_LEFT = 3;
int TURNING_RIGHT = 1;
int NO_TURNING = 0;

void translate(Vector *currentPosition, Vector*other, int direction){
    if(direction==UP){
        currentPosition->x+=other->x;
        currentPosition->y+=other->y;
    }
    if(direction==DOWN){
        currentPosition->x+=-other->x;
        currentPosition->y+=-other->y;
    }
    if(direction==LEFT){
        currentPosition->x-=other->y;
        currentPosition->y+=other->x;
    }
    if(direction==RIGHT){
        currentPosition->x+=other->y;
        currentPosition->y-=other->x;
    }
}

ull searchedTime;int leftTimes;
void dig(Stage currentStagePath, ull currentTime, Vector *currentPosition, int currentStage,
          int direction) {
    //dcout<<"Looking for time "<<searchedTime<<", now is "<<currentTime<<", doing "<<rsd[currentStage]<<" stage"<<endl;
    ////dcout<<r<<endl;
    
    vector<Stage> *currentPath = subStages[currentStagePath][currentStage];
    ////dcout<<(currentPath==NULL)<<endl;
    //dcout<<"Path size: "<<currentPath->size()<<endl;
    FOR(i, currentPath->size()) {//if(++r>10)return;
        int lowerStage = currentStage - 1;
        Stage nextStage = currentPath->at(i);
        ull nextStageTime = durations[nextStage][lowerStage];
        //dcout<<"It's "<<currentTime<<", nextStageTime is "<<nextStageTime<<", nextStage is "<<rsd[nextStage]<<"["<<lowerStage<<"]"<<endl;
        if (currentTime + nextStageTime > searchedTime) {
            //dcout<<"I dig into"<<endl;
            dig(nextStage, currentTime, currentPosition, lowerStage, direction);
        } else {
            //dcout<<"I skip it, so I move in direction "<<direction<<", from ["
            //    <<currentPosition->x<<","<<currentPosition->y<<"] by "<<rsd[nextStage]<<"["<<lowerStage<<"] vector ["<<
            //    translations[nextStage][lowerStage]->x<<","<<translations[nextStage][lowerStage]->y<<"]"
            //    <<"";
                translate(currentPosition, translations[nextStage][lowerStage], direction);
            //currentPosition->x += translations[nextStage][lowerStage]->x * getDirectionalX(direction);
            //currentPosition->y += translations[nextStage][lowerStage]->y * getDirectionalY(direction);

            //dcout<<" to ["<<currentPosition->x<<","<<currentPosition->y<<"]"<<endl;
        }
        //dcout<<"I am looking in direction "<<direction<<" and going to turn "<<turnings[nextStage][lowerStage]<<endl;
        direction = (direction + turnings[nextStage][lowerStage] + 4) % 4;
        currentTime += nextStageTime;

        if (currentTime == searchedTime) {
            //dcout<<"Found it ["<<searchedTime<<"]! Left: "<<leftTimes<<", now: "<<currentTime<<", direction: "<<direction<<endl<<"    >>>    ";
            cout << currentPosition->x << " " << currentPosition->y << endl;
            if (!leftTimes) {
                //dcout<<"Iamdone"<<endl;
               exit(0);
            }
            ull newSearchTime;
            cin >> newSearchTime;
            while(leftTimes && newSearchTime==searchedTime){
                cin >> newSearchTime;
                --leftTimes;
                cout<<currentPosition->x << " " << currentPosition->y << endl;
            }
            searchedTime=newSearchTime;
            --leftTimes;
            //dcout<<"New searchedTime "<<searchedTime<<endl;

        }
    }
}

void calculate(Stage stage, int order) {////dcout<<stage<<" "<<order<<endl;  
    if (durations[stage][order] != -1) {
        return;
    }
    ////dcout<<"got "<<stage<<endl;
    ull sumDuration = 0;
    int sumTurnings = 0;
    Vector *sumTranslations = new Vector(0, 0);
    FOR(i, subStages[stage][order]->size()) {
        int lessStage = order-1;//max(order - 1, 0);
        ////dcout<<"zlecam "<<i<<endl;
        Stage subStage = subStages[stage][order]->at(i);
        calculate(subStage, lessStage);

        if(stage==10){
            // //dcout<<"Translating "<<rsd[stage]<<"["<<order<<"] by "<<rsd[subStage]<<"["<<lessStage<<"]\n";
            // //dcout<<"x+="<<translations[subStage][lessStage]->x*getDirectionalX(sumTurnings%4)<<endl;
            // //dcout<<"y+="<<translations[subStage][lessStage]->y*getDirectionalY(sumTurnings%4)<<endl;
            // //dcout<<"turnings += "<<turnings[subStage][lessStage]<<endl;
        }

        //sumTranslations->x += translations[subStage][lessStage]->x*getDirectionalX(sumTurnings%4);
        //sumTranslations->y += translations[subStage][lessStage]->y*getDirectionalY(sumTurnings%4);
        translate(sumTranslations, translations[subStage][lessStage], sumTurnings%4);
        sumDuration += durations[subStage][lessStage];
        sumTurnings = (sumTurnings + turnings[subStage][lessStage])%4;
    }
    durations[stage][order] = sumDuration;
    turnings[stage][order] = sumTurnings % 4;
    translations[stage][order] = sumTranslations;

    if(order<3){
        //dcout<<"sumTranslations["<<rsd[stage]<<"]["<<order<<"].x = "<<translations[stage][order]->x<<endl;
        //dcout<<"sumTranslations["<<rsd[stage]<<"]["<<order<<"].y = "<<translations[stage][order]->y<<endl;
        //dcout<<"turnings["<<rsd[stage]<<"]["<<order<<"].y = "<<turnings[stage][order]<<endl;
    }
}

int LEVELS = 40;

Vector *STEP_FORWARD = new Vector(1, 1);
Vector *NO_MOVE = new Vector(0, 0);

ull INSTANT = 0;
ull A_SECOND = 1;
int UNKNOWN = -1;

void configure(Stage stage, long long int zeroDuration, Vector *zeroTranslation, int zeroTurning,
               initializer_list<int> initializerList) {

    durations[stage] = new ull[LEVELS];
    FOR(i, LEVELS)durations[stage][i] = -1;
    durations[stage][0] = zeroDuration;

    translations[stage] = new Vector *[LEVELS];
    translations[stage][0] = zeroTranslation;

    turnings[stage] = new int[LEVELS];
    turnings[stage][0] = zeroTurning;

    subStages[stage]=new vector<Stage>*[LEVELS];
    FOR(i,LEVELS)
        subStages[stage][i] = new vector<Stage>(initializerList);
    subStages[stage][0]->clear();
}


#define MRM M, TURN_RIGHT, M
#define MLM M, TURN_LEFT, M
#define MRML M, TURN_RIGHT, M, TURN_LEFT
#define D_PART LR, TURN_LEFT, RL
#define H_PART TT, M, LF, TURN_LEFT, FL, MRM, FR, TURN_LEFT, RF, M, TT
#define H_PART_1_REV M, TURN_RIGHT, M, TURN_RIGHT, M, TURN_RIGHT, M
#define FRONT_BORDER_1 M, M, TURN_LEFT, M, TURN_LEFT, M, TURN_LEFT, M, M, TURN_LEFT
#define FRONT_BORDER Rb, M, H_PART, C, Lb
#define TOP_BORDER Tb, M, D_PART, C, Tb

int main() {
ios::sync_with_stdio(false);
int stageCounter = 0;
Stage LEVEL = ++stageCounter;rsd[LEVEL]="LEVEL";// 1
Stage M = ++stageCounter;rsd[M]="M";// 2

//Stage MRM = ++stageCounter;rsd[MRM]="MRM";// 3
Stage TOP_BOUNCE_TURN = ++stageCounter;rsd[TOP_BOUNCE_TURN]="TOP_BOUNCE_TURN";// 4
Stage C = ++stageCounter;rsd[C]="C";// 5
Stage TURN_LEFT = ++stageCounter;rsd[TURN_LEFT]="TURN_LEFT";// 6
Stage TURN_RIGHT = ++stageCounter;rsd[TURN_RIGHT]="TURN_RIGHT";// 7

Stage Rb = ++stageCounter;rsd[Rb]="Rb";// 8
Stage Lb = ++stageCounter;rsd[Lb]="Lb";// 9
Stage Tb = ++stageCounter;rsd[Tb]="Tb";// 10
Stage Fb = ++stageCounter;rsd[Fb]="Fb";// 11
Stage TT = ++stageCounter;rsd[TT]="TT";// 12
Stage LF = ++stageCounter;rsd[LF]="LF";// 13
Stage FL = ++stageCounter;rsd[FL]="FL";// 14
Stage RL = ++stageCounter;rsd[RL]="RL";// 15
Stage LR = ++stageCounter;rsd[LR]="LR";// 16
Stage FR = ++stageCounter;rsd[FR]="FR";// 17
Stage RF = ++stageCounter;rsd[RF]="RF";// 18
Stage LL = ++stageCounter;rsd[LL]="LL";// 19
Stage RR = ++stageCounter;rsd[RR]="RR";// 20
Stage FF = ++stageCounter;rsd[FF]="FF";// 21

    configure(M, A_SECOND, STEP_FORWARD, NO_TURNING, {M});
    configure(TURN_LEFT, INSTANT, NO_MOVE, TURNING_LEFT, {TURN_LEFT});
    configure(TURN_RIGHT, INSTANT, NO_MOVE, TURNING_RIGHT, {TURN_RIGHT});

    configure(C, A_SECOND, STEP_FORWARD, TURNING_LEFT, {M, TURN_LEFT});

    configure(TOP_BOUNCE_TURN, INSTANT, NO_MOVE, TURNING_LEFT, {TURNING_RIGHT});


    configure(Rb, INSTANT, NO_MOVE, NO_TURNING, {Fb, MRML, Rb});
    configure(Lb, INSTANT, NO_MOVE, NO_TURNING, {Lb, MRML, Fb});
    configure(Tb, INSTANT, NO_MOVE, NO_TURNING, {TOP_BORDER});
    configure(Fb, INSTANT, NO_MOVE, NO_TURNING, {FRONT_BORDER});

    configure(LF, INSTANT, NO_MOVE, NO_TURNING, {LL, MRM, FR});
    configure(FL, INSTANT, NO_MOVE, NO_TURNING, {RF, M, H_PART, M, LL});
    configure(RL, INSTANT, NO_MOVE, NO_TURNING, {FF, MRM, RL});
    configure(LR, INSTANT, NO_MOVE, NO_TURNING, {LR, MRM, FF});
    configure(FR, INSTANT, NO_MOVE, NO_TURNING, {RR, M, H_PART, M, LF});
    configure(RF, INSTANT, NO_MOVE, NO_TURNING, {FL, MRM, RR});

    configure(TT, INSTANT, NO_MOVE, NO_TURNING, {TT, M, D_PART, M, TT});
    configure(LL, INSTANT, NO_MOVE, NO_TURNING, {LF, MRM, FL});
    configure(RR, INSTANT, NO_MOVE, NO_TURNING, {FR, MRM, RF});
    configure(FF, INSTANT, NO_MOVE, NO_TURNING, {RL, M, H_PART, M, LR});

    configure(LEVEL, INSTANT, NO_MOVE, NO_TURNING, {FRONT_BORDER, C, Fb, MRML, Rb, C, TOP_BORDER, C, Lb, MRML, Fb, C});

    subStages[FL][1] = new vector<Stage>({MLM});
    subStages[LF][1] = new vector<Stage>({M, H_PART_1_REV, M});
    subStages[FR][1] = new vector<Stage>({MLM});
    subStages[RF][1] = new vector<Stage>({M, H_PART_1_REV, M});
    subStages[LR][1] = new vector<Stage>({MLM});
    subStages[RL][1] = new vector<Stage>({MLM});
    subStages[TT][1] = new vector<Stage>({MLM});
    subStages[LL][1] = new vector<Stage>({MLM});
    subStages[RR][1] = new vector<Stage>({MLM});
    subStages[FF][1] = new vector<Stage>({M, H_PART_1_REV, M});

    subStages[LEVEL][1] = new vector<Stage>({FRONT_BORDER_1, C, MRML, C, MRML, C, MRML, C});


    subStages[Fb][1] = new vector<Stage>({FRONT_BORDER_1});
    subStages[Tb][1] = new vector<Stage>({MRML});

    //calculate(LEVEL, 33);
// int x=3;
// cout<<"Rb["<<x<<"] = "<<durations[Rb][x]<<endl;;
// cout<<"Lb["<<x<<"] = "<<durations[Lb][x]<<endl;;
// cout<<"Tb["<<x<<"] = "<<durations[Tb][x]<<endl;;
// cout<<"Fb["<<x<<"] = "<<durations[Fb][x]<<endl;;
// cout<<"TT["<<x<<"] = "<<durations[TT][x]<<endl;;
// //dcout<<endl;
// cout<<"LF["<<x<<"] = "<<durations[LF][x]<<endl;;
// cout<<"FL["<<x<<"] = "<<durations[FL][x]<<endl;;
// cout<<"RL["<<x<<"] = "<<durations[RL][x]<<endl;;
// cout<<"LR["<<x<<"] = "<<durations[LR][x]<<endl;;
// cout<<"FR["<<x<<"] = "<<durations[FR][x]<<endl;;
// //dcout<<endl;
// cout<<"RF["<<x<<"] = "<<durations[RF][x]<<endl;;
// cout<<"LL["<<x<<"] = "<<durations[LL][x]<<endl;;
// cout<<"RR["<<x<<"] = "<<durations[RR][x]<<endl;;
// cout<<"FF["<<x<<"] = "<<durations[FF][x]<<endl;;
// cout<<"LEVEL["<<x<<"] = "<<durations[LEVEL][x]<<endl;;

//     cout<<durations[FR][x]+durations[RF][x]<<endl;
    // cout<<durations[FL][x]+durations[LF][x]<<endl;
    //return 0;

    int boardSize;
    //int times;
    cin >> boardSize;
    cin >> leftTimes;


    calculate(LEVEL,boardSize);
    //ull searchedTime;
    cin >> searchedTime;
    --leftTimes;
    //cout<<searchedTime<<endl;
    //cout<<durations[LEVEL][boardSize]<<endl;

    dig(LEVEL, 0, new Vector(1,0), boardSize, UP);
    //cout<<"WTF?"<<leftTimes<<endl;
    return 0;
}