Problem p00654 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
long long b[110000];
long long gz[550];
double eps = 1e-9;
int main() {
  int a;
  while (scanf("%d", &a), a) {
    long double A = 1;
    int sz = 0;
    for (int i = 0; i < (a + 1) * a / 2; i++) {
      scanf("%lld", b + i);
      if (b[i] % 2 == 0) {
        A *= pow(b[i], 1.0 / a);
        gz[sz++] = b[i];
      } else
        A *= pow(b[i], -1.0 / a / (a - 1));
    }
    long long gs = (long long)(A + 0.5);
    printf("%lld\\n", gs);
    std::sort(gz, gz + a);
    for (int i = 0; i < a; i++) {
      if (i)
        printf(" ");
      printf("%lld", gz[i] / gs);
    }
    printf("\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
long long gcd(long long a, long long b) {
  while (a) {
    b %= a;
    long long c = a;
    a = b;
    b = c;
  }
  return b;
}
long long b[110000];
long long c[550];
long long gz[550];
long long ks[110000];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    long long gs = -1;
    int sz = 0;
    for (int i = 0; i < (a + 1) * a / 2; i++) {
      scanf("%lld", b + i);
      if (b[i] % 2 == 0) {
        if (!~gs)
          gs = b[i];
        else
          gs = gcd(gs, b[i]);
        gz[sz++] = b[i];
      }
    }
    std::sort(b, b + (a + 1) * a / 2);
    long vk = 1;
    while (gs % 2 == 0) {
      gs /= 2;
      vk *= 2;
    }
    for (int i = 1; i * i <= gs; i++) {
      if (gs % i == 0) {
        if (i % 1 == 0) {
          long long G = i * vk;
          bool ok = true;
          for (int j = 0; j < sz; j++)
            c[j] = gz[j] / G;
          int ind = 0;
          for (int j = 0; j < sz; j++)
            ks[ind++] = gz[j];
          for (int j = 0; j < sz; j++)
            for (int k = j + 1; k < sz; k++)
              ks[ind++] = c[j] * c[k];
          std::sort(ks, ks + ind);
          for (int j = 0; j < ind; j++)
            if (b[j] != ks[j]) {
              ok = false;
              break;
            }
          if (ok) {
            std::sort(c, c + sz);
            printf("%lld\\n", G);
            for (int j = 0; j < sz; j++) {
              if (j)
                printf(" ");
              printf("%lld", c[j]);
            }
            printf("\\n");
            break;
          }
        }
        if ((gs / i) % 1 == 0) {
          long long G = gs / i * vk;
          bool ok = true;
          for (int j = 0; j < sz; j++)
            c[j] = gz[j] / G;
          int ind = 0;
          for (int j = 0; j < sz; j++)
            ks[ind++] = gz[j];
          for (int j = 0; j < sz; j++)
            for (int k = j + 1; k < sz; k++)
              ks[ind++] = c[j] * c[k];
          std::sort(ks, ks + ind);
          for (int j = 0; j < ind; j++)
            if (b[j] != ks[j]) {
              ok = false;
              break;
            }
          if (ok) {
            std::sort(c, c + sz);
            printf("%lld\\n", G);
            for (int j = 0; j < sz; j++) {
              if (j)
                printf(" ");
              printf("%lld", c[j]);
            }
            printf("\\n");
            break;
          }
        }
      }
    }
  }
}