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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

#ifdef iq
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

ll area(int x1, int y1, int x2, int y2, int x3, int y3) {
  int a = x2 - x1, b = y2 - y1;
  int c = x3 - x1, d = y3 - y1;
  return abs(a * (ll)d - b * (ll)c);
}

ll f(int a, int b) {
  a = abs(a), b = abs(b);
  while (b) {
    int ret = a % b;
    a = b;
    b = ret;
  }
  return a;
}

ll cost(int x1, int y1, int x2, int y2, int x3, int y3) {
  ll a = area(x1, y1, x2, y2, x3, y3);
  ll g = f(x2 - x1, y2 - y1) + f(x3 - x1, y3 - y1) + f(x3 - x2, y3 - y2);
  return a - g + 2;
}

ll bad_consider = 0;

ll solve(int w, int h, int k) {
  ll sum = 0;
  int t = min(w, h);
  int z = max(w, h);
  for (int x1 = 1; x1 < w; x1++) {
    for (int y = h - 1; y >= 1; y--) {
      if (x1 * (ll)(h - y) > k + 3 * z) {
        break;
      }
      ll val = x1 * (ll)(h - y) - f(y, x1);
      ll mx = min((ll)w - 1, max(0ll, (k - val) / y));
      sum += 2 * mx;
      auto eval = [&](int x2) {
        if (val + x2 * (ll)y - f(h - y, x2) - f(x2 - x1, h) <= k) {
          sum += 2;
        }
      };
      if (x1 > mx) {
        eval(x1);
      }
      for (int x2 = mx + 1; x2 < w; x2++) {
        if (val + x2 * (ll)y - 2 * t > k) {
          break;
        }
        if (x2 != x1)
          eval(x2);
      }
    }
  }
  return sum;
}

int main() {
#ifdef iq
  freopen("a.in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  int w, h, k;
  cin >> w >> h >> k;
  k = 2 * (k - 1);
  cout << solve(w, h, k) + solve(h, w, k) << '\\n';
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

#ifdef iq
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

ll area(int x1, int y1, int x2, int y2, int x3, int y3) {
  int a = x2 - x1, b = y2 - y1;
  int c = x3 - x1, d = y3 - y1;
  return abs(a * (ll)d - b * (ll)c);
}

ll f(int a, int b) {
  a = abs(a), b = abs(b);
  while (b) {
    int ret = a % b;
    a = b;
    b = ret;
  }
  return a;
}

ll cost(int x1, int y1, int x2, int y2, int x3, int y3) {
  ll a = area(x1, y1, x2, y2, x3, y3);
  ll g = f(x2 - x1, y2 - y1) + f(x3 - x1, y3 - y1) + f(x3 - x2, y3 - y2);
  return a - g + 2;
}

ll solve(int w, int h, int k) {
  ll sum = 0;
  for (int x1 = 1; x1 < w; x1++) {
    for (int y = h - 1; y >= 1; y--) {
      if (x1 * (ll)(h - y) > k + 3 * h) {
        break;
      }
      ll val = x1 * (ll)(h - y) - f(y, x1);
      for (int x2 = 1; x2 < w; x2++) {
        if (val + x2 * (ll)y - h - w > k) {
          break;
        }
        if (val + x2 * (ll)y <= k) {
          sum += 2;
          continue;
        }
        if (val + x2 * (ll)y - f(h - y, x2) - f(x2 - x1, h) <= k) {
          sum += 2;
        }
      }
    }
  }
  return sum;
}

int main() {
#ifdef iq
  freopen("a.in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  int w, h, k;
  cin >> w >> h >> k;
  k = 2 * (k - 1);
  cout << solve(w, h, k) + solve(h, w, k) << '\\n';
}