alexjercan/bugnet huggingface dataset:
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int M = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  if ((s | t) != t) {
    cout << 0 << '\\n';
    return 0;
  }
  vector<int> a;
  for (int i = 0; i < n; ++i) {
    int aa;
    cin >> aa;
    if ((aa & s) == s && (aa | t) == t) {
      a.push_back(aa);
    }
  }
  n = a.size();
  k = min(n, k);
  vector<P> cond;
  for (int i = 0; i < 18; ++i) {
    if (((s >> i) & 1) == 0) {
      cond.push_back({i, 0});
    }
    if (((t >> i) & 1) == 1) {
      cond.push_back({i, 1});
    }
  }
  vector<unordered_map<long long, long long>> dp(k + 1);
  for (int i = 0; i <= k; ++i) {
    dp[i].reserve(1 << 18);
  }
  dp[0][0] = 1;
  for (int i = 0; i < n; ++i) {
    long long mask = 0;
    for (int j = 0; j < (int)cond.size(); ++j) {
      if (((a[i] >> cond[j].first) & 1) == cond[j].second) {
        mask ^= 1LL << j;
      }
    }
    for (int j = k - 1; j >= 0; --j) {
      for (auto &l : dp[j]) {
        dp[j + 1][l.first | mask] += l.second;
      }
    }
  }
  long long ans = 0;
  for (auto &i : dp) {
    ans += i[(1LL << (int)cond.size()) - 1];
  }

  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int M = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  if ((s | t) != t) {
    cout << 0 << '\\n';
    return 0;
  }
  vector<int> a;
  for (int i = 0; i < n; ++i) {
    int aa;
    cin >> aa;
    if ((aa & s) == s && (aa | t) == t) {
      a.push_back(aa);
    }
  }
  n = a.size();
  k = min(n, k);
  vector<P> cond;
  for (int i = 0; i < 18; ++i) {
    if (((s >> i) & 1) == 0) {
      cond.push_back({i, 0});
    }
    if (((t >> i) & 1) == 1) {
      cond.push_back({i, 1});
    }
  }
  vector<unordered_map<long long, long long>> dp(k + 1);
  dp[0][0] = 1;
  for (int i = 0; i < n; ++i) {
    long long mask = 0;
    for (int j = 0; j < (int)cond.size(); ++j) {
      if (((a[i] >> cond[j].first) & 1) == cond[j].second) {
        mask ^= 1LL << j;
      }
    }
    for (int j = k - 1; j >= 0; --j) {
      for (auto &l : dp[j]) {
        dp[j + 1][l.first | mask] += l.second;
      }
    }
  }
  long long ans = 0;
  for (auto &i : dp) {
    ans += i[(1LL << (int)cond.size()) - 1];
  }

  cout << ans << '\\n';

  return 0;
}
Problem p02649 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 50, M = 262144, C = 18;

int n, m, san, sor, a[N + 9];

void into() {
  scanf("%d%d%d%d", &n, &m, &san, &sor);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
}

void Get_a() {
  int lst = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] & san ^ san)
      continue;
    if ((a[i] | sor) ^ sor)
      continue;
    a[++lst] = a[i];
  }
  n = lst;
}

LL c[N + 9][N + 9];

void Get_c() {
  c[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    c[i][0] = c[i][i] = 1;
    for (int j = 1; j < i; ++j)
      c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
  }
}

int mu[M + 9];

void Get_mu() {
  mu[0] = 1;
  for (int s = 1; s < M; ++s)
    mu[s] = s & 1 ? -mu[s >> 1] : mu[s >> 1];
}

int vis[M + 9], cnt[M + 9];
LL ans;

void Get_ans() {
  if (!n)
    return;
  for (int cs = san ^ sor, s = cs; 2333; s = s - 1 & cs) {
    for (int i = 1; i <= n; ++i)
      ++cnt[a[i] & s];
    for (int i = 1; i <= n; ++i) {
      int t = a[i] & s;
      if (vis[t])
        continue;
      vis[t] = 1;
      for (int j = 1; j <= m; ++j)
        ans += c[cnt[t]][j] * mu[s];
    }
    for (int i = 1; i <= n; ++i)
      vis[a[i] & s] = cnt[a[i] & s] = 0;
    if (!s)
      break;
  }
}

void work() {
  Get_a();
  Get_c();
  Get_mu();
  Get_ans();
}

void outo() { printf("%lld\\n", ans); }

int main() {
  into();
  work();
  outo();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 50, M = 262144, C = 18;

int n, m, san, sor, a[N + 9];

void into() {
  scanf("%d%d%d%d", &n, &m, &san, &sor);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
}

void Get_a() {
  int lst = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] & san ^ san)
      continue;
    if ((a[i] | sor) ^ sor)
      continue;
    a[++lst] = a[i];
  }
  n = lst;
}

LL c[N + 9][N + 9];

void Get_c() {
  c[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    c[i][0] = c[i][i] = 1;
    for (int j = 1; j < i; ++j)
      c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
  }
}

int mu[N + 9];

void Get_mu() {
  mu[0] = 1;
  for (int s = 1; s < 1 << n; ++s)
    mu[s] = s & 1 ? -mu[s >> 1] : mu[s >> 1];
}

int vis[M + 9], cnt[M + 9];
LL ans;

void Get_ans() {
  if (!n)
    return;
  for (int cs = san ^ sor, s = cs; 2333; s = s - 1 & cs) {
    for (int i = 1; i <= n; ++i)
      ++cnt[a[i] & s];
    for (int i = 1; i <= n; ++i) {
      int t = a[i] & s;
      if (vis[t])
        continue;
      vis[t] = 1;
      for (int j = 1; j <= m; ++j)
        ans += c[cnt[t]][j] * mu[s];
    }
    for (int i = 1; i <= n; ++i)
      vis[a[i] & s] = 0, --cnt[a[i] & s];
    if (!s)
      break;
  }
}

void work() {
  Get_a();
  Get_c();
  Get_mu();
  Get_ans();
}

void outo() { printf("%lld\\n", ans); }

int main() {
  into();
  work();
  outo();
  return 0;
}
Problem p02650 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int W, H, K;

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int64_t solve() {
  // A = I + B / 2 - 1, where I <= K
  // A - B / 2 + 1 = I <= K
  // 2 * A - B <= 2 * (K - 1)
  //
  // A = H * W - W * (H - (y1 + y2) / 2) - x * y1 / 2 - (W - x) * y2 / 2
  //   = W * (y1 + y2) / 2 - x * y1 / 2 - (W - x) * y2 / 2
  //   = [(W - x) * y1 + x * y2] / 2
  // B = gcd(y1 - y2, W) + gcd(y1, x) + gcd(y2, W - x)
  int64_t total = 0;
  int lim = 2 * (K - 1) + 3 * W;
  for (int x = 1; x < W; x++) {
    for (int y2 = 1; y2 < H; y2++) {
      if (x * y2 > lim)
        break;
      // (W - x) * y1 + x * y2 <= 2 * (K - 1) + 3 <= 2 * (K - 1) + B
      int y1 = (2 * (K - 1) - x * y2 + 3) / (W - x);
      if (y1 >= y2) {
        total += 2 * y2 - 1;
        continue;
      }
      if (y1 < 1)
        y1 = 1;
      total += 2 * (y1 - 1);
      for (; y1 <= y2; y1++) {
        int A2 = (W - x) * y1 + x * y2;
        if (A2 > lim)
          break;
        int B = gcd(y1, x) + gcd(y2, W - x) + gcd(abs(y1 - y2), W);
        if (A2 - B <= 2 * (K - 1)) {
          total++;
          if (y1 != y2)
            total++;
        }
      }
    }
  }
  return total;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> W >> H >> K;

  int64_t total = solve();
  swap(W, H);
  total += solve();
  total *= 2;
  cout << total << endl;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int W, H, K;

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int solve() {
  // A = I + B / 2 - 1, where I <= K
  // A - B / 2 + 1 = I <= K
  // 2 * A - B <= 2 * (K - 1)
  //
  // A = H * W - W * (H - (y1 + y2) / 2) - x * y1 / 2 - (W - x) * y2 / 2
  //   = W * (y1 + y2) / 2 - x * y1 / 2 - (W - x) * y2 / 2
  //   = [(W - x) * y1 + x * y2] / 2
  // B = gcd(y1 - y2, W) + gcd(y1, x) + gcd(y2, W - x)
  int total = 0;
  int lim = 2 * (K - 1) + 3 * W;
  for (int x = 1; x < W; x++) {
    for (int y2 = 1; y2 < H; y2++) {
      if (x * y2 > lim)
        break;
      for (int y1 = 1; y1 <= y2; y1++) {
        int A2 = (W - x) * y1 + x * y2;
        if (A2 > lim)
          break;
        int B = gcd(y1, x) + gcd(y2, W - x) + gcd(abs(y1 - y2), W);
        if (A2 - B <= 2 * (K - 1)) {
          total++;
          if (y1 != y2)
            total++;
        }
      }
    }
  }
  return total;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> W >> H >> K;

  int total = solve();
  swap(W, H);
  total += solve();
  total *= 2;
  cout << total << endl;
}
Problem p02650 (C++) — buggy submission, verdict: Time Limit Exceeded

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
int h, w, k;
const int BUBEN = 1e5 + 10;
int f(int x, int y) { return __gcd(x, y) + 1; }
const int maxN = 1e5 + 10;
vector<int> f1[maxN], f2[maxN];
int pp[maxN];
ll solve(int h, int w, int k) {
  ll ans = 0;
  k *= 2;
  for (int i = 0; i < maxN; i++) {
    pp[i] = __gcd(i, h);
  }
  for (int a = 1; a < h; a++) {
    vector<int> d1;
    vector<int> d2;
    for (int b = 1; b < w; b++) {
      if ((h - a) * b > k + BUBEN)
        break;
      d1.emplace_back((h - a) * b - f(a, b));
    }
    int max_b = d1.size();
    for (int c = 1; c < w; c++) {
      if (c * a > k + BUBEN)
        break;
      d2.emplace_back(c * a - f(h - a, c));
    }
    int max_c = d2.size();
    int t = max(max_b, max_c);
    for (int i = 0; i <= t; i++) {
      f1[i % h].clear();
      f2[i % h].clear();
    }
    for (int i = 1; i <= max_b; i++) {
      f1[i % h].emplace_back(d1[i - 1]);
    }
    for (int i = 1; i <= max_c; i++) {
      f2[i % h].emplace_back(d2[i - 1]);
    }
    for (int i = 0; i <= min(h - 1, max_b); i++) {
      sort(f1[i].begin(), f1[i].end());
      if (f1[i].empty())
        continue;
      for (int j = 0; j <= min(h - 1, max_c); j++) {
        if (f2[j].empty())
          continue;
        if (i == 1)
          sort(f2[j].begin(), f2[j].end());
        int d = pp[abs(i - j)];
        int lim = k - 4 + d;
        int ptr = f2[j].size() - 1;
        for (int x = 0; x < f1[i].size(); x++) {
          while (ptr >= 0 && f1[i][x] + f2[j][ptr] > lim)
            ptr--;
          ans += (ptr + 1);
        }
      }
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  //    freopen("input.txt", "r", stdin);
  cin >> h >> w >> k;
  cout << 2 * solve(h, w, k) + 2 * solve(w, h, k);
  return 0;
}


Error: TLE

Fixed submission:

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
int h, w, k;
const int BUBEN = 1e5 + 10;
int f(int x, int y) { return __gcd(x, y) + 1; }
int solve(int h, int w, int k) {
  int ans = 0;
  k *= 2;
  for (int a = 1; a < h; a++) {
    for (int b = 1; b < w; b++) {
      if ((h - a) * b > k + BUBEN)
        break;
      for (int c = 1; c < w; c++) {
        int area = c * a + (h - a) * b;
        if (area > k + BUBEN)
          break;
        area -= f(a, b);
        area -= f(h - a, c);
        if (b == c) {
          area -= (h + 1);
        } else {
          area -= f(abs(b - c), h);
        }
        area += 2;
        area += 3;
        if (area <= k)
          ans++;
      }
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  //    freopen("input.txt", "r", stdin);
  cin >> h >> w >> k;
  cout << 2 * solve(h, w, k) + 2 * solve(w, h, k);
  return 0;
}
Problem p02650 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
using namespace std;
int n, m, k;
long long gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
void swap(long long &x, long long &y) {
  long long t = x;
  x = y;
  y = t;
}
long long calculate(int r, int c) {
  long long total = 0;
  int pnum = 0;
  for (long long x = 1; x < r; x++) {
    if (x * c > 2 * k - 1 + c * 3)
      break;
    for (long long y = 1; y < c; y++) {
      int start = x + 1;
      long long s = 2 * k - 1 - (x * c - x * y - gcd(x, y));
      start = s / y - 1;
      if (start <= x)
        start = x + 1;
      if (start >= r)
        start = r - 1;
      if (r <= x + 1)
        continue;
      total += (start - x - 1);
      //	start=x+1;
      for (long long z = start; z < r; z++) {
        long long p = z * y - gcd(c - y, z) - gcd(z - x, c);
        if (x * c + z * y - x * y > 2 * k - 1 + c * 3)
          break;
        if (p <= s)
          total++;
      }
      pnum++;
      //	printf("%d\\n",total);
      if (pnum == 9) {
        total++;
        total--;
      }
    }
  }
  total *= 2;
  for (long long x = 1; x < r; x++) {
    if (x * c > 2 * k - 1 + c * 3)
      break;
    for (long long y = 1; y < c; y++) {
      long long z = x;
      long long p =
          x * c + z * y - x * y - gcd(x, y) - gcd(c - y, z) - gcd(z - x, c);
      if (p <= 2 * k - 1)
        total++;
    }
  }
  return total * 2;
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  // n++;m++;
  long long ans = 0;
  ans = calculate(n, m);
  ans = ans + calculate(m, n);
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;
int n, m, k;
long long gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
void swap(long long &x, long long &y) {
  long long t = x;
  x = y;
  y = t;
}
long long calculate(int r, int c) {
  long long total = 0;
  for (long long x = 1; x < r; x++) {
    if (x * c > 2 * k - 1 + c * 3)
      break;
    for (long long y = 1; y < c; y++)
      for (long long z = x + 1; z < r; z++) {
        long long p =
            x * c + z * y - x * y - gcd(x, y) - gcd(c - y, z) - gcd(z - x, c);
        if (x * c + z * y - x * y > 2 * k - 1 + c * 3)
          break;
        if (p <= 2 * k - 1)
          total++;
      }
  }
  total *= 2;
  for (long long x = 1; x < r; x++) {
    if (x * c > 2 * k - 1 + c * 3)
      break;
    for (long long y = 1; y < c; y++) {
      long long z = x;
      long long p =
          x * c + z * y - x * y - gcd(x, y) - gcd(c - y, z) - gcd(z - x, c);
      if (p <= 2 * k - 1)
        total++;
    }
  }
  return total * 2;
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  // n++;m++;
  long long ans = 0;
  ans = calculate(n, m);
  ans = ans + calculate(m, n);
  printf("%lld\\n", ans);
}
Problem p02650 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define MP make_pair

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MOD = 998244353;

LL solve(int w, int h, int k) {
  LL ret = 0;
  for (int a = 1; a <= w - 1 && a * h <= 2 * k + 2 * h - 2; a++)
    rep1(x, h - 1) {
      int ub = 2 * k + __gcd(a, h - x) - 2 - a * x;
      ret += min(ub / (h - x), w - 1) >= a
                 ? (min(w - 1, ub / (h - x)) - a) * 2 + 1
                 : 0;
      for (int b = max(a, min(ub / (h - x), w - 1) + 1);
           b <= w - 1 && b * (h - x) <= ub + h + x; b++)
        if (b * (h - x) - __gcd(h, b - a) - __gcd(b, x) <= ub)
          ret += b == a ? 1 : 2;
    }
  return ret;
}

int main() {
  int w, h, k;
  scanf("%d%d%d", &w, &h, &k);
  printf("%lld\\n", 2 * solve(w, h, k) + 2 * solve(h, w, k));
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define MP make_pair

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MOD = 998244353;

LL solve(int w, int h, int k) {
  LL ret = 0;
  for (int a = 1; a <= w - 1 && a * h <= 2 * k + 2 * h - 2; a++)
    rep1(x, h - 1) {
      int ub = 2 * k + __gcd(a, h - x) - 2 - a * x;
      for (int b = a; b <= w - 1 && b * (h - x) <= ub + h + x; b++) {
        if (b * (h - x) - __gcd(h, b - a) - __gcd(b, x) <= ub)
          ret += b == a ? 1 : 2;
      }
    }
  return ret;
}

int main() {
  int w, h, k;
  scanf("%d%d%d", &w, &h, &k);
  printf("%lld\\n", 2 * solve(w, h, k) + 2 * solve(h, w, k));
  return 0;
}
Problem p02650 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

Int gcd(Int x, Int y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

Int inside(Int x1, Int y1, Int x2, Int y2) {
  Int s = abs(x1 * y2 - x2 * y1);
  s += 2;
  s -= gcd(abs(x1), abs(y1));
  s -= gcd(abs(x2), abs(y2));
  s -= gcd(abs(x2 - x1), abs(y2 - y1));
  return s / 2;
}

Int cnt(Int w, Int h, Int k) {
  Int ans = 0;
  for (Int x = 1; x < w; x++) {
    for (Int s = 0; s < h - 1; s++) {
      if (inside(1, x, -s, w) > 2 * k)
        break;
      if (inside(1, x, -s, w) > k)
        continue;
      Int R = 2 * k - x * s - 2 + gcd(w, s);
      Int bottom = max(1LL, R / w), top = min(h - s, R / w + 2);
      bottom = min(bottom, top - 1);
      while (top - bottom > 1) {
        Int mid = (top + bottom) / 2;
        if (inside(mid, x, -s, w) <= k)
          bottom = mid;
        else
          top = mid;
      }
      if (s == 0)
        ans += bottom;
      else
        ans += bottom * 2;
    }
  }
  return ans;
}

int main() {
  Int w, h, k;
  cin >> w >> h >> k;
  cout << 2 * cnt(w, h, k) + 2 * cnt(h, w, k) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

Int gcd(Int x, Int y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

Int inside(Int x1, Int y1, Int x2, Int y2) {
  Int s = abs(x1 * y2 - x2 * y1);
  s += 2;
  s -= gcd(abs(x1), abs(y1));
  s -= gcd(abs(x2), abs(y2));
  s -= gcd(abs(x2 - x1), abs(y2 - y1));
  return s / 2;
}

Int cnt(Int w, Int h, Int k) {
  Int ans = 0;
  for (Int x = 1; x < w; x++) {
    for (Int s = 0; s < h - 1; s++) {
      if (inside(1, x, -s, w) > 2 * k)
        break;
      if (inside(1, x, -s, w) > k)
        continue;
      Int bottom = 1, top = h - s;
      while (top - bottom > 1) {
        Int mid = (top + bottom) / 2;
        if (inside(mid, x, -s, w) <= k)
          bottom = mid;
        else
          top = mid;
      }
      if (s == 0)
        ans += bottom;
      else
        ans += bottom * 2;
    }
  }
  return ans;
}

int main() {
  Int w, h, k;
  cin >> w >> h >> k;
  cout << 2 * cnt(w, h, k) + 2 * cnt(h, w, k) << endl;
  return 0;
}
Problem p02650 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
int n, m, k;
long long ans;

void solve() {
  for (int b = 0; b < m; ++b) {
    for (int c = 1; c < n && b * c <= k * 2 - 2 + n * 3; ++c) {
      int r = k * 2 - 2 + __gcd(n, b) - b * c;
      ans += max(0, min(m - 1 - b, r / n)) * (1 + (b > 0));
      for (int a = r / n + 1; a <= r / n + 2; ++a) {
        if (a > 0 && a + b < m)
          ans +=
              (a * n - __gcd(a, c) - __gcd(n - c, a + b) <= r) * (1 + (b > 0));
      }
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &k);
  solve();
  swap(n, m);
  solve();
  printf("%lld\\n", ans * 2);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n, m, k;
long long ans;

void solve() {
  for (int b = 0; b < m; ++b) {
    for (int c = 1; c < n; ++c) {
      int r = k * 2 - 2 + __gcd(n, b) - b * c;
      ans += max(0, min(m - 1 - b, r / n)) * (1 + (b > 0));
      for (int a = r / n + 1; a <= r / n + 2; ++a) {
        if (a > 0 && a + b < m)
          ans +=
              (a * n - __gcd(a, c) - __gcd(n - c, a + b) <= r) * (1 + (b > 0));
      }
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &k);
  solve();
  swap(n, m);
  solve();
  printf("%lld\\n", ans * 2);

  return 0;
}
Problem p02650 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int H, W, K;

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> H >> W >> K;
  ll ret = 0;
  FOR(i, 2) {
    for (int s = 0; s <= H - 2; s++) {
      int R = 2 * K + __gcd(s, W) - 2;
      ll pat = 0;
      for (x = 1; x < W; x++) {
        int R2 = R - s * x;
        if (R2 < 0)
          break;

        int Ymi = min(H - s - 2, R2 / W - 1);
        if (Ymi > 0)
          pat += Ymi;
        for (y = max(1, Ymi + 1); y <= Ymi + 2 && y + s < H; y++) {
          ll v = 1LL * W * y - __gcd(x, y) - __gcd(W - x, y + s);
          if (v <= R2)
            pat++;
        }
      }
      ret += ((s == 0) ? 1 : 2) * pat;
    }
    swap(H, W);
  }
  cout << 2 * ret << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int H, W, K;

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> H >> W >> K;
  ll ret = 0;
  FOR(i, 2) {
    for (int s = 0; s <= H - 2; s++) {
      int R = 2 * K + __gcd(s, W) - 2;
      ll pat = 0;
      for (x = 1; x < W; x++) {
        int R2 = R - s * x;
        if (R2 < 0)
          break;

        int Ymi = R2 / W;
        // if(Ymi) pat+=Ymi-1;
        // for(y=max(1,Ymi);y<=Ymi+2 && y+s<H;y++) {
        for (y = 1; y <= Ymi + 2 && y + s < H; y++) {
          ll v = 1LL * W * y - __gcd(x, y) - __gcd(W - x, y + s);
          if (v <= R2)
            pat++;
        }
      }
      ret += ((s == 0) ? 1 : 2) * pat;
    }
    swap(H, W);
  }
  cout << 2 * ret << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}