#include <bits/stdc++.h> #define MP make_pair #define PB push_back #define int long long #define st first #define nd second #define rd third #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) using namespace std; template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(...) (__VA_ARGS__) #define debugv(x) #define cerr if(0)cout #endif #define next ____next #define prev ____prev #define left ____left #define right ___right #define hash ____hash typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } template<class T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} template<class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } }; template<class T> void ResizeVec(T&, vector<int>) {} template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } typedef Triple<int, int, int> TIII; template<class A, class B, class C> ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; } template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } struct Point { int x, y; Point operator-(Point p) { return {x - p.x, y - p.y}; } Point operator+(Point p) { return {x + p.x, y + p.y}; } bool operator==(Point p) { return x == p.x && y == p.y; } friend ostream& operator<<(ostream& out, Point p) { return out<<"("<<p.x<<", "<<p.y<<")"; } }; Point Clockwise(Point p) { return {p.y, -p.x}; } typedef vector<Point> VP; VP Clockwise(VP v) { VP res; for (auto& p : v) { res.PB(Clockwise(p)); } return res; } VP CounterClockwise(VP v) { vector<Point> res = v; REP (tr, 3) { res = Clockwise(res); } return res; } VP Shift(VP v, Point diff) { VP res; diff = diff - v[0]; for (auto& p : v) { res.PB(p + diff); } return res; } VP App(vector<VP> v) { VP nic; for (auto ziom : v) { for (auto dupa : ziom) { nic.PB(dupa); } } return nic; } VP Reverse(VP v) { reverse(ALL(v)); return v; } const int MaxMemo = 7; const int N = (1 << (MaxMemo + 1)) + 2; bool up[N][N], right[N][N]; Point memo[MaxMemo + 3][N * N][2]; Point RotateClockwiseWrt(Point to_rot, Point mid) { return mid + Clockwise(to_rot - mid); } Point RotateCounterWrt(Point to_rot, Point mid) { REP (i, 3) { to_rot = RotateClockwiseWrt(to_rot, mid); } return to_rot; } int pot[66]; Point ReflectWrtVer(int order, Point p) { p.x = pot[order + 1] - p.x; return p; } Point ReflectWrtHor(int order, Point p) { p.y = pot[order + 1] - p.y; return p; } // string go_out = "go_out"; // string midpoint_in = "midpoint_in"; // string enter_chimney = "enter_chimney"; // string enter_turn = "enter_turn"; // string transposed_run = "transposed_run"; // string enter_blind_alley = "enter_blind_alley"; // string exit_blind_alley = "exit_blind_alley"; // string exit_walk_after_blind_alley = "exit_walk_after_blind_alley"; // string enter_half_corridor = "enter_half_corridor"; // string exit_half_corridor = "exit_half_corridor"; // string rightmost = "rightmost"; int go_out = 0; //"go_out"; int midpoint_in = 1; //"midpoint_in"; int enter_chimney = 2; //"enter_chimney"; int enter_turn = 3; //"enter_turn"; int transposed_run = 4; //"transposed_run"; int enter_blind_alley = 5; //"enter_blind_alley"; int exit_blind_alley = 6; //"exit_blind_alley"; int exit_walk_after_blind_alley = 7; //"exit_walk_after_blind_alley"; int enter_half_corridor = 8; //"enter_half_corridor"; int exit_half_corridor = 9; //"exit_half_corridor"; int rightmost = 10; //"rightmost"; //map<string, int> checkpoints[33]; int checkpoints[33][12]; Point Rec(int order, int t, bool is_transposed) { if (order <= MaxMemo - 1) { return memo[order][t][is_transposed]; } int side = pot[order + 1]; Point center = Point{pot[order], pot[order]}; if (!is_transposed) { if (t > checkpoints[order][go_out]) { return RotateCounterWrt(Rec(order, t - checkpoints[order][go_out] - 1, true), center); } if (t > checkpoints[order][midpoint_in]) { return ReflectWrtVer(order, Rec(order, 2 * checkpoints[order][midpoint_in] - t, false)); } // if (t <= checkpoints[order][enter_chimney]) { // return Rec(order - 1, t, true); // } if (t <= checkpoints[order][enter_turn]) { Point ret = Rec(order - 1, t, true); if (ret.y % 4 == 2 && ret.x == center.x - 1) { ret.x += 2; } return ret; } if (t >= checkpoints[order][enter_blind_alley] && t <= checkpoints[order][exit_blind_alley]) { int rec_t = checkpoints[order - 1][enter_turn] + checkpoints[order][exit_blind_alley] - t; Point ret = Rec(order - 1, rec_t, false); ret = ReflectWrtHor(order - 1, ret); ret.y += side / 4; return ret; } if (t >= checkpoints[order][exit_blind_alley] && t <= checkpoints[order][exit_walk_after_blind_alley]) { Point ret = Rec(order - 1, t - checkpoints[order][exit_blind_alley] + checkpoints[order - 1][enter_chimney], false); ret.y += side / 2; return ret; } if (t >= checkpoints[order][enter_turn] && t <= checkpoints[order][enter_half_corridor]) { int from_enter = t - checkpoints[order][enter_turn]; Point ret = Rec(order - 2, from_enter + checkpoints[order - 2][enter_turn], false); return ret + Point{side / 2 - 1, side / 2} - Point{side / 8 - 1, side / 8}; } if (t == checkpoints[order][enter_half_corridor] + 1) { return Point{3 * side / 8, side / 2 - 1}; } if (t == checkpoints[order][exit_half_corridor] - 1) { return Point{3 * side / 8, side / 2 + 1}; } if (t >= checkpoints[order][exit_half_corridor]) { int to_half = checkpoints[order][midpoint_in] - t; Point ret = Rec(order - 2, checkpoints[order - 2][midpoint_in] - to_half, false); return ret + Point{side / 2, side / 2 + 1} - Point{side / 8, side / 8 + 1}; } if (t < checkpoints[order][enter_blind_alley]) { int rec_t = checkpoints[order - 2][rightmost] - (t - checkpoints[order][enter_half_corridor] - 2); Point ret = Rec(order - 2, rec_t, false); ret = ReflectWrtHor(order - 2, ret); return ret + Point{3 * side / 8 - 1, side / 2} - Point{side / 4 - 1, side / 8}; } int exit_turn = checkpoints[order - 2][go_out] - checkpoints[order - 2][enter_turn]; int rec_t = exit_turn - (t - checkpoints[order][exit_walk_after_blind_alley]); Point ret = Rec(order - 2, rec_t, false); ret = ReflectWrtHor(order - 2, ret); return ret + Point{3 * side / 8 - 1, side / 2} - Point{side / 4 - 1, side / 8}; } else { if (t > checkpoints[order][transposed_run] / 2) { return ReflectWrtHor(order, Rec(order, checkpoints[order][transposed_run] - t, true)); } if (t <= checkpoints[order - 1][go_out] + 1) { return Rec(order - 1, t, false); } Point ret = Rec(order - 1, t - checkpoints[order - 1][go_out] - 2, true); ret.x += center.x; if (ret.x % 4 == 2 && ret.y == center.y - 1) { ret.y += 2; } else { int from_right = 2 * center.x - ret.x; if (from_right % 12 >= 5 && from_right % 12 <= 7 && ret.y >= center.y - 3) { ret = ReflectWrtHor(order, ret); } } return ret; } debug(order, t, is_transposed); assert(false); } void Check(int order, int t, bool is_transposed) { Point ret = Rec(order, t, is_transposed); assert(order <= MaxMemo); if (!(ret == memo[order][t][is_transposed])) { debug(ret, memo[order][t][is_transposed], order, t, is_transposed); } assert(ret == memo[order][t][is_transposed]); } void PreprocBasic() { pot[0] = 1; RE (i, 62) { pot[i] = 2 * pot[i - 1]; } checkpoints[3][enter_blind_alley] = 26; checkpoints[3][exit_blind_alley] = 34; checkpoints[4][enter_half_corridor] = 92; checkpoints[4][exit_half_corridor] = 252; checkpoints[2][rightmost] = 18; RE (order, 30) { checkpoints[order][go_out] = pot[2 * order + 1] - 2; checkpoints[order][midpoint_in] = checkpoints[order][go_out] / 2; checkpoints[order][enter_chimney] = (pot[2 * order - 1] - 2) / 3; checkpoints[order][enter_turn] = (pot[2 * order] + 2) / 3; checkpoints[order][transposed_run] = pot[2 * order + 2] - 2 - checkpoints[order][go_out]; if (order >= 4) { checkpoints[order][enter_blind_alley] = 4 * checkpoints[order - 1][enter_blind_alley] - 6; checkpoints[order][exit_blind_alley] = 4 * checkpoints[order - 1][exit_blind_alley] + 2; } checkpoints[order][exit_walk_after_blind_alley] = checkpoints[order][exit_blind_alley] + checkpoints[order - 1][go_out] - 2 * checkpoints[order - 1][enter_chimney]; if (order >= 5) { int delta = 20; if (order % 2 == 1) { delta = -20; } checkpoints[order][enter_half_corridor] = 4 * checkpoints[order - 1][enter_half_corridor] + delta; checkpoints[order][exit_half_corridor] = 4 * checkpoints[order - 1][exit_half_corridor] + delta; debug(checkpoints[order][enter_half_corridor], checkpoints[order][exit_half_corridor]); } if (order >= 3) { int delta = -14; if (order % 2 == 1) { delta = 26; } checkpoints[order][rightmost] = 4 * checkpoints[order - 1][rightmost] + delta; debug(checkpoints[order][rightmost]); } } } Point Transpose(Point p) { return {p.y, p.x}; } void BrutSmall() { Point p[4]; p[0] = {1, 1}; p[1] = {1, 3}; p[2] = {3, 3}; p[3] = {3, 1}; VP v; REP (tr, 4) { v.PB(p[tr]); } RE (order, MaxMemo) { if (order > 1) { REP (tr, 4) { p[tr] = p[tr] + p[tr] - p[0]; } p[3] = p[3] + Point{2, 0}; v = App(vector<VP>{Shift(Reverse(Clockwise(v)), p[0]), Shift(v, p[1]), Shift(v, p[2]), Reverse(Shift(CounterClockwise(v), p[3]))}); } REP (transposed, 2) { REP (x, N) { REP (y, N) { right[x][y] = up[x][y] = false; } } for (int i = 0; i < SZ(v) - 1; i++) { Point fir = v[i], sec = v[i + 1]; if (fir.x + fir.y > sec.x + sec.y) { swap(fir, sec); } if (sec.y > fir.y) { up[fir.x][fir.y] = 1; } else { right[fir.x][fir.y] = 1; } } int side = (1 << (order + 1)); for (int i = 0; i < side; i++) { right[i][0] = 1; right[i][side] = 1; up[0][i] = 1; up[side][i] = 1; } Point dir = {1, 1}; Point cur = {1, 0}; for (int steps = 1; ; steps++) { memo[order][steps - 1][transposed] = cur; cur = cur + dir; if (!transposed && cur == Point{side - 1, side / 2}) { debug(order, side, side * side, steps); } // debug(cur, steps); if (cur.y >= 1 && up[cur.x][cur.y - 1]) { dir.x *= -1; } else if (cur.x >= 1 && right[cur.x - 1][cur.y]) { dir.y *= -1; } if (cur.x == 1 && cur.y == 0) { debug(steps); break; } } for (auto& pt : v) { pt = Transpose(pt); } } } } void SanityChecks() { FOR (t, 0, checkpoints[MaxMemo][enter_turn]) { Check(MaxMemo, t, false); } assert(Rec(MaxMemo, checkpoints[MaxMemo][enter_turn], false) == (Point{pot[MaxMemo] - 1, pot[MaxMemo]})); FOR (t, 0, checkpoints[MaxMemo][transposed_run]) { Check(MaxMemo, t, true); } assert(Rec(MaxMemo, checkpoints[MaxMemo][transposed_run], true) == (Point{1, pot[MaxMemo + 1]})); FOR (t, checkpoints[MaxMemo][go_out] + 1, pot[2 * MaxMemo + 2] - 1) { //checkpoints[MaxMemo][go_out] + 1 + checkpoints[MaxMemo Check(MaxMemo, t, false); } assert(Rec(MaxMemo, checkpoints[MaxMemo][go_out] + 1, false) == (Point{pot[MaxMemo + 1], 1})); assert(Rec(MaxMemo, pot[2 * MaxMemo + 2] - 1, false) == (Point{0, 1})); FOR (t, checkpoints[MaxMemo][enter_blind_alley], checkpoints[MaxMemo][exit_blind_alley]) { Check(MaxMemo, t, false); } assert(Rec(MaxMemo, checkpoints[MaxMemo][enter_blind_alley], false) == (Point{pot[MaxMemo - 1] - 1, pot[MaxMemo]})); assert(Rec(MaxMemo, checkpoints[MaxMemo][exit_blind_alley], false) == (Point{pot[MaxMemo - 1] - 1, pot[MaxMemo]})); FOR (t, checkpoints[MaxMemo][exit_blind_alley], checkpoints[MaxMemo][exit_walk_after_blind_alley]) { Check(MaxMemo, t, false); } assert(Rec(MaxMemo, checkpoints[MaxMemo][exit_walk_after_blind_alley], false) == (Point{pot[MaxMemo - 1] + 1, pot[MaxMemo]})); FOR (t, checkpoints[MaxMemo][enter_turn], checkpoints[MaxMemo][enter_half_corridor]) { Check(MaxMemo, t, false); } assert(Rec(MaxMemo, checkpoints[MaxMemo][enter_half_corridor], false) == (Point{3 * pot[MaxMemo - 2] + 1, pot[MaxMemo]})); FOR (t, checkpoints[MaxMemo][exit_half_corridor], checkpoints[MaxMemo][midpoint_in]) { Check(MaxMemo, t, false); } //debug(Rec(MaxMemo, checkpoints[MaxMemo][exit_half_corridor], false)); assert(Rec(MaxMemo, checkpoints[MaxMemo][exit_half_corridor], false) == (Point{3 * pot[MaxMemo - 2] + 1, pot[MaxMemo]})); assert(Rec(MaxMemo, checkpoints[MaxMemo][midpoint_in], false) == (Point{pot[MaxMemo], pot[MaxMemo] + 1})); Check(MaxMemo, checkpoints[MaxMemo][enter_half_corridor] + 1, false); Check(MaxMemo, checkpoints[MaxMemo][exit_half_corridor] - 1, false); FOR (t, checkpoints[MaxMemo][enter_half_corridor], checkpoints[MaxMemo][enter_blind_alley]) { Check(MaxMemo, t, false); } FOR (t, checkpoints[MaxMemo][exit_walk_after_blind_alley], checkpoints[MaxMemo][exit_half_corridor]) { Check(MaxMemo, t, false); } FOR (t, 0, pot[2 * MaxMemo + 2] - 1) { Check(MaxMemo, t, false); } cerr<<"sanity check passed"<<endl; } int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); cin.tie(0); //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC; PreprocBasic(); BrutSmall(); #ifdef LOCAL SanityChecks(); #endif int n; cin>>n; int T; cin>>T; RE (i, T) { int t; cin>>t; Point pt = Rec(n, t, false); cout<<pt.x<<" "<<pt.y<<endl; } return 0; }
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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | #include <bits/stdc++.h> #define MP make_pair #define PB push_back #define int long long #define st first #define nd second #define rd third #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) using namespace std; template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(...) (__VA_ARGS__) #define debugv(x) #define cerr if(0)cout #endif #define next ____next #define prev ____prev #define left ____left #define right ___right #define hash ____hash typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } template<class T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} template<class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } }; template<class T> void ResizeVec(T&, vector<int>) {} template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } typedef Triple<int, int, int> TIII; template<class A, class B, class C> ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; } template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } struct Point { int x, y; Point operator-(Point p) { return {x - p.x, y - p.y}; } Point operator+(Point p) { return {x + p.x, y + p.y}; } bool operator==(Point p) { return x == p.x && y == p.y; } friend ostream& operator<<(ostream& out, Point p) { return out<<"("<<p.x<<", "<<p.y<<")"; } }; Point Clockwise(Point p) { return {p.y, -p.x}; } typedef vector<Point> VP; VP Clockwise(VP v) { VP res; for (auto& p : v) { res.PB(Clockwise(p)); } return res; } VP CounterClockwise(VP v) { vector<Point> res = v; REP (tr, 3) { res = Clockwise(res); } return res; } VP Shift(VP v, Point diff) { VP res; diff = diff - v[0]; for (auto& p : v) { res.PB(p + diff); } return res; } VP App(vector<VP> v) { VP nic; for (auto ziom : v) { for (auto dupa : ziom) { nic.PB(dupa); } } return nic; } VP Reverse(VP v) { reverse(ALL(v)); return v; } const int MaxMemo = 7; const int N = (1 << (MaxMemo + 1)) + 2; bool up[N][N], right[N][N]; Point memo[MaxMemo + 3][N * N][2]; Point RotateClockwiseWrt(Point to_rot, Point mid) { return mid + Clockwise(to_rot - mid); } Point RotateCounterWrt(Point to_rot, Point mid) { REP (i, 3) { to_rot = RotateClockwiseWrt(to_rot, mid); } return to_rot; } int pot[66]; Point ReflectWrtVer(int order, Point p) { p.x = pot[order + 1] - p.x; return p; } Point ReflectWrtHor(int order, Point p) { p.y = pot[order + 1] - p.y; return p; } // string go_out = "go_out"; // string midpoint_in = "midpoint_in"; // string enter_chimney = "enter_chimney"; // string enter_turn = "enter_turn"; // string transposed_run = "transposed_run"; // string enter_blind_alley = "enter_blind_alley"; // string exit_blind_alley = "exit_blind_alley"; // string exit_walk_after_blind_alley = "exit_walk_after_blind_alley"; // string enter_half_corridor = "enter_half_corridor"; // string exit_half_corridor = "exit_half_corridor"; // string rightmost = "rightmost"; int go_out = 0; //"go_out"; int midpoint_in = 1; //"midpoint_in"; int enter_chimney = 2; //"enter_chimney"; int enter_turn = 3; //"enter_turn"; int transposed_run = 4; //"transposed_run"; int enter_blind_alley = 5; //"enter_blind_alley"; int exit_blind_alley = 6; //"exit_blind_alley"; int exit_walk_after_blind_alley = 7; //"exit_walk_after_blind_alley"; int enter_half_corridor = 8; //"enter_half_corridor"; int exit_half_corridor = 9; //"exit_half_corridor"; int rightmost = 10; //"rightmost"; //map<string, int> checkpoints[33]; int checkpoints[33][12]; Point Rec(int order, int t, bool is_transposed) { if (order <= MaxMemo - 1) { return memo[order][t][is_transposed]; } int side = pot[order + 1]; Point center = Point{pot[order], pot[order]}; if (!is_transposed) { if (t > checkpoints[order][go_out]) { return RotateCounterWrt(Rec(order, t - checkpoints[order][go_out] - 1, true), center); } if (t > checkpoints[order][midpoint_in]) { return ReflectWrtVer(order, Rec(order, 2 * checkpoints[order][midpoint_in] - t, false)); } // if (t <= checkpoints[order][enter_chimney]) { // return Rec(order - 1, t, true); // } if (t <= checkpoints[order][enter_turn]) { Point ret = Rec(order - 1, t, true); if (ret.y % 4 == 2 && ret.x == center.x - 1) { ret.x += 2; } return ret; } if (t >= checkpoints[order][enter_blind_alley] && t <= checkpoints[order][exit_blind_alley]) { int rec_t = checkpoints[order - 1][enter_turn] + checkpoints[order][exit_blind_alley] - t; Point ret = Rec(order - 1, rec_t, false); ret = ReflectWrtHor(order - 1, ret); ret.y += side / 4; return ret; } if (t >= checkpoints[order][exit_blind_alley] && t <= checkpoints[order][exit_walk_after_blind_alley]) { Point ret = Rec(order - 1, t - checkpoints[order][exit_blind_alley] + checkpoints[order - 1][enter_chimney], false); ret.y += side / 2; return ret; } if (t >= checkpoints[order][enter_turn] && t <= checkpoints[order][enter_half_corridor]) { int from_enter = t - checkpoints[order][enter_turn]; Point ret = Rec(order - 2, from_enter + checkpoints[order - 2][enter_turn], false); return ret + Point{side / 2 - 1, side / 2} - Point{side / 8 - 1, side / 8}; } if (t == checkpoints[order][enter_half_corridor] + 1) { return Point{3 * side / 8, side / 2 - 1}; } if (t == checkpoints[order][exit_half_corridor] - 1) { return Point{3 * side / 8, side / 2 + 1}; } if (t >= checkpoints[order][exit_half_corridor]) { int to_half = checkpoints[order][midpoint_in] - t; Point ret = Rec(order - 2, checkpoints[order - 2][midpoint_in] - to_half, false); return ret + Point{side / 2, side / 2 + 1} - Point{side / 8, side / 8 + 1}; } if (t < checkpoints[order][enter_blind_alley]) { int rec_t = checkpoints[order - 2][rightmost] - (t - checkpoints[order][enter_half_corridor] - 2); Point ret = Rec(order - 2, rec_t, false); ret = ReflectWrtHor(order - 2, ret); return ret + Point{3 * side / 8 - 1, side / 2} - Point{side / 4 - 1, side / 8}; } int exit_turn = checkpoints[order - 2][go_out] - checkpoints[order - 2][enter_turn]; int rec_t = exit_turn - (t - checkpoints[order][exit_walk_after_blind_alley]); Point ret = Rec(order - 2, rec_t, false); ret = ReflectWrtHor(order - 2, ret); return ret + Point{3 * side / 8 - 1, side / 2} - Point{side / 4 - 1, side / 8}; } else { if (t > checkpoints[order][transposed_run] / 2) { return ReflectWrtHor(order, Rec(order, checkpoints[order][transposed_run] - t, true)); } if (t <= checkpoints[order - 1][go_out] + 1) { return Rec(order - 1, t, false); } Point ret = Rec(order - 1, t - checkpoints[order - 1][go_out] - 2, true); ret.x += center.x; if (ret.x % 4 == 2 && ret.y == center.y - 1) { ret.y += 2; } else { int from_right = 2 * center.x - ret.x; if (from_right % 12 >= 5 && from_right % 12 <= 7 && ret.y >= center.y - 3) { ret = ReflectWrtHor(order, ret); } } return ret; } debug(order, t, is_transposed); assert(false); } void Check(int order, int t, bool is_transposed) { Point ret = Rec(order, t, is_transposed); assert(order <= MaxMemo); if (!(ret == memo[order][t][is_transposed])) { debug(ret, memo[order][t][is_transposed], order, t, is_transposed); } assert(ret == memo[order][t][is_transposed]); } void PreprocBasic() { pot[0] = 1; RE (i, 62) { pot[i] = 2 * pot[i - 1]; } checkpoints[3][enter_blind_alley] = 26; checkpoints[3][exit_blind_alley] = 34; checkpoints[4][enter_half_corridor] = 92; checkpoints[4][exit_half_corridor] = 252; checkpoints[2][rightmost] = 18; RE (order, 30) { checkpoints[order][go_out] = pot[2 * order + 1] - 2; checkpoints[order][midpoint_in] = checkpoints[order][go_out] / 2; checkpoints[order][enter_chimney] = (pot[2 * order - 1] - 2) / 3; checkpoints[order][enter_turn] = (pot[2 * order] + 2) / 3; checkpoints[order][transposed_run] = pot[2 * order + 2] - 2 - checkpoints[order][go_out]; if (order >= 4) { checkpoints[order][enter_blind_alley] = 4 * checkpoints[order - 1][enter_blind_alley] - 6; checkpoints[order][exit_blind_alley] = 4 * checkpoints[order - 1][exit_blind_alley] + 2; } checkpoints[order][exit_walk_after_blind_alley] = checkpoints[order][exit_blind_alley] + checkpoints[order - 1][go_out] - 2 * checkpoints[order - 1][enter_chimney]; if (order >= 5) { int delta = 20; if (order % 2 == 1) { delta = -20; } checkpoints[order][enter_half_corridor] = 4 * checkpoints[order - 1][enter_half_corridor] + delta; checkpoints[order][exit_half_corridor] = 4 * checkpoints[order - 1][exit_half_corridor] + delta; debug(checkpoints[order][enter_half_corridor], checkpoints[order][exit_half_corridor]); } if (order >= 3) { int delta = -14; if (order % 2 == 1) { delta = 26; } checkpoints[order][rightmost] = 4 * checkpoints[order - 1][rightmost] + delta; debug(checkpoints[order][rightmost]); } } } Point Transpose(Point p) { return {p.y, p.x}; } void BrutSmall() { Point p[4]; p[0] = {1, 1}; p[1] = {1, 3}; p[2] = {3, 3}; p[3] = {3, 1}; VP v; REP (tr, 4) { v.PB(p[tr]); } RE (order, MaxMemo) { if (order > 1) { REP (tr, 4) { p[tr] = p[tr] + p[tr] - p[0]; } p[3] = p[3] + Point{2, 0}; v = App(vector<VP>{Shift(Reverse(Clockwise(v)), p[0]), Shift(v, p[1]), Shift(v, p[2]), Reverse(Shift(CounterClockwise(v), p[3]))}); } REP (transposed, 2) { REP (x, N) { REP (y, N) { right[x][y] = up[x][y] = false; } } for (int i = 0; i < SZ(v) - 1; i++) { Point fir = v[i], sec = v[i + 1]; if (fir.x + fir.y > sec.x + sec.y) { swap(fir, sec); } if (sec.y > fir.y) { up[fir.x][fir.y] = 1; } else { right[fir.x][fir.y] = 1; } } int side = (1 << (order + 1)); for (int i = 0; i < side; i++) { right[i][0] = 1; right[i][side] = 1; up[0][i] = 1; up[side][i] = 1; } Point dir = {1, 1}; Point cur = {1, 0}; for (int steps = 1; ; steps++) { memo[order][steps - 1][transposed] = cur; cur = cur + dir; if (!transposed && cur == Point{side - 1, side / 2}) { debug(order, side, side * side, steps); } // debug(cur, steps); if (cur.y >= 1 && up[cur.x][cur.y - 1]) { dir.x *= -1; } else if (cur.x >= 1 && right[cur.x - 1][cur.y]) { dir.y *= -1; } if (cur.x == 1 && cur.y == 0) { debug(steps); break; } } for (auto& pt : v) { pt = Transpose(pt); } } } } void SanityChecks() { FOR (t, 0, checkpoints[MaxMemo][enter_turn]) { Check(MaxMemo, t, false); } assert(Rec(MaxMemo, checkpoints[MaxMemo][enter_turn], false) == (Point{pot[MaxMemo] - 1, pot[MaxMemo]})); FOR (t, 0, checkpoints[MaxMemo][transposed_run]) { Check(MaxMemo, t, true); } assert(Rec(MaxMemo, checkpoints[MaxMemo][transposed_run], true) == (Point{1, pot[MaxMemo + 1]})); FOR (t, checkpoints[MaxMemo][go_out] + 1, pot[2 * MaxMemo + 2] - 1) { //checkpoints[MaxMemo][go_out] + 1 + checkpoints[MaxMemo Check(MaxMemo, t, false); } assert(Rec(MaxMemo, checkpoints[MaxMemo][go_out] + 1, false) == (Point{pot[MaxMemo + 1], 1})); assert(Rec(MaxMemo, pot[2 * MaxMemo + 2] - 1, false) == (Point{0, 1})); FOR (t, checkpoints[MaxMemo][enter_blind_alley], checkpoints[MaxMemo][exit_blind_alley]) { Check(MaxMemo, t, false); } assert(Rec(MaxMemo, checkpoints[MaxMemo][enter_blind_alley], false) == (Point{pot[MaxMemo - 1] - 1, pot[MaxMemo]})); assert(Rec(MaxMemo, checkpoints[MaxMemo][exit_blind_alley], false) == (Point{pot[MaxMemo - 1] - 1, pot[MaxMemo]})); FOR (t, checkpoints[MaxMemo][exit_blind_alley], checkpoints[MaxMemo][exit_walk_after_blind_alley]) { Check(MaxMemo, t, false); } assert(Rec(MaxMemo, checkpoints[MaxMemo][exit_walk_after_blind_alley], false) == (Point{pot[MaxMemo - 1] + 1, pot[MaxMemo]})); FOR (t, checkpoints[MaxMemo][enter_turn], checkpoints[MaxMemo][enter_half_corridor]) { Check(MaxMemo, t, false); } assert(Rec(MaxMemo, checkpoints[MaxMemo][enter_half_corridor], false) == (Point{3 * pot[MaxMemo - 2] + 1, pot[MaxMemo]})); FOR (t, checkpoints[MaxMemo][exit_half_corridor], checkpoints[MaxMemo][midpoint_in]) { Check(MaxMemo, t, false); } //debug(Rec(MaxMemo, checkpoints[MaxMemo][exit_half_corridor], false)); assert(Rec(MaxMemo, checkpoints[MaxMemo][exit_half_corridor], false) == (Point{3 * pot[MaxMemo - 2] + 1, pot[MaxMemo]})); assert(Rec(MaxMemo, checkpoints[MaxMemo][midpoint_in], false) == (Point{pot[MaxMemo], pot[MaxMemo] + 1})); Check(MaxMemo, checkpoints[MaxMemo][enter_half_corridor] + 1, false); Check(MaxMemo, checkpoints[MaxMemo][exit_half_corridor] - 1, false); FOR (t, checkpoints[MaxMemo][enter_half_corridor], checkpoints[MaxMemo][enter_blind_alley]) { Check(MaxMemo, t, false); } FOR (t, checkpoints[MaxMemo][exit_walk_after_blind_alley], checkpoints[MaxMemo][exit_half_corridor]) { Check(MaxMemo, t, false); } FOR (t, 0, pot[2 * MaxMemo + 2] - 1) { Check(MaxMemo, t, false); } cerr<<"sanity check passed"<<endl; } int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); cin.tie(0); //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC; PreprocBasic(); BrutSmall(); #ifdef LOCAL SanityChecks(); #endif int n; cin>>n; int T; cin>>T; RE (i, T) { int t; cin>>t; Point pt = Rec(n, t, false); cout<<pt.x<<" "<<pt.y<<endl; } return 0; } |