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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int P, Q, R;
double getx(double t) {
  return (double)(P - Q) * cos(t) + (double)R * cos(t * (P - Q) / Q);
}
double dx(double t) {
  return (double)(P - Q) * (-sin(t)) +
         (double)R * (P - Q) / Q * (-sin(t * (P - Q) / Q));
}
double gety(double t) {
  return (double)(P - Q) * sin(t) - (double)R * sin(t * (P - Q) / Q);
}
double dy(double t) {
  return (double)(P - Q) * (cos(t)) -
         (double)R * (P - Q) / Q * (cos(t * (P - Q) / Q));
}
double dist(double x1, double y1, double x2, double y2) {
  return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}

int main() {
  while (cin >> P >> Q >> R && P) {
    int time = -1;
    for (int k = 1;; k++) {
      if (R == 0) {
        time = 1;
        break;
      }
      if (k * (P - Q) % Q == 0) {
        time = k;
        break;
      }
    }
    const int ITER = 1000000;
    const double delta = 2.0 * time * M_PI / ITER;
    double ans = 0;
    REP(iter, ITER) {
      double t = delta * iter;
      ans += sqrt(dx(t) * dx(t) + dy(t) * dy(t)) * delta;
    }
    printf("%.2f\\n", ans + EPS);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int P, Q, R;
double getx(double t) {
  return (double)(P - Q) * cos(t) + R * cos(t * (P - Q) / Q);
}
double gety(double t) {
  return (double)(P - Q) * sin(t) - R * sin(t * (P - Q) / Q);
}
double dist(double x1, double y1, double x2, double y2) {
  return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}

int main() {
  while (cin >> P >> Q >> R && P) {
    int time = -1;
    for (int k = 1;; k++) {
      if (R == 0) {
        time = 1;
        break;
      }
      if (k * (P - Q) % Q == 0) {
        time = k;
        break;
      }
    }
    double t = 0;
    const int ITER = 10000000 / 2;
    const double delta = 2.0 * time * M_PI / ITER;
    double ans = 0;
    REP(iter, ITER) {
      ans += dist(getx(t), gety(t), getx(t + delta), gety(t + delta));
      t += delta;
    }
    printf("%.2f\\n", ans + EPS);
  }
  return 0;
}