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

#include <algorithm>
#include <atomic>
#include <bitset>
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <mutex>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

static int my_speed_up = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  return 0;
}();
using ll = long long;
using ull = unsigned long long;
template <class T> using v = vector<T>;
template <class T> using vv = vector<vector<T>>;
template <class T> using vvv = vector<vector<vector<T>>>;
template <class T> using vvvv = vector<vector<vector<vector<T>>>>;
template <class T1, class T2> using um = unordered_map<T1, T2>;
template <class T> using us = unordered_set<T>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define print_line cout << "[" << setw(3) << __LINE__ << "]  ";
#define for_b(i, b) for (int i = 0; i < (b); ++i)
#define for_ab(i, a, b) for (int i = a; i < (b); ++i)
#define what_is(x)                                                             \\
  { print_line cout << #x << " = " << x << endl; }
template <class T, class... Ts>
void what_are_2(T const &arg, Ts const &...args) {
  cout << arg;
  if constexpr (sizeof...(args) > 0) {
    cout << ", ";
    what_are_2(args...);
  }
}
#define what_are(...)                                                          \\
  {                                                                            \\
    print_line what_are_2(__VA_ARGS__);                                        \\
    cout << endl;                                                              \\
  }
#define hit                                                                    \\
  { print_line cout << "hit!" << endl; }
#define pad(x) for_b(i, x) cout << "  ";
#define show_1d(___x)                                                          \\
  {                                                                            \\
    print_line cout << #___x << " = ";                                         \\
    for (auto ___num : ___x)                                                   \\
      cout << ___num << ", ";                                                  \\
    cout << endl;                                                              \\
  }
#define show_2d(___x)                                                          \\
  {                                                                            \\
    print_line cout << #___x << endl;                                          \\
    for (auto ___row : ___x) {                                                 \\
      for (auto ___num : ___row)                                               \\
        cout << ___num << ", ";                                                \\
      cout << endl;                                                            \\
    }                                                                          \\
  }
#define show_3d(___x)                                                          \\
  {                                                                            \\
    print_line cout << #___x << endl;                                          \\
    for (auto ___row : ___x) {                                                 \\
      for (auto ___row2 : ___row) {                                            \\
        for (auto ___num : ___row2)                                            \\
          cout << ___num << ", ";                                              \\
        cout << endl;                                                          \\
      }                                                                        \\
      cout << endl;                                                            \\
    }                                                                          \\
  }

vv<int> d4 = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vv<int> d8 = {{1, 0}, {-1, 0}, {0, 1},  {0, -1},
              {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
bool is_valid_pos(size_t w, size_t h, int x, int y) {
  return x >= 0 && x < static_cast<int>(w) && y >= 0 && y < static_cast<int>(h);
}

// ⣿⣿⣟⣽⣿⣿⣿⣿⣟⣵⣿⣿⣿⡿⣳⣫⣾⣿⣿⠟⠻⣿⣿⣿⢻⣿⣿⣿⣿⣷⡽⣿
// ⣿⣟⣾⡿⣿⣿⢟⣽⣿⣿⣿⣿⣫⡾⣵⣿⣿⣿⠃⠄⠄⠘⢿⣿⣾⣿⣿⣿⢿⣿⣿⡜
// ⡿⣼⡟⣾⣿⢫⣿⣿⣿⣿⡿⣳⣿⣱⣿⣿⣿⡋⠄⠄⠄⠄⠄⠛⠛⠋⠁⠄⠄⣿⢸⣿
// ⢳⣟⣼⡿⣳⣿⣿⣿⣿⡿⣹⡿⣃⣿⣿⣿⢳⠁⠄⠄⠄⢀⣀⠄⠄⠄⠄⠄⢀⣿⢿⣿
// ⡟⣼⣿⣱⣿⡿⣿⣿⣿⢡⣫⣾⢸⢿⣿⡟⣿⣶⡶⢰⣿⣿⣿⢷⠄⠄⠄⠄⢼⣿⣸⣿
// ⣽⣿⢣⣿⡟⣽⣿⣿⠃⣲⣿⣿⣸⣷⡻⡇⣿⣿⢇⣿⣿⣿⣏⣎⣸⣦⣠⡞⣾⢧⣿⣿
// ⣿⡏⣿⡿⢰⣿⣿⡏⣼⣿⣿⡏⠙⣿⣿⣤⡿⣿⢸⣿⣿⢟⡞⣰⣿⣿⡟⣹⢯⣿⣿⣿
// ⡿⢹⣿⠇⣿⣿⣿⣸⣿⣿⣿⣿⣦⡈⠻⣿⣿⣮⣿⣿⣯⣏⣼⣿⠿⠏⣰⡅⢸⣿⣿⣿
// ⡀⣼⣿⢰⣿⣿⣇⣿⣿⡿⠛⠛⠛⠛⠄⣘⣿⣿⣿⣿⣿⣿⣶⣿⠿⠛⢾⡇⢸⣿⣿⣿
// ⠄⣿⡟⢸⣿⣿⢻⣿⣿⣷⣶⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⡋⠉⣠⣴⣾⣿⡇⣸⣿⣿⡏
// ⠄⣿⡇⢸⣿⣿⢸⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣄⠘⢿⣿⠏⠄⣿⣿⣿⣹
// ⠄⢻⡇⢸⣿⣿⠸⣿⣿⣿⣿⣿⣿⠿⠿⢿⣿⣿⣿⣿⣿⣿⣿⣦⣼⠃⠄⢰⣿⣿⢯⣿
// ⠄⢸⣿⢸⣿⣿⡄⠙⢿⣿⣿⡿⠁⠄⠄⠄⠄⠉⣿⣿⣿⣿⣿⣿⡏⠄⢀⣾⣿⢯⣿⣿
// ⣾⣸⣿⠄⣿⣿⡇⠄⠄⠙⢿⣀⠄⠄⠄⠄⠄⣰⣿⣿⣿⣿⣿⠟⠄⠄⣼⡿⢫⣻⣿⣿
// ⣿⣿⣿⠄⢸⣿⣿⠄⠄⠄⠄⠙⠿⣷⣶⣤⣴⣿⠿⠿⠛⠉⠄⠄ ⢸⣿⣿⣿⣿⠃⠄⢀⣴

int main() {
  int N;
  cin >> N;

  v<int> tb(N, 0);

  for_ab(i, 1, tb.size()) {
    for (int j = i; j < tb.size(); j += i)
      ++tb[j];
  }
  int res = accumulate(tb.begin(), tb.end(), 0);
  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <atomic>
#include <bitset>
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <mutex>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

static int my_speed_up = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  return 0;
}();
using ll = long long;
using ull = unsigned long long;
template <class T> using v = vector<T>;
template <class T> using vv = vector<vector<T>>;
template <class T> using vvv = vector<vector<vector<T>>>;
template <class T> using vvvv = vector<vector<vector<vector<T>>>>;
template <class T1, class T2> using um = unordered_map<T1, T2>;
template <class T> using us = unordered_set<T>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define print_line cout << "[" << setw(3) << __LINE__ << "]  ";
#define for_b(i, b) for (int i = 0; i < (b); ++i)
#define for_ab(i, a, b) for (int i = a; i < (b); ++i)
#define what_is(x)                                                             \\
  { print_line cout << #x << " = " << x << endl; }
template <class T, class... Ts>
void what_are_2(T const &arg, Ts const &...args) {
  cout << arg;
  if constexpr (sizeof...(args) > 0) {
    cout << ", ";
    what_are_2(args...);
  }
}
#define what_are(...)                                                          \\
  {                                                                            \\
    print_line what_are_2(__VA_ARGS__);                                        \\
    cout << endl;                                                              \\
  }
#define hit                                                                    \\
  { print_line cout << "hit!" << endl; }
#define pad(x) for_b(i, x) cout << "  ";
#define show_1d(___x)                                                          \\
  {                                                                            \\
    print_line cout << #___x << " = ";                                         \\
    for (auto ___num : ___x)                                                   \\
      cout << ___num << ", ";                                                  \\
    cout << endl;                                                              \\
  }
#define show_2d(___x)                                                          \\
  {                                                                            \\
    print_line cout << #___x << endl;                                          \\
    for (auto ___row : ___x) {                                                 \\
      for (auto ___num : ___row)                                               \\
        cout << ___num << ", ";                                                \\
      cout << endl;                                                            \\
    }                                                                          \\
  }
#define show_3d(___x)                                                          \\
  {                                                                            \\
    print_line cout << #___x << endl;                                          \\
    for (auto ___row : ___x) {                                                 \\
      for (auto ___row2 : ___row) {                                            \\
        for (auto ___num : ___row2)                                            \\
          cout << ___num << ", ";                                              \\
        cout << endl;                                                          \\
      }                                                                        \\
      cout << endl;                                                            \\
    }                                                                          \\
  }

vv<int> d4 = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vv<int> d8 = {{1, 0}, {-1, 0}, {0, 1},  {0, -1},
              {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
bool is_valid_pos(size_t w, size_t h, int x, int y) {
  return x >= 0 && x < static_cast<int>(w) && y >= 0 && y < static_cast<int>(h);
}

// ⣿⣿⣟⣽⣿⣿⣿⣿⣟⣵⣿⣿⣿⡿⣳⣫⣾⣿⣿⠟⠻⣿⣿⣿⢻⣿⣿⣿⣿⣷⡽⣿
// ⣿⣟⣾⡿⣿⣿⢟⣽⣿⣿⣿⣿⣫⡾⣵⣿⣿⣿⠃⠄⠄⠘⢿⣿⣾⣿⣿⣿⢿⣿⣿⡜
// ⡿⣼⡟⣾⣿⢫⣿⣿⣿⣿⡿⣳⣿⣱⣿⣿⣿⡋⠄⠄⠄⠄⠄⠛⠛⠋⠁⠄⠄⣿⢸⣿
// ⢳⣟⣼⡿⣳⣿⣿⣿⣿⡿⣹⡿⣃⣿⣿⣿⢳⠁⠄⠄⠄⢀⣀⠄⠄⠄⠄⠄⢀⣿⢿⣿
// ⡟⣼⣿⣱⣿⡿⣿⣿⣿⢡⣫⣾⢸⢿⣿⡟⣿⣶⡶⢰⣿⣿⣿⢷⠄⠄⠄⠄⢼⣿⣸⣿
// ⣽⣿⢣⣿⡟⣽⣿⣿⠃⣲⣿⣿⣸⣷⡻⡇⣿⣿⢇⣿⣿⣿⣏⣎⣸⣦⣠⡞⣾⢧⣿⣿
// ⣿⡏⣿⡿⢰⣿⣿⡏⣼⣿⣿⡏⠙⣿⣿⣤⡿⣿⢸⣿⣿⢟⡞⣰⣿⣿⡟⣹⢯⣿⣿⣿
// ⡿⢹⣿⠇⣿⣿⣿⣸⣿⣿⣿⣿⣦⡈⠻⣿⣿⣮⣿⣿⣯⣏⣼⣿⠿⠏⣰⡅⢸⣿⣿⣿
// ⡀⣼⣿⢰⣿⣿⣇⣿⣿⡿⠛⠛⠛⠛⠄⣘⣿⣿⣿⣿⣿⣿⣶⣿⠿⠛⢾⡇⢸⣿⣿⣿
// ⠄⣿⡟⢸⣿⣿⢻⣿⣿⣷⣶⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⡋⠉⣠⣴⣾⣿⡇⣸⣿⣿⡏
// ⠄⣿⡇⢸⣿⣿⢸⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣄⠘⢿⣿⠏⠄⣿⣿⣿⣹
// ⠄⢻⡇⢸⣿⣿⠸⣿⣿⣿⣿⣿⣿⠿⠿⢿⣿⣿⣿⣿⣿⣿⣿⣦⣼⠃⠄⢰⣿⣿⢯⣿
// ⠄⢸⣿⢸⣿⣿⡄⠙⢿⣿⣿⡿⠁⠄⠄⠄⠄⠉⣿⣿⣿⣿⣿⣿⡏⠄⢀⣾⣿⢯⣿⣿
// ⣾⣸⣿⠄⣿⣿⡇⠄⠄⠙⢿⣀⠄⠄⠄⠄⠄⣰⣿⣿⣿⣿⣿⠟⠄⠄⣼⡿⢫⣻⣿⣿
// ⣿⣿⣿⠄⢸⣿⣿⠄⠄⠄⠄⠙⠿⣷⣶⣤⣴⣿⠿⠿⠛⠉⠄⠄ ⢸⣿⣿⣿⣿⠃⠄⢀⣴

int main() {
  int N;
  cin >> N;

  int res = 0;
  for_ab(i, 1, N) {
    int sqrti = sqrt(i) + 1;
    for_ab(j, 1, sqrti) {
      if ((i % j) == 0) {
        if (j * j != i)
          res += 2;
        else
          ++res;
      }
    }
  }

  cout << res << endl;

  return 0;
}