#include <bits/stdc++.h> #define REP(a,b) for(int a=0; a<(b); ++a) #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define ALL(a) (a).begin(), (a).end() #define SIZE(a) ((int)(a).size()) #define VAR(x) #x ": " << x << " " #define popcount __builtin_popcount #define popcountll __builtin_popcountll #define gcd __gcd #define x first #define y second #define st first #define nd second #define pb push_back using namespace std; template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; } template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; } typedef long long LL; typedef pair<LL, LL> PLL; typedef long double K; typedef vector<int> VI; const int dx[] = {0,0,-1,1}; //1,1,-1,1}; const int dy[] = {-1,1,0,0}; //1,-1,1,-1}; PLL L0[] = { {1,0}, {2,1}, {3,2}, {2,3}, {1,2}, {2,1}, {3,0}, {4,1}, {3,2}, {4,3}, {3,4}, {2,3}, {1,4}, {0,3}, {1,2}, {0,1} }; PLL N0[] = { {3,4}, {2,5}, {1,6}, {2,7}, {3,6}, {2,5}, {1,4}, {2,3}, {3,4} }; PLL EI[] = { {0,0}, {-1,1}, {-2,2}, {-1,3}, {0,2}, {-1,1}, {-2,0}, {-3,-1}, {-4,0}, {-5,1}, {-6,0}, {-7,-1}, {-8,0}, {-9,1}, {-10, 0} }; PLL OI[] = { {0,0}, {-1,1}, {-2,0}, {-3,-1}, {-4,0}, {-5,1}, {-6,0}, {-7,-1}, {-8,0}, {-9,1}, {-10, 2}, {-9,3}, {-8,2}, {-9,1}, {-10, 0} }; LL B[33]; LL S[33]; LL P0[33]; LL P1[33]; LL P2[33]; LL P3[33]; LL P4[33]; LL P5[33]; LL M[33]; LL MB[33]; LL IL[33]; LL ILD[33]; LL ILDR[33]; PLL rot_clock(PLL p, LL k){ return PLL(p.y, k-p.x); } PLL rot_counter(PLL p, LL k){ return PLL(k-p.y, p.x); } PLL mirror_vert(PLL p, LL x){ return PLL(2*x-p.x, p.y); } PLL solve(int n, LL t); PLL do_intro(LL t, bool even){ //printf("do_intro %lld %d\n", t, even?0:1); if(t <= 14){ if(even) return EI[t]; return OI[t]; } int i = 0; while(t >= ILDR[i]) ++i; t -= ILDR[i-1]; LL p = ILD[i-1]; //printf("%lld %lld\n", p, t); if(t <= 2+P5[i]){ //printf("mid\n"); PLL r = solve(i, t + P0[i]); r.x = B[i-1]-1-r.x - 2*p; r.y = -r.y; return r; } p += 1; t -= 2+P5[i]; //printf("%lld\n", t); PLL r = do_intro(t, even); r.x -= 2*p; //printf("ret %lld %lld\n", r.x, r.y); return r; } PLL intro(int n, LL t){ assert(n >= 1); return do_intro(t, (n%2==0)); } PLL nest(int n, LL t){ assert(n > 1); if(n == 2){ assert(t <= 9); return N0[t]; } if(t <= P0[n-1]){ PLL r = intro(n-2, t); r.x += B[n-1] - 1; r.y += B[n-1]; return r; } t -= P0[n-1]; if(t == 1) return PLL(B[n-1]/2, B[n-1]-1); t -= 2; if(t <= P4[n-1]){ PLL r = nest(n-1, P4[n-1] - t); r.y += B[n-1]/2; r.y = B[n] - r.y; return r; } t -= P4[n-1]+1; if(t <= P5[n-1]){ t += P0[n-1]+1; PLL r = solve(n-1, t); r.y += B[n-1]; return r; } t -= P5[n-1]+1; assert(t <= P0[n-1]); PLL r = intro(n-2, t); r.x += B[n-1] - 1; r.y += B[n-1]; r.x = 2*(B[n-1] - B[n-1]/4) - r.x; r.y = 2*B[n-1] - r.y; return r; } bool mirror(LL t){ //printf("MIRROR %lld?\n", t); if(t <= 4){ //printf("simple\n"); if(t <= 2) return 0; return 1; } int i = 1; while(i != 31 && t >= MB[i]) ++i; t -= MB[i-1]; if(t <= M[i]){ //printf("mid %d\n", i); return (i > 1); } t -= M[i]; if(i == 1) return !mirror(t); return mirror(t); } PLL solve(int n, LL t){ assert(t < S[n]); if(n == 1) return L0[t]; if(t <= P1[n-1]){ PLL r = solve(n-1, t+P3[n-1]); r = rot_clock(r, B[n-1]); if(r.x == B[n-1]-1 && r.y % 4 == 2) r.x += 2; return r; } t -= P1[n-1]; if(t <= P4[n]){ PLL r = nest(n, t); return r; } t -= P4[n]; if(t == 1) return PLL(B[n-1], B[n-1]+1); --t; LL h = P1[n-1] + P4[n] + 1; if(t <= h){ PLL r = solve(n, h-t); return mirror_vert(r, B[n-1]); } t -= h; if(t == 1){ return PLL(B[n], 1); } t -= 1; if(t <= P3[n-1]-1){ PLL r = solve(n-1, t); r = rot_counter(r, B[n-1]); r.x += B[n-1]; return r; } t -= P3[n-1]-1; if(t == 1) return PLL(B[n]-1, B[n]/2); t -= 2; if(t <= P1[n-1]){ PLL r = solve(n-1, t+P3[n-1]); r.x += B[n-1]; r.y += B[n-1]; return r; } t -= P1[n-1]; if(t <= P0[n]){ bool mir = mirror(t); t = P0[n] - t; PLL r = solve(n-1, t+P3[n-1]); r.y += B[n-1]; if(mir) r.x = B[n] - r.x; return r; } t -= P0[n] - 1; PLL r = solve(n, P0[n] + P1[n-1] + 2 + P3[n-1] + 1 + 1 + h + P4[n] + P1[n-1] - t); r.x = B[n] - r.x; return r; } int main(){ B[1] = 4; S[1] = 16; P0[1] = 0; P1[1] = 6; P2[1] = -2; P3[1] = 7; P4[1] = 0; P5[1] = 4; M[1] = 6; MB[0] = 4; MB[1] = 14; IL[1] = 1; ILD[1] = 5; ILDR[1] = 14; FWD(i,2,31){ B[i] = B[i-1] * 2; S[i] = S[i-1] * 4; P0[i] = P0[i-1] * 4 + 2; P1[i] = P1[i-1] * 4 - 2; P2[i] = P2[i-1] * 4 + 8; P3[i] = P3[i-1] * 4 + 3; P4[i] = P4[i-1] * 4 + 8; P5[i] = P5[i-1] * 4 + 8; M[i] = M[i-1] * 4 + 2; MB[i] = M[i] + 2*MB[i-1]; IL[i] = 2*IL[i-1] + 1; ILD[i] = 2*ILD[i-1] + 1; ILDR[i] = 2*ILDR[i-1] + 2 + P5[i]; } //FWD(i,1,31) printf("%d: %lld %lld\n", i, M[i], MB[i]); /*FWD(i,1,31) printf("%d: %lld %lld %lld %lld %lld %lld %lld %lld\n", i, B[i], S[i], P0[i], P1[i], P2[i], P3[i], P4[i], P5[i]);*/ int n, z; scanf("%d %d", &n, &z); while(z--){ LL t; scanf("%lld", &t); PLL r = solve(n, t); printf("%lld %lld\n", r.x, r.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 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 | #include <bits/stdc++.h> #define REP(a,b) for(int a=0; a<(b); ++a) #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define ALL(a) (a).begin(), (a).end() #define SIZE(a) ((int)(a).size()) #define VAR(x) #x ": " << x << " " #define popcount __builtin_popcount #define popcountll __builtin_popcountll #define gcd __gcd #define x first #define y second #define st first #define nd second #define pb push_back using namespace std; template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; } template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; } typedef long long LL; typedef pair<LL, LL> PLL; typedef long double K; typedef vector<int> VI; const int dx[] = {0,0,-1,1}; //1,1,-1,1}; const int dy[] = {-1,1,0,0}; //1,-1,1,-1}; PLL L0[] = { {1,0}, {2,1}, {3,2}, {2,3}, {1,2}, {2,1}, {3,0}, {4,1}, {3,2}, {4,3}, {3,4}, {2,3}, {1,4}, {0,3}, {1,2}, {0,1} }; PLL N0[] = { {3,4}, {2,5}, {1,6}, {2,7}, {3,6}, {2,5}, {1,4}, {2,3}, {3,4} }; PLL EI[] = { {0,0}, {-1,1}, {-2,2}, {-1,3}, {0,2}, {-1,1}, {-2,0}, {-3,-1}, {-4,0}, {-5,1}, {-6,0}, {-7,-1}, {-8,0}, {-9,1}, {-10, 0} }; PLL OI[] = { {0,0}, {-1,1}, {-2,0}, {-3,-1}, {-4,0}, {-5,1}, {-6,0}, {-7,-1}, {-8,0}, {-9,1}, {-10, 2}, {-9,3}, {-8,2}, {-9,1}, {-10, 0} }; LL B[33]; LL S[33]; LL P0[33]; LL P1[33]; LL P2[33]; LL P3[33]; LL P4[33]; LL P5[33]; LL M[33]; LL MB[33]; LL IL[33]; LL ILD[33]; LL ILDR[33]; PLL rot_clock(PLL p, LL k){ return PLL(p.y, k-p.x); } PLL rot_counter(PLL p, LL k){ return PLL(k-p.y, p.x); } PLL mirror_vert(PLL p, LL x){ return PLL(2*x-p.x, p.y); } PLL solve(int n, LL t); PLL do_intro(LL t, bool even){ //printf("do_intro %lld %d\n", t, even?0:1); if(t <= 14){ if(even) return EI[t]; return OI[t]; } int i = 0; while(t >= ILDR[i]) ++i; t -= ILDR[i-1]; LL p = ILD[i-1]; //printf("%lld %lld\n", p, t); if(t <= 2+P5[i]){ //printf("mid\n"); PLL r = solve(i, t + P0[i]); r.x = B[i-1]-1-r.x - 2*p; r.y = -r.y; return r; } p += 1; t -= 2+P5[i]; //printf("%lld\n", t); PLL r = do_intro(t, even); r.x -= 2*p; //printf("ret %lld %lld\n", r.x, r.y); return r; } PLL intro(int n, LL t){ assert(n >= 1); return do_intro(t, (n%2==0)); } PLL nest(int n, LL t){ assert(n > 1); if(n == 2){ assert(t <= 9); return N0[t]; } if(t <= P0[n-1]){ PLL r = intro(n-2, t); r.x += B[n-1] - 1; r.y += B[n-1]; return r; } t -= P0[n-1]; if(t == 1) return PLL(B[n-1]/2, B[n-1]-1); t -= 2; if(t <= P4[n-1]){ PLL r = nest(n-1, P4[n-1] - t); r.y += B[n-1]/2; r.y = B[n] - r.y; return r; } t -= P4[n-1]+1; if(t <= P5[n-1]){ t += P0[n-1]+1; PLL r = solve(n-1, t); r.y += B[n-1]; return r; } t -= P5[n-1]+1; assert(t <= P0[n-1]); PLL r = intro(n-2, t); r.x += B[n-1] - 1; r.y += B[n-1]; r.x = 2*(B[n-1] - B[n-1]/4) - r.x; r.y = 2*B[n-1] - r.y; return r; } bool mirror(LL t){ //printf("MIRROR %lld?\n", t); if(t <= 4){ //printf("simple\n"); if(t <= 2) return 0; return 1; } int i = 1; while(i != 31 && t >= MB[i]) ++i; t -= MB[i-1]; if(t <= M[i]){ //printf("mid %d\n", i); return (i > 1); } t -= M[i]; if(i == 1) return !mirror(t); return mirror(t); } PLL solve(int n, LL t){ assert(t < S[n]); if(n == 1) return L0[t]; if(t <= P1[n-1]){ PLL r = solve(n-1, t+P3[n-1]); r = rot_clock(r, B[n-1]); if(r.x == B[n-1]-1 && r.y % 4 == 2) r.x += 2; return r; } t -= P1[n-1]; if(t <= P4[n]){ PLL r = nest(n, t); return r; } t -= P4[n]; if(t == 1) return PLL(B[n-1], B[n-1]+1); --t; LL h = P1[n-1] + P4[n] + 1; if(t <= h){ PLL r = solve(n, h-t); return mirror_vert(r, B[n-1]); } t -= h; if(t == 1){ return PLL(B[n], 1); } t -= 1; if(t <= P3[n-1]-1){ PLL r = solve(n-1, t); r = rot_counter(r, B[n-1]); r.x += B[n-1]; return r; } t -= P3[n-1]-1; if(t == 1) return PLL(B[n]-1, B[n]/2); t -= 2; if(t <= P1[n-1]){ PLL r = solve(n-1, t+P3[n-1]); r.x += B[n-1]; r.y += B[n-1]; return r; } t -= P1[n-1]; if(t <= P0[n]){ bool mir = mirror(t); t = P0[n] - t; PLL r = solve(n-1, t+P3[n-1]); r.y += B[n-1]; if(mir) r.x = B[n] - r.x; return r; } t -= P0[n] - 1; PLL r = solve(n, P0[n] + P1[n-1] + 2 + P3[n-1] + 1 + 1 + h + P4[n] + P1[n-1] - t); r.x = B[n] - r.x; return r; } int main(){ B[1] = 4; S[1] = 16; P0[1] = 0; P1[1] = 6; P2[1] = -2; P3[1] = 7; P4[1] = 0; P5[1] = 4; M[1] = 6; MB[0] = 4; MB[1] = 14; IL[1] = 1; ILD[1] = 5; ILDR[1] = 14; FWD(i,2,31){ B[i] = B[i-1] * 2; S[i] = S[i-1] * 4; P0[i] = P0[i-1] * 4 + 2; P1[i] = P1[i-1] * 4 - 2; P2[i] = P2[i-1] * 4 + 8; P3[i] = P3[i-1] * 4 + 3; P4[i] = P4[i-1] * 4 + 8; P5[i] = P5[i-1] * 4 + 8; M[i] = M[i-1] * 4 + 2; MB[i] = M[i] + 2*MB[i-1]; IL[i] = 2*IL[i-1] + 1; ILD[i] = 2*ILD[i-1] + 1; ILDR[i] = 2*ILDR[i-1] + 2 + P5[i]; } //FWD(i,1,31) printf("%d: %lld %lld\n", i, M[i], MB[i]); /*FWD(i,1,31) printf("%d: %lld %lld %lld %lld %lld %lld %lld %lld\n", i, B[i], S[i], P0[i], P1[i], P2[i], P3[i], P4[i], P5[i]);*/ int n, z; scanf("%d %d", &n, &z); while(z--){ LL t; scanf("%lld", &t); PLL r = solve(n, t); printf("%lld %lld\n", r.x, r.y); } return 0; } |