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

/**
 * author: Behradm
 * Created: 2020.09.19 16:26:12
 * In The Name of God
 **/
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

const int N = 600001;
int lanat_be_tle[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  auto Bad = [&](int n) {
    int sq = sqrt(n);
    if (sq * sq == n)
      return true;
    return false;
  };

  int n;
  scanf("%d", &n);

  for (int i = 1; i < N; i++) {
    int cnt = 0;
    for (int j = 1; j * j <= i; j++) {
      if (i % j == 0)
        cnt += 2;
    }
    if (Bad(i))
      cnt -= 1;
    lanat_be_tle[i] = cnt;
  }

  int res = 0;
  // set<array<int, 3>> s;
  for (int k = 1; k < n; k++) {
    int cur = n - k;
    if (cur < N) {
      res += lanat_be_tle[cur];
      continue;
    }
    for (int i = 1; i * i <= cur; i++) {
      if (cur % i == 0) {
        res += 2;
      }
    }
    if (Bad(cur))
      res -= 1;

    /*for (int i = 1; i * i <= cur; i++) {
      int a = i, b = cur / i;
      if (a * a == cur) {
//        s.insert({i, i, k});
        res += 1;
      }
      if (a * b == cur && a != b) {
  //      s.insert({i, cur / i, k});
    //    s.insert({cur / i, i, k});
        res += 2;
      }
      if (b * b == cur && a != b) {
      //  s.insert({cur / i, cur / i, k});
        res += 1;
      }
    }*/

    /*int len = (int) d.size();
    for (int i = 0; i < len; i++) {
      for (int j = 0; j < len; j++) {
        if (d[i] * d[j] == cur && d[i] * d[j] + k == n) {
          s.insert({d[i], d[j], k});
        }
      }
    }*/
  }

  printf("%d\\n", res);
  return 0;
}

/*
  set<tuple<int, int, int>> s;
  for (int k = 1; k < n; k++) {
    int cur = n - k;
    for (int i = 1; i * i <= cur; i++) {
      if (i * i == cur)
        s.insert(make_tuple(i, i, k));
      if (i * (cur / i) == cur) {
        s.insert(make_tuple(i, cur / i, k));
        s.insert(make_tuple(cur / i, i, k));
      }
      if ((cur / i) * (cur / i) == cur)
        s.insert(make_tuple(cur / i, cur / i, k));
    }
*/

Error: TLE

Fixed submission:

/**
 * author: Behradm
 * Created: 2020.09.19 16:26:12
 * In The Name of God
 **/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  auto Bad = [&](int n) {
    int sq = sqrt(n);
    if (sq * sq == n)
      return true;
    return false;
  };

  vector<int> lanat_be_tle(400001, 0);
  for (int i = 1; i <= 400000; i++) {
    int cnt = 0;
    for (int j = 1; j * j <= i; j++) {
      if (i % j == 0)
        cnt += 2;
    }
    if (Bad(i))
      cnt -= 1;
    lanat_be_tle[i] = cnt;
  }
  int n;
  cin >> n;

  // set<array<int, 3>> s;
  int res = 0;
  for (int k = 1; k < n; k++) {
    int cur = n - k;
    if (cur <= 400000) {
      res += lanat_be_tle[cur];
      continue;
    }

    for (int i = 1; i * i <= cur; i++) {
      if (i * i == cur) {
        //        s.insert({i, i, k});
        res += 1;
      }
      if (i * (cur / i) == cur && i != cur / i) {
        //      s.insert({i, cur / i, k});
        //    s.insert({cur / i, i, k});
        res += 2;
      }
      if ((cur / i) * (cur / i) == cur && i != cur / i) {
        //  s.insert({cur / i, cur / i, k});
        res += 1;
      }
    }

    /*int len = (int) d.size();
    for (int i = 0; i < len; i++) {
      for (int j = 0; j < len; j++) {
        if (d[i] * d[j] == cur && d[i] * d[j] + k == n) {
          s.insert({d[i], d[j], k});
        }
      }
    }*/
  }

  cout << res << '\\n';
  return 0;
}

/*
  set<tuple<int, int, int>> s;
  for (int k = 1; k < n; k++) {
    int cur = n - k;
    for (int i = 1; i * i <= cur; i++) {
      if (i * i == cur)
        s.insert(make_tuple(i, i, k));
      if (i * (cur / i) == cur) {
        s.insert(make_tuple(i, cur / i, k));
        s.insert(make_tuple(cur / i, i, k));
      }
      if ((cur / i) * (cur / i) == cur)
        s.insert(make_tuple(cur / i, cur / i, k));
    }
*/