/*------------------------------------------------------------------------------ * problem025.cc: Ant on a Chessboard (12.6.1) **----------------------------------------------------------------------------*/ /* Copyright (c) 2009 Peter Bui. All Rights Reserved. * * Peter Bui * **------------------------------------------------------------------------------ * Includes **----------------------------------------------------------------------------*/ #include #include #include #include #include using namespace std; /*------------------------------------------------------------------------------ * Constants **----------------------------------------------------------------------------*/ #define NPOWERS 45000 /*------------------------------------------------------------------------------ * Globals **----------------------------------------------------------------------------*/ static vector Powers(NPOWERS); /*------------------------------------------------------------------------------ * Functions **----------------------------------------------------------------------------*/ static void generate_powers(int n) { int i; for (i = 0; i < n; i++) Powers[i] = i * i; } static void compute_position(int n, int &c, int &r) { int p, s; p = int(lower_bound(Powers.begin(), Powers.end(), n) - Powers.begin()); for (s = 0; s < (p+p); s++) if (n == (Powers[p] - s)) break; if (p % 2) { // Odd, go right, and down c = 1 + (s < p ? s : p - 1); r = p - (s < p ? 0 : s - p + 1); } else { // Even, go up, and left c = p - (s < p ? 0 : s - p + 1); r = 1 + (s < p ? s : p - 1); } } /*------------------------------------------------------------------------------ * Main Execution **----------------------------------------------------------------------------*/ int main(int argc, char* argv[]) { int n, c, r; generate_powers(NPOWERS); while (cin >> n) { compute_position(n, c, r); cout << c << ' ' << r << endl; } return (EXIT_SUCCESS); } /*------------------------------------------------------------------------------ * vim: sts=4 sw=4 ts=8 ft=cpp **----------------------------------------------------------------------------*/