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


// Problem : F - Triangles
// Contest : AtCoder - Tokio Marine & Nichido Fire Insurance Programming Contest
// 2020 URL :
// https://atcoder.jp/contests/tokiomarine2020/tasks/tokiomarine2020_f Memory
// Limit : 1024 MB Time Limit : 4000 ms Powered by CP Editor
// (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef complex<double> cd;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define pque priority_queue
#define rep(i, l, r) for (i = (l); i <= (r); ++i)
#define per(i, l, r) for (i = (l); i >= (r); --i)
#define REP(i, l, r) for (i = (l); i < (r); ++i)
#define PER(i, l, r) for (i = (l); i > (r); --i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define fi first
#define se second
#define bugf(...) fprintf(stderr, __VA_ARGS__);
#define bugs(x) cerr << "    " << (x);
#define bugx(x) cerr << "    " << (#x) << " = " << (x);
#define bugl cerr << "\\n";
#define bugL cerr << "\\n================================================\\n";
#define bugt                                                                   \\
  bugx(clock());                                                               \\
  bugl;
template <class IT> inline void cmax(IT &a, IT b) {
  if (a < b)
    a = b;
}
template <class IT> inline void cmin(IT &a, IT b) {
  if (b < a)
    a = b;
}
template <class IT> inline bool bmax(IT &a, IT b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <class IT> inline bool bmin(IT &a, IT b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
inline int gcd(int a, int b) {
  int c;
  while (c = b) {
    b = a % b;
    a = c;
  }
  return a;
}
ll ans;

inline void sol(int w, int h, int k) {
  // bugL bugs("SOL") bugx(w) bugx(h) bugl
  int x, s, y, r, b = ((k - 1) << 1) + w;
  REP(x, 1, w) per(s, min(h - 2, b / x), 0) {
    // bugx(x) bugx(s) bugl
    r = (k << 1) - (2 + x * s) + gcd(w, s);
    y = r / w + 1;
    if (y * w - gcd(x, y) - gcd(w - x, y + s) > r)
      --y;
    cmin(y, h - (s + 1));
    ans += s ? (y << 1) : y;
  }
}
inline void ac() {
  int w, h, k;
  scanf("%d%d%d", &w, &h, &k);
  sol(w, h, k);
  sol(h, w, k);
  printf("%lld", ans << 1);
}
int main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);

  ac();
  return 0;
}
#undef pque
#undef rep
#undef per
#undef REP
#undef PER
#undef mp
#undef pb
#undef pf
#undef pob
#undef pof
#undef fi
#undef se
#undef bugs
#undef bugx
#undef bugl

Error: TLE

Fixed submission:

// Problem : F - Triangles
// Contest : AtCoder - Tokio Marine & Nichido Fire Insurance Programming Contest
// 2020 URL :
// https://atcoder.jp/contests/tokiomarine2020/tasks/tokiomarine2020_f Memory
// Limit : 1024 MB Time Limit : 4000 ms Powered by CP Editor
// (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (i = (l); i <= (r); ++i)
#define per(i, l, r) for (i = (l); i >= (r); --i)
#define REP(i, l, r) for (i = (l); i < (r); ++i)
#define PER(i, l, r) for (i = (l); i > (r); --i)
#define R register
template <class IT> inline void cmax(IT &a, IT b) {
  if (a < b)
    a = b;
}
template <class IT> inline void cmin(IT &a, IT b) {
  if (b < a)
    a = b;
}
template <class IT> inline bool bmax(IT &a, IT b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <class IT> inline bool bmin(IT &a, IT b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
inline int gcd(int a, int b) {
  R int c;
  while (c = b) {
    b = a % b;
    a = c;
  }
  return a;
}
int ans;
inline void sol(int w, int h, int k) {
  // bugs("SOL") bugx(w) bugx(h) bugl
  int a, b, c, A, C, B = k + h - 2, D;
  rep(b, 1, h >> 1) {
    A = min(B / (h - b) + 2, w);
    D = B;
    REP(a, 1, A) {
      C = min(D / b + 2, w);
      REP(c, 1, C)
      if (a * (h - b) + b * c + 2 <=
          k + gcd(a, b) + gcd(c, h - b) + gcd((a > c) ? (a - c) : (c - a), h)) {
        if ((b << 1) == h)
          ++ans;
        else
          ans += 2;
      }
      cmax(D -= h - b, 0);
    }
  }
}
inline void ac() {}
int main() {
  int w, h, k, n, i;
  scanf("%d%d%d", &w, &h, &k);
  k <<= 1;
  sol(w, h, k);
  sol(h, w, k);
  printf("%d", ans << 1);
  return 0;
}
#undef pque
#undef rep
#undef per
#undef REP
#undef PER
#undef mp
#undef pb
#undef pf
#undef pob
#undef pof
#undef fi
#undef se
#undef bugs
#undef bugx
#undef bugl