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

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
const double EPS = 1e-10;
const double INF = 1e12;
const double PI = acos(-1);

struct Point {
  double x, y, z;
  Point(double x, double y, double z) : x(x), y(y), z(z) {}
  Point() {}
  Point &operator+=(const Point &a) {
    x += a.x;
    y += a.y;
    z += a.z;
    return *this;
  }
  Point &operator-=(const Point &a) {
    x -= a.x;
    y -= a.y;
    z -= a.z;
    return *this;
  }
  Point &operator*=(const double &a) {
    x *= a;
    y *= a;
    z *= a;
    return *this;
  }
  Point operator+(const Point &a) const {
    return Point(x + a.x, y + a.y, z + a.z);
  }
  Point operator-(const Point &a) const {
    return Point(x - a.x, y - a.y, z - a.z);
  }
  Point operator*(const double &a) const { return Point(a * x, a * y, a * z); }
  Point operator-() { return Point(-x, -y, -z); }
};

double dot(const Point &a, const Point &b) {
  return a.x * b.x + a.y * b.y + a.z * b.z;
}
Point cross(const Point &a, const Point &b) {
  return Point(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z,
               a.x * b.y - a.y * b.x);
}
double abs(const Point &a) { return sqrt(dot(a, a)); }
Point unit(const Point &a) { return a * (1 / abs(a)); }
bool operator==(const Point &a, const Point &b) { return abs(a - b) < EPS; }

Point projectionLP3(Point p, Point q, Point c) {
  double t = dot(unit(q - p), c - p);
  return p + unit(q - p) * t;
}
double distanceSP3(Point p, Point q, Point c) {
  Point r = projectionLP3(p, q, c);
  if (dot(q - p, r - p) < 0)
    return abs(c - p);
  if (dot(p - q, r - q) < 0)
    return abs(c - q);
  return abs(c - r);
}

pair<Point, Point> calcpos(double t, double m) {
  using P2d = complex<double>;
  double arg1 = m * PI / 180 + t / 2500000 * 2 * PI;
  P2d moon2d = P2d(380000, 0) * P2d(cos(arg1), sin(arg1));
  Point moon = Point(moon2d.real(), moon2d.imag(), 0);
  double arg2 = t / 7200 * 2 * PI;
  P2d kaguya2d = P2d(0, 1900) * P2d(cos(arg2), sin(arg2));
  Point kaguya = moon + Point(kaguya2d.real(), 0, kaguya2d.imag());
  return make_pair(moon, kaguya);
}

int main() {
  cout << fixed << setprecision(3);
  double m, t;
  cin >> m >> t;
  t *= 60;
  double dt = 1e-1;
  double ans = 0;
  Point origin(0, 0, 0);
  for (double time = dt / 2; time < t; time += dt) {
    pair<Point, Point> pos = calcpos(time, m);
    if (distanceSP3(origin, pos.second, pos.first) < 1800) {
      ans += dt;
    }
  }
  cout << fixed << setprecision(3);
  cout << ans / 60 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
const double EPS = 1e-10;
const double INF = 1e12;
const double PI = acos(-1);

struct Point {
  double x, y, z;
  Point(double x, double y, double z) : x(x), y(y), z(z) {}
  Point() {}
  Point &operator+=(const Point &a) {
    x += a.x;
    y += a.y;
    z += a.z;
    return *this;
  }
  Point &operator-=(const Point &a) {
    x -= a.x;
    y -= a.y;
    z -= a.z;
    return *this;
  }
  Point &operator*=(const double &a) {
    x *= a;
    y *= a;
    z *= a;
    return *this;
  }
  Point operator+(const Point &a) const {
    return Point(x + a.x, y + a.y, z + a.z);
  }
  Point operator-(const Point &a) const {
    return Point(x - a.x, y - a.y, z - a.z);
  }
  Point operator*(const double &a) const { return Point(a * x, a * y, a * z); }
  Point operator-() { return Point(-x, -y, -z); }
};

double dot(const Point &a, const Point &b) {
  return a.x * b.x + a.y * b.y + a.z * b.z;
}
Point cross(const Point &a, const Point &b) {
  return Point(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z,
               a.x * b.y - a.y * b.x);
}
double abs(const Point &a) { return sqrt(dot(a, a)); }
Point unit(const Point &a) { return a * (1 / abs(a)); }
bool operator==(const Point &a, const Point &b) { return abs(a - b) < EPS; }

Point projectionLP3(Point p, Point q, Point c) {
  double t = dot(unit(q - p), c - p);
  return p + unit(q - p) * t;
}
double distanceSP3(Point p, Point q, Point c) {
  Point r = projectionLP3(p, q, c);
  if (dot(q - p, r - p) < 0)
    return abs(c - p);
  if (dot(p - q, r - q) < 0)
    return abs(c - q);
  return abs(c - r);
}

pair<Point, Point> calcpos(double t, double m) {
  using P2d = complex<double>;
  double arg1 = m * PI / 180 + t / 2500000 * 2 * PI;
  P2d moon2d = P2d(380000, 0) * P2d(cos(arg1), sin(arg1));
  Point moon = Point(moon2d.real(), moon2d.imag(), 0);
  double arg2 = t / 7200 * 2 * PI;
  P2d kaguya2d = P2d(0, 1900) * P2d(cos(arg2), sin(arg2));
  Point kaguya = moon + Point(kaguya2d.real(), 0, kaguya2d.imag());
  return make_pair(moon, kaguya);
}

int main() {
  cout << fixed << setprecision(3);
  double m, t;
  cin >> m >> t;
  t *= 60;
  double dt = 1e-2;
  double ans = 0;
  Point origin(0, 0, 0);
  for (double time = dt / 2; time < t; time += dt) {
    pair<Point, Point> pos = calcpos(time, m);
    if (distanceSP3(origin, pos.second, pos.first) < 1800) {
      ans += dt;
    }
  }
  cout << fixed << setprecision(3);
  cout << ans / 60 << endl;
  return 0;
}