#include <cstdio> #include <cmath> using namespace std; static const int MAX_N = 30; typedef long long ll; constexpr int T = 0; constexpr int L = 1; constexpr int U = 2; constexpr int R = 3; constexpr int Left(int rot) { return (rot + 1) % 4; } constexpr int Right(int rot) { return (rot + 3) % 4; } constexpr int MirrorHoriz(int rot) { return rot % 2 == 0 ? rot ^ 2 : rot; } constexpr int MirrorVert(int rot) { return rot % 2 == 1 ? rot ^ 2 : rot; } // T -> R, L -> L, U -> U, R -> T constexpr int LowerLeft(int rot) { return rot == T || rot == R ? rot ^ 3 : rot; } // T -> L, L -> T, U -> U, R -> R constexpr int LowerRight(int rot) { return rot < 2 ? rot ^ 1 : rot; } struct Point { int x, y; Point() : x(0), y(0) {} Point(int x, int y) : x(x), y(y) {} Point MirrorHoriz() const { return {x, -y}; } Point MirrorVert() const { return {-x, y}; } Point Left() const { return {-y, x}; } template <int rot> Point Rotate() const { return rot ? Left().Rotate<::Right(rot)>() : *this; } Point Right() const { return Rotate<R>(); } void operator+=(Point p) { x += p.x; y += p.y; } }; Point operator+(Point a, Point b) { return {a.x + b.x, a.y + b.y}; } Point operator-(Point a, Point b) { return {a.x - b.x, a.y - b.y}; } const Point X(1, 0); const Point Y(0, 1); Point After(int n) { return {1 << (n + 1), 0}; } struct Res { bool up; int t; Res() : t(-1) {} }; template <int A, int B> Res Between(int n); template <int A, int B> Point FindBetween(int n, ll t); template <int A, int B> ll BackForth(int n) { Res a = Between<A, B>(n); return a.t + (a.up ? Between<MirrorVert(A), MirrorVert(B)>(n) : Between<B, A>(n)).t; } template <int A, int B> Point FindBackForth(int n, ll t) { if (t == 0) return X; Res a = Between<A, B>(n); if (t < a.t) return FindBetween<A, B>(n, t); Point p = After(n); if (t == a.t) return p - X; t -= a.t; return p - (a.up ? FindBetween<MirrorVert(A), MirrorVert(B)>(n, t) : FindBetween<B, A>(n, t)).MirrorHoriz(); } ll Outside(int n) { return BackForth<L, R>(n - 1); } Point FindOutside(int n, ll t) { return FindBackForth<L, R>(n - 1, t); } ll InsideMiddle(int n) { return BackForth<T, L>(n); } ll Inside(int n) { return 2 * Between<U, U>(n - 1).t + 4 + 2 * InsideMiddle(n - 1); } Point FindInsideMiddle(int n, ll t) { return FindBackForth<T, L>(n, t); } Point FindInside(int n, ll t) { Res a = Between<U, U>(n - 1); if (t < a.t) return FindBetween<U, U>(n - 1, t); Point p = After(n - 1); if (t == a.t) return p - X; t -= a.t + 1; auto Flip = [=](Point p) { return a.up ? p.MirrorHoriz() : p; }; ll bt = InsideMiddle(n - 1); if (t < bt) return Flip(p + FindInsideMiddle(n - 1, t).Right()); t -= bt; if (t == 0) return Flip(p - Y); else if (t == 1) return p + X; t -= 2; if (t < bt) return Flip(p + FindInsideMiddle(n - 1, t).Right()).MirrorHoriz(); t -= bt; if (t == 0) return Flip(p + Y); return Flip(p - FindBetween<U, U>(n - 1, t)); } template <int A> ll Middle(int n) { if (n == 0 || A == T || A == U) return 0; else if (A == L) return Outside(n); else return Inside(n); } template <int A> Point FindMiddle(int n, ll t) { if (n == 0 || A == T || A == U || t == 0) return X; else if (A == L) return FindOutside(n, t); else return FindInside(n, t); } template <int A, int B> Res Between(int n) { if (n == 0) { Res res; res.up = true; res.t = 0; return res; } static Res cache[MAX_N + 1]; Res& res = cache[n]; if (res.t >= 0) return res; Res a = Between<LowerLeft(A), LowerLeft(B)>(n - 1); ll mt = a.up ? Middle<Right(A)>(n) : Middle<Right(B)>(n); Res b = a.up ? Between<LowerRight(B), LowerRight(A)>(n - 1) : Between<LowerRight(A), LowerRight(B)>(n - 1); res.up = !b.up; res.t = a.t + 1 + mt + 1 + b.t; return res; } template <int A, int B> Point FindBetween(int n, ll t) { if (t == 0) return X; Res a = Between<LowerLeft(A), LowerLeft(B)>(n - 1); if (t < a.t) return FindBetween<LowerLeft(A), LowerLeft(B)>(n - 1, t); Point p = After(n - 1); if (t == a.t) return p - X; t -= a.t + 1; ll mt = a.up ? Middle<Right(A)>(n) : Middle<Right(B)>(n); if (t < mt) return p + (a.up ? FindMiddle<Right(A)>(n, t).MirrorHoriz().Left() : FindMiddle<Right(B)>(n, t).Right()); if (t == mt) return a.up ? p + Y : p - Y; t -= mt + 1; return p + (a.up ? FindBetween<LowerRight(B), LowerRight(A)>(n - 1, t).MirrorHoriz() : FindBetween<LowerRight(A), LowerRight(B)>(n - 1, t)); } template <int A> Point Find(int n, ll t) { if (t == 0) return X; Res a = Between<A, A>(n); if (t < a.t) { Point q = FindBetween<A, A>(n, t); q.y = abs(q.y); return q; } Point p = After(n); if (t == a.t) return p - X; t -= a.t + 1; return p + Find<Right(A)>(n, t).Left(); } int main() { int n, z; scanf("%d%d", &n, &z); for (; z > 0; z--) { ll t; scanf("%lld", &t); Point p = Find<T>(n, t); printf("%d %d\n", p.x, p.y); } 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 | #include <cstdio> #include <cmath> using namespace std; static const int MAX_N = 30; typedef long long ll; constexpr int T = 0; constexpr int L = 1; constexpr int U = 2; constexpr int R = 3; constexpr int Left(int rot) { return (rot + 1) % 4; } constexpr int Right(int rot) { return (rot + 3) % 4; } constexpr int MirrorHoriz(int rot) { return rot % 2 == 0 ? rot ^ 2 : rot; } constexpr int MirrorVert(int rot) { return rot % 2 == 1 ? rot ^ 2 : rot; } // T -> R, L -> L, U -> U, R -> T constexpr int LowerLeft(int rot) { return rot == T || rot == R ? rot ^ 3 : rot; } // T -> L, L -> T, U -> U, R -> R constexpr int LowerRight(int rot) { return rot < 2 ? rot ^ 1 : rot; } struct Point { int x, y; Point() : x(0), y(0) {} Point(int x, int y) : x(x), y(y) {} Point MirrorHoriz() const { return {x, -y}; } Point MirrorVert() const { return {-x, y}; } Point Left() const { return {-y, x}; } template <int rot> Point Rotate() const { return rot ? Left().Rotate<::Right(rot)>() : *this; } Point Right() const { return Rotate<R>(); } void operator+=(Point p) { x += p.x; y += p.y; } }; Point operator+(Point a, Point b) { return {a.x + b.x, a.y + b.y}; } Point operator-(Point a, Point b) { return {a.x - b.x, a.y - b.y}; } const Point X(1, 0); const Point Y(0, 1); Point After(int n) { return {1 << (n + 1), 0}; } struct Res { bool up; int t; Res() : t(-1) {} }; template <int A, int B> Res Between(int n); template <int A, int B> Point FindBetween(int n, ll t); template <int A, int B> ll BackForth(int n) { Res a = Between<A, B>(n); return a.t + (a.up ? Between<MirrorVert(A), MirrorVert(B)>(n) : Between<B, A>(n)).t; } template <int A, int B> Point FindBackForth(int n, ll t) { if (t == 0) return X; Res a = Between<A, B>(n); if (t < a.t) return FindBetween<A, B>(n, t); Point p = After(n); if (t == a.t) return p - X; t -= a.t; return p - (a.up ? FindBetween<MirrorVert(A), MirrorVert(B)>(n, t) : FindBetween<B, A>(n, t)).MirrorHoriz(); } ll Outside(int n) { return BackForth<L, R>(n - 1); } Point FindOutside(int n, ll t) { return FindBackForth<L, R>(n - 1, t); } ll InsideMiddle(int n) { return BackForth<T, L>(n); } ll Inside(int n) { return 2 * Between<U, U>(n - 1).t + 4 + 2 * InsideMiddle(n - 1); } Point FindInsideMiddle(int n, ll t) { return FindBackForth<T, L>(n, t); } Point FindInside(int n, ll t) { Res a = Between<U, U>(n - 1); if (t < a.t) return FindBetween<U, U>(n - 1, t); Point p = After(n - 1); if (t == a.t) return p - X; t -= a.t + 1; auto Flip = [=](Point p) { return a.up ? p.MirrorHoriz() : p; }; ll bt = InsideMiddle(n - 1); if (t < bt) return Flip(p + FindInsideMiddle(n - 1, t).Right()); t -= bt; if (t == 0) return Flip(p - Y); else if (t == 1) return p + X; t -= 2; if (t < bt) return Flip(p + FindInsideMiddle(n - 1, t).Right()).MirrorHoriz(); t -= bt; if (t == 0) return Flip(p + Y); return Flip(p - FindBetween<U, U>(n - 1, t)); } template <int A> ll Middle(int n) { if (n == 0 || A == T || A == U) return 0; else if (A == L) return Outside(n); else return Inside(n); } template <int A> Point FindMiddle(int n, ll t) { if (n == 0 || A == T || A == U || t == 0) return X; else if (A == L) return FindOutside(n, t); else return FindInside(n, t); } template <int A, int B> Res Between(int n) { if (n == 0) { Res res; res.up = true; res.t = 0; return res; } static Res cache[MAX_N + 1]; Res& res = cache[n]; if (res.t >= 0) return res; Res a = Between<LowerLeft(A), LowerLeft(B)>(n - 1); ll mt = a.up ? Middle<Right(A)>(n) : Middle<Right(B)>(n); Res b = a.up ? Between<LowerRight(B), LowerRight(A)>(n - 1) : Between<LowerRight(A), LowerRight(B)>(n - 1); res.up = !b.up; res.t = a.t + 1 + mt + 1 + b.t; return res; } template <int A, int B> Point FindBetween(int n, ll t) { if (t == 0) return X; Res a = Between<LowerLeft(A), LowerLeft(B)>(n - 1); if (t < a.t) return FindBetween<LowerLeft(A), LowerLeft(B)>(n - 1, t); Point p = After(n - 1); if (t == a.t) return p - X; t -= a.t + 1; ll mt = a.up ? Middle<Right(A)>(n) : Middle<Right(B)>(n); if (t < mt) return p + (a.up ? FindMiddle<Right(A)>(n, t).MirrorHoriz().Left() : FindMiddle<Right(B)>(n, t).Right()); if (t == mt) return a.up ? p + Y : p - Y; t -= mt + 1; return p + (a.up ? FindBetween<LowerRight(B), LowerRight(A)>(n - 1, t).MirrorHoriz() : FindBetween<LowerRight(A), LowerRight(B)>(n - 1, t)); } template <int A> Point Find(int n, ll t) { if (t == 0) return X; Res a = Between<A, A>(n); if (t < a.t) { Point q = FindBetween<A, A>(n, t); q.y = abs(q.y); return q; } Point p = After(n); if (t == a.t) return p - X; t -= a.t + 1; return p + Find<Right(A)>(n, t).Left(); } int main() { int n, z; scanf("%d%d", &n, &z); for (; z > 0; z--) { ll t; scanf("%lld", &t); Point p = Find<T>(n, t); printf("%d %d\n", p.x, p.y); } return 0; } |