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

#include <bits/stdc++.h>

#define debug(...) fprintf(stderr, __VA_ARGS__)

#ifndef AT_HOME
#define getchar() IO::myGetchar()
#define putchar(x) IO::myPutchar(x)
#endif

namespace IO {
static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;

inline char myGetchar() {
  static char buf[IN_BUF], *ps = buf, *pt = buf;
  if (ps == pt) {
    ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin);
  }
  return ps == pt ? EOF : *ps++;
}

template <typename T> inline bool read(T &x) {
  bool op = 0;
  char ch = getchar();
  x = 0;
  for (; !isdigit(ch) && ch != EOF; ch = getchar()) {
    op ^= (ch == '-');
  }
  if (ch == EOF) {
    return false;
  }
  for (; isdigit(ch); ch = getchar()) {
    x = x * 10 + (ch ^ '0');
  }
  if (op) {
    x = -x;
  }
  return true;
}

inline int readStr(char *s) {
  int n = 0;
  char ch = getchar();
  for (; isspace(ch) && ch != EOF; ch = getchar())
    ;
  for (; !isspace(ch) && ch != EOF; ch = getchar()) {
    s[n++] = ch;
  }
  s[n] = '\\0';
  return n;
}

inline void myPutchar(char x) {
  static char pbuf[OUT_BUF], *pp = pbuf;
  struct _flusher {
    ~_flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); }
  };
  static _flusher outputFlusher;
  if (pp == pbuf + OUT_BUF) {
    fwrite(pbuf, 1, OUT_BUF, stdout);
    pp = pbuf;
  }
  *pp++ = x;
}

template <typename T> inline void print_(T x) {
  if (x == 0) {
    putchar('0');
    return;
  }
  static int num[40];
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  for (*num = 0; x; x /= 10) {
    num[++*num] = x % 10;
  }
  while (*num) {
    putchar(num[*num] ^ '0');
    --*num;
  }
}

template <typename T> inline void print(T x, char ch = '\\n') {
  print_(x);
  putchar(ch);
}

inline void printStr_(const char *s, int n = -1) {
  if (n == -1) {
    n = strlen(s);
  }
  for (int i = 0; i < n; ++i) {
    putchar(s[i]);
  }
}

inline void printStr(const char *s, int n = -1, char ch = '\\n') {
  printStr_(s, n);
  putchar(ch);
}
} // namespace IO
using namespace IO;

const int N = 100005;

int K;

long long solve(int W, int H) {
  int S = 3 * H + 2 * K;
  long long ans[2] = {0, 0};
  for (int y = 1; y < H; ++y) {
    for (int d = 0; d * y <= S && d < W; ++d) {
      int R = std::__gcd(d, H) + 2 * K - 2 - d * y;
      int lim = std::min(std::max(R / H, 0), W - d - 1);
      ans[d == 0] += lim;
      for (int x1 = lim + 1; x1 <= lim + 2 && x1 + d < W; ++x1) {
        if (x1 * H - std::__gcd(x1 + d, H - y) - std::__gcd(x1, y) <= R) {
          ++ans[d == 0];
        }
      }
    }
  }
  return 2 * ans[0] + ans[1];
}

int main() {
  int W, H;
  read(W), read(H), read(K);
  // W = H = K = 100000;
  print(2 * (solve(W, H) + solve(H, W)));
  debug("%.3lf\\n", 1.0 * clock() / CLOCKS_PER_SEC);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define debug(...) fprintf(stderr, __VA_ARGS__)

#ifndef AT_HOME
#define getchar() IO::myGetchar()
#define putchar(x) IO::myPutchar(x)
#endif

namespace IO {
static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;

inline char myGetchar() {
  static char buf[IN_BUF], *ps = buf, *pt = buf;
  if (ps == pt) {
    ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin);
  }
  return ps == pt ? EOF : *ps++;
}

template <typename T> inline bool read(T &x) {
  bool op = 0;
  char ch = getchar();
  x = 0;
  for (; !isdigit(ch) && ch != EOF; ch = getchar()) {
    op ^= (ch == '-');
  }
  if (ch == EOF) {
    return false;
  }
  for (; isdigit(ch); ch = getchar()) {
    x = x * 10 + (ch ^ '0');
  }
  if (op) {
    x = -x;
  }
  return true;
}

inline int readStr(char *s) {
  int n = 0;
  char ch = getchar();
  for (; isspace(ch) && ch != EOF; ch = getchar())
    ;
  for (; !isspace(ch) && ch != EOF; ch = getchar()) {
    s[n++] = ch;
  }
  s[n] = '\\0';
  return n;
}

inline void myPutchar(char x) {
  static char pbuf[OUT_BUF], *pp = pbuf;
  struct _flusher {
    ~_flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); }
  };
  static _flusher outputFlusher;
  if (pp == pbuf + OUT_BUF) {
    fwrite(pbuf, 1, OUT_BUF, stdout);
    pp = pbuf;
  }
  *pp++ = x;
}

template <typename T> inline void print_(T x) {
  if (x == 0) {
    putchar('0');
    return;
  }
  static int num[40];
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  for (*num = 0; x; x /= 10) {
    num[++*num] = x % 10;
  }
  while (*num) {
    putchar(num[*num] ^ '0');
    --*num;
  }
}

template <typename T> inline void print(T x, char ch = '\\n') {
  print_(x);
  putchar(ch);
}

inline void printStr_(const char *s, int n = -1) {
  if (n == -1) {
    n = strlen(s);
  }
  for (int i = 0; i < n; ++i) {
    putchar(s[i]);
  }
}

inline void printStr(const char *s, int n = -1, char ch = '\\n') {
  printStr_(s, n);
  putchar(ch);
}
} // namespace IO
using namespace IO;

const int N = 100005;

int K;

int solve(int W, int H) {
  int S = 3 * H + 2 * K;
  int ans[2] = {0, 0};
  for (int x1 = 1; x1 < W && x1 * H <= S; ++x1) {
    for (int y = 1; y < H; ++y) {
      for (int dx = 0; dx * y <= S - x1 * H && x1 + dx < W; ++dx) {
        int x2 = x1 + dx;
        int t = x1 * H + dx * y;
        t -= std::__gcd(dx, H) + std::__gcd(x2, H - y) + std::__gcd(x1, y) - 2;
        if (t % 2 == 0 && t <= 2 * K) {
          ++ans[dx == 0];
        }
      }
    }
  }
  return 2 * ans[0] + ans[1];
}

int main() {
  int W, H;
  read(W), read(H), read(K);
  print(2 * (solve(W, H) + solve(H, W)));
}