#include #include #include #include #include using namespace std; long long GetIndex(long long x, long long y) { if (x>=y) { if (y<=0 && x<=1-y) { return 4*y*y - 3*y + x; } else { return 4*x*x - 3*x + y; } } else { if (y+x>=0) { return 4*y*y - y - x; } else { return 4*x*x - x - y; } } } #define MAX 500000000 class Leaper { public: static Init() { _visited = (int*)malloc(MAX * sizeof(int)); if (_visited==0) { cerr << "# out of memory" << endl; exit(1); } else { memset(_visited, 0, MAX * sizeof(int)); } } Leaper(long long n) : _n(n) { X = 0; Y = 0; Index = 0; } bool Move() { _visited[Index] = _n; _index = LLONG_MAX; Visit(X + _n, Y + 1); Visit(X - _n, Y + 1); Visit(X + 1, Y + _n); Visit(X + 1, Y - _n); Visit(X + _n, Y - 1); Visit(X - _n, Y - 1); Visit(X - 1, Y + _n); Visit(X - 1, Y - _n); if (_index < LLONG_MAX) { X = _x; Y = _y; Index = _index; return true; } else { return false; } } long long X; long long Y; long long Index; ~Leaper() { } static void Fini() { free(_visited); _visited = 0; } private: long long _n; void Visit(long long x, long long y) { long long index = GetIndex(x, y); if (index >= MAX) { cerr << "# reached the frontier" << endl; exit(2); } if (_index > index && _visited[index]!=_n) { _x = x; _y = y; _index = index; } } long long _x; long long _y; long long _index; static int *_visited; }; int *Leaper::_visited = 0; long long a(long long n) { if (n==1) { return -1; } else { Leaper leaper(n); while (leaper.Move()) { } return leaper.Index; } } int main() { Leaper::Init(); for (long long n=1; n<=1000; n++) { cout << n << " " << a(n) << endl; } Leaper::Fini(); return 0; }