Problem p01591 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define sgn(v) (abs((v)) < eps ? 0 : ((v) < 0 ? -1 : 1))
#define sqr(v) ((v) * (v))
#define tri(v) ((v) * (v) * (v))
#define MP make_pair
#define PB push_back
#define FI first
#define SE second
#define debug 0
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<double, int> PDI;
typedef pair<double, double> PDD;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<LL, LL> PLL;
const double eps = 1e-8;
const double pi = acos(-1);
using namespace std;
const int maxn = 1e4 + 100;
double x[maxn], y[maxn];
const double inf = 1e100;
int main() {
  int n, i;
  scanf("%d", &n);
  double sumx, sumy, sqrx, sqry, sqrxy, triy, trix;
  double sumxy = 0, xxy = 0, xyy = 0;
  sumx = sumy = sqrx = sqry = sqrxy = 0;
  trix = triy = 0;
  for (i = 0; i < n; ++i) {
    scanf("%lf%lf", &x[i], &y[i]);
    sumxy += (x[i] * y[i]);
    sumx += x[i];
    sumy += y[i];
    xyy += x[i] * sqr(y[i]);
    xxy += sqr(x[i]) * y[i];
    sqrx += sqr(x[i]);
    sqry += sqr(y[i]);
    sqrxy += (sqr(x[i]) + sqr(y[i]));
    triy += tri(y[i]);
    trix += tri(x[i]);
  }
  double C = n * sqrx - sumx * sumx;
  double D = n * sumxy - sumx * sumy;
  double E = n * trix + n * xyy - sqrxy * sumx;
  double G = n * sqry - sumy * sumy;
  double H = n * xxy + n * triy - sqrxy * sumy;
  double a, b, c;
  a = (H * D - E * G) / (C * G - sqr(D));
  b = (H * C - E * D) / (sqr(D) - G * C);
  c = -(sqrxy + a * sumx + b * sumy) / n;
  printf("%.3f %.3f %.3f\n", a, b, c);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define sgn(v) (abs((v)) < eps ? 0 : ((v) < 0 ? -1 : 1))
#define sqr(v) ((v) * (v))
#define tri(v) ((v) * (v) * (v))
#define MP make_pair
#define PB push_back
#define FI first
#define SE second
#define debug 0
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<double, int> PDI;
typedef pair<double, double> PDD;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<LL, LL> PLL;
const double eps = 1e-8;
const double pi = acos(-1);
using namespace std;
const int maxn = 4e4 + 100;
double x[maxn], y[maxn];
const double inf = 1e100;
int main() {
  int n, i;
  scanf("%d", &n);
  double sumx, sumy, sqrx, sqry, sqrxy, triy, trix;
  double sumxy = 0, xxy = 0, xyy = 0;
  sumx = sumy = sqrx = sqry = sqrxy = 0;
  trix = triy = 0;
  for (i = 0; i < n; ++i) {
    scanf("%lf%lf", &x[i], &y[i]);
    sumxy += (x[i] * y[i]);
    sumx += x[i];
    sumy += y[i];
    xyy += x[i] * sqr(y[i]);
    xxy += sqr(x[i]) * y[i];
    sqrx += sqr(x[i]);
    sqry += sqr(y[i]);
    sqrxy += (sqr(x[i]) + sqr(y[i]));
    triy += tri(y[i]);
    trix += tri(x[i]);
  }
  double C = n * sqrx - sumx * sumx;
  double D = n * sumxy - sumx * sumy;
  double E = n * trix + n * xyy - sqrxy * sumx;
  double G = n * sqry - sumy * sumy;
  double H = n * xxy + n * triy - sqrxy * sumy;
  double a, b, c;
  a = (H * D - E * G) / (C * G - sqr(D));
  b = (H * C - E * D) / (sqr(D) - G * C);
  c = -(sqrxy + a * sumx + b * sumy) / n;
  printf("%.3f %.3f %.3f\n", a, b, c);
  return 0;
}