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

#include <bits/stdc++.h>
#define For(i, x, y) for (register int i = (x); i <= (y); i++)
#define FOR(i, x, y) for (register int i = (x); i < (y); i++)
#define Dow(i, x, y) for (register int i = (x); i >= (y); i--)
#define Debug(v)                                                               \\
  for (auto i : v)                                                             \\
    printf("%lld ", i);                                                        \\
  puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fil(a, b) memset((a), (b), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pa;
typedef pair<ll, ll> PA;
typedef vector<int> poly;
inline ll read() {
  ll x = 0, f = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && (c != '-'))
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}

int w, h, k;
inline ll solve(int w, int h, int k) {
  ll ans = 0;
  FOR(y, 1, w) {
    ++ans;
    For(z, 2, h - 1) {
      if (1ll * y * z + 1ll * z * (w - y) - __gcd(z, y) - __gcd(w - y, z) - w <=
          2 * k - 2)
        ++ans;
      int l = 1, r = z - 1, tmp = __gcd(w - y, z), L = 0, lim = 2 * k - 2;
      while (l <= r) {
        int mid = l + r >> 1;
        if (1ll * y * z + 1ll * (w - y) * mid - 2 - tmp <= lim)
          L = mid, l = mid + 1;
        else
          r = mid - 1;
      }
      int ret = 2 * L, R = L;
      For(x, L + 1, z - 1) {
        if (1ll * y * z + 1ll * (w - y) * x - y - min(z - x, w) - tmp > lim) {
          R = x - 1;
          break;
        }
        if (1ll * y * z + 1ll * (w - y) * x - tmp - __gcd(x, y) -
                __gcd(w, z - x) <=
            lim) {
          ret += 2;
        }
      }
      ans += ret;
      if (R == 0)
        break;
    }
  }
  return ans;
}

int main() {
  w = read(), h = read(), k = read();
  if (w == 1 || h == 1)
    return puts("0"), 0;
  printf("%lld\\n", 2 * (solve(w, h, k) + solve(h, w, k)));
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define For(i, x, y) for (register int i = (x); i <= (y); i++)
#define FOR(i, x, y) for (register int i = (x); i < (y); i++)
#define Dow(i, x, y) for (register int i = (x); i >= (y); i--)
#define Debug(v)                                                               \\
  for (auto i : v)                                                             \\
    printf("%lld ", i);                                                        \\
  puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fil(a, b) memset((a), (b), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pa;
typedef pair<ll, ll> PA;
typedef vector<int> poly;
inline ll read() {
  ll x = 0, f = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && (c != '-'))
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}

int w, h, k;
inline ll solve(int w, int h, int k) {
  ll ans = 0;
  /*FOR(y,1,w)
          FOR(z,1,h)
                  For(x,1,z) if
  (1ll*y*z+1ll*x*(w-y)-__gcd(x,y)-__gcd(w-y,z)-__gcd(w,z-x)<=2*k-2){ ans++;
                          printf("%d %d %d %d\\n",w,y,z,x);
                          if (z!=x) ans++;
                  }
  return ans;*/
  FOR(y, 1, w) {
    ++ans;
    For(z, 2, h - 1) {
      if (1ll * y * z + 1ll * z * (w - y) - __gcd(z, y) - __gcd(w - y, z) - w <=
          2 * k - 2)
        ++ans;
      int l = 1, r = z - 1, tmp = __gcd(w - y, z), R = 0, lim = 2 * k - 2;
      l = 1, r = z - 1;
      while (l <= r) {
        int mid = l + r >> 1;
        if (1ll * y * z + 1ll * (w - y) * mid - y - min(z - mid, w) - tmp <=
            lim)
          R = mid, l = mid + 1;
        else
          r = mid - 1;
      }
      if (R == 0)
        break;
      int ret = 0;
      For(x, 1, R) if (1ll * y * z + 1ll * (w - y) * x - tmp - __gcd(x, y) -
                           __gcd(w, z - x) <=
                       lim) {
        ret += 2;
      }
      ans += ret;
    }
  }
  return ans;
}

int main() {
  w = read(), h = read(), k = read();
  if (w == 1 || h == 1)
    return puts("0"), 0;
  printf("%lld\\n", 2 * (solve(w, h, k) + solve(h, w, k)));
}