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

#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbv(v)                                                                 \\
  cerr << #v << "=";                                                           \\
  for (auto _x : v)                                                            \\
    cerr << _x << ", ";                                                        \\
  cerr << endl
#define dba(a, n)                                                              \\
  cerr << #a << "=";                                                           \\
  for (int _i = 0; _i < (n); ++_i)                                             \\
    cerr << a[_i] << ", ";                                                     \\
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
ll solve(int W, int H, int K) {
  K = 2 * K - 2;
  ll ans = 0;
  for (int x = 1; x < W; ++x) {
    int lim = min(H - 1, (K + 2 * W) / (W - x));
    for (int y1 = 1; y1 <= lim; ++y1) {
      int rhs = K - (W - x) * y1 + __gcd(x, y1);
      // x * y2 <= rhs + (W, |y1-y2|) + (W-x, y2)
      int bound = min(H - 1, max(0, rhs / x));
      ans += bound;
      int lim2 = min(H - 1, (rhs + 2 * W - x) / x);
      for (int y2 = bound + 1; y2 <= lim2; ++y2) {
        if (x * y2 <= rhs + __gcd(W, abs(y1 - y2)) + __gcd(W - x, y2))
          ++ans;
      }
    }
  }
  return ans;
}
int main() {
  int w, h, k;
  scanf("%d%d%d", &w, &h, &k);
  ll ans = 2 * (solve(w, h, k) + solve(h, w, k));
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbv(v)                                                                 \\
  cerr << #v << "=";                                                           \\
  for (auto _x : v)                                                            \\
    cerr << _x << ", ";                                                        \\
  cerr << endl
#define dba(a, n)                                                              \\
  cerr << #a << "=";                                                           \\
  for (int _i = 0; _i < (n); ++_i)                                             \\
    cerr << a[_i] << ", ";                                                     \\
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int solve(int W, int H, int K) {
  K = 2 * K - 2;
  int ans = 0;
  for (int x = 1; x < W; ++x) {
    int lim = min(H - 1, (K + 2 * W) / (W - x));
    for (int y1 = 1; y1 <= lim; ++y1) {
      int rhs = K - (W - x) * y1 + __gcd(x, y1);
      // x * y2 <= rhs + (W, |y1-y2|) + (W-x, y2)
      int lim2 = min(H - 1, (rhs + 2 * W - x) / x);
      for (int y2 = 1; y2 <= lim2; ++y2) {
        if (x * y2 <= rhs + __gcd(W, abs(y1 - y2)) + __gcd(W - x, y2))
          ++ans;
      }
    }
  }
  return ans;
}
int main() {
  int w, h, k;
  scanf("%d%d%d", &w, &h, &k);
  int ans = 2 * (solve(w, h, k) + solve(h, w, k));
  printf("%d\\n", ans);
}