Problem p02963 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * @brief  : c++ code for AtCoder
 * @author : rk222
 * @created: 2019.07.21 20:55:19
 */

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))

#define disp(x) cout << #x << ": " << x << endl
#define disp_vec(v)                                                            \
  cout << #v << ":";                                                           \
  rep(i, v.size()) cout << " " << v[i];                                        \
  cout << endl

const int INF = 100000000;
const int M = 100000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

/* ------------------------------------- */

int main() {
  ll s;
  scanf("%lld", &s);
  ll l = ((ll)sqrt(s - 1LL)) + 1LL;
  // if (s >= 90000000000000000) {
  //     l--;
  // }
  ll n = l * l - s;
  // printf("%lld %lld %lld\n", n, l, s);
  if (s == 1LL) {
    printf("0 0 1 0 0 1\n");
    return 0;
  }
  if (s == 1000000000000000000LL) {
    printf("0 0 1000000000 0  0 1000000000\n");
    return 0;
  }
baaaa:
  ll a = 0LL;
  ll b;
  ll sqn = sqrt(n);
  while (1) {
    if (a > l || a > sqn) {
      break;
    }
    if (a == 0) {
      if (n == 0) {
        b = 0;
        break;
      } else {
        a++;
        continue;
      }
    }

    if (n % a == 0 && n / a <= l) {
      b = n / a;
      break;
    } else {
      a++;
    }
    // printf("%lld\n", a);
  }
  if (l - a < 0 || l - b < 0) {
    l--;
    n = l * l - s;
    goto baaaa;
  }

  printf("%lld %lld %lld %lld %lld %lld\n", l - a, 0LL, 0LL, l - b, l, l);
  // printf("%lld\n", 2LL * l * l - l * (a + b) - (l - a) * (l - b));

  /* --------------------------------- */
  return 0;
}

Fixed submission:
/**
 * @brief  : c++ code for AtCoder
 * @author : rk222
 * @created: 2019.07.21 20:55:19
 */

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))

#define disp(x) cout << #x << ": " << x << endl
#define disp_vec(v)                                                            \
  cout << #v << ":";                                                           \
  rep(i, v.size()) cout << " " << v[i];                                        \
  cout << endl

const int INF = 100000000;
const int M = 100000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

/* ------------------------------------- */

int main() {
  ll s;
  scanf("%lld", &s);
  ll l = ((ll)sqrt(s - 1LL)) + 1LL;
  // if (s >= 90000000000000000) {
  //     l--;
  // }
  ll n = l * l - s;
  // printf("%lld %lld %lld\n", n, l, s);
  if (s == 1LL) {
    printf("0 0 1 0 0 1\n");
    return 0;
  }
  if (s == 1000000000000000000LL) {
    printf("0 0 1000000000 0  0 1000000000\n");
    return 0;
  }
baaaa:
  ll a = 0LL;
  ll b;
  ll sqn = sqrt(n);
  while (1) {
    if (a > l || a > sqn) {
      break;
    }
    if (a == 0) {
      if (n == 0) {
        b = 0;
        break;
      } else {
        a++;
        continue;
      }
    }

    if (n % a == 0 && n / a <= l) {
      b = n / a;
      break;
    } else {
      a++;
    }
    // printf("%lld\n", a);
  }
  if (l - a < 0 || l - b < 0) {
    l++;
    n = l * l - s;
    goto baaaa;
  }

  printf("%lld %lld %lld %lld %lld %lld\n", l - a, 0LL, 0LL, l - b, l, l);
  // printf("%lld\n", 2LL * l * l - l * (a + b) - (l - a) * (l - b));

  /* --------------------------------- */
  return 0;
}