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

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

#define ll long long
#define ull unsigned long long
#define lld long double
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define vt vector
#define vll vt<ll>
#define pll pair<ll, ll>
#define vpll vt<pll>
#define vvll vt<vll>
#define all(v) v.begin(), v.end()
#define FOR(i, n) for (ll i = 0; i < n; i++)
#define ffo(i, a, b) for (ll i = a; i <= b; i++)
#define rfo(i, a, b) for (ll i = a; i >= b; i--)
#define space cout << "\\n\\n";
#define endl '\\n'
template <typename T> using mxpq = priority_queue<T>;
template <typename T> using mnpq = priority_queue<T, vt<T>, greater<T>>;
#define fps(x, y) fixed << setprecision(y) << x
#define merg(a, b, c)                                                          \\
  set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()))
#define mmss(arr, v) memset(arr, v, sizeof(arr))
#define go_t                                                                   \\
  int testcases;                                                               \\
  cin >> testcases;                                                            \\
  ffo(caseno, 1, testcases)

#define ctime auto start = high_resolution_clock::now()
#define etime auto stop = high_resolution_clock::now()
#define ptime                                                                  \\
  auto z1z = duration_cast<microseconds>(stop - start);                        \\
  cout << "Time elapsed : " << z1z.count() << " microseconds\\n"

const ll mod = 1e9 + 7;
const ll N = 6e4 + 6;
const ll maxN = 1e6 + 15;
const ll MAX_SIZE = 2e6 + 6;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const double PI = 3.14159265359;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
// up, right, down, left
// int dx [] = {+1,-1,+0,+0,-1,-1,+1,+1};///Eight Directions
// int dy [] = {+0,+0,+1,-1,+1,-1,-1,+1};///Eight Directions
// int dx[]={-2,-2,-1,1,-1,1,2,2};///Knight moves
// int dy[]={1,-1,-2,-2,2,2,-1,1};///Knight moves

ll powerM(ll x, ll y, ll M = mod) { // default argument
  ll v = 1;
  x = x % M;
  while (y > 0) {
    if (y & 1)
      v = (v * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return v;
}

ll power(ll x, ll y) {
  ll v = 1;
  while (y > 0) {
    if (y & 1)
      v = v * x;
    y = y >> 1;
    x = x * x;
  }
  return v;
}

vt<int> spf(maxN);
void spfsieve() {
  for (int i = 2; i < maxN; i += 2)
    spf[i] = 2;
  for (int i = 3; i < maxN; i += 2) {
    if (spf[i] == 0) {
      spf[i] = i;
      for (int j = 2 * i; j < maxN; j += i) {
        if (spf[j] == 0)
          spf[j] = i;
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in4.txt", "r", stdin);
  freopen("out1.txt", "w", stdout);
#endif
  FIO spfsieve();
  ll n;
  cin >> n;
  ll ans = 0;
  for (int c = 1; c < n; ++c) {
    ll v = n - c;
    if (v == 1) {
      ans++;
      continue;
    }
    ll div = 1;
    map<ll, ll> mp;
    while (v != 1) {
      mp[spf[v]]++;
      v = v / spf[v];
    }
    for (auto it : mp) {
      div *= (it.ss + 1);
    }
    ans += (div);
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define ull unsigned long long
#define lld long double
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define vt vector
#define vll vt<ll>
#define pll pair<ll, ll>
#define vpll vt<pll>
#define vvll vt<vll>
#define all(v) v.begin(), v.end()
#define FOR(i, n) for (ll i = 0; i < n; i++)
#define ffo(i, a, b) for (ll i = a; i <= b; i++)
#define rfo(i, a, b) for (ll i = a; i >= b; i--)
#define space cout << "\\n\\n";
#define endl '\\n'
template <typename T> using mxpq = priority_queue<T>;
template <typename T> using mnpq = priority_queue<T, vt<T>, greater<T>>;
#define fps(x, y) fixed << setprecision(y) << x
#define merg(a, b, c)                                                          \\
  set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()))
#define mmss(arr, v) memset(arr, v, sizeof(arr))
#define go_t                                                                   \\
  int testcases;                                                               \\
  cin >> testcases;                                                            \\
  ffo(caseno, 1, testcases)

#define ctime auto start = high_resolution_clock::now()
#define etime auto stop = high_resolution_clock::now()
#define ptime                                                                  \\
  auto z1z = duration_cast<microseconds>(stop - start);                        \\
  cout << "Time elapsed : " << z1z.count() << " microseconds\\n"

const ll mod = 1e9 + 7;
const ll N = 6e4 + 6;
const ll maxN = 1e6 + 15;
const ll MAX_SIZE = 2e6 + 6;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const double PI = 3.14159265359;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
// up, right, down, left
// int dx [] = {+1,-1,+0,+0,-1,-1,+1,+1};///Eight Directions
// int dy [] = {+0,+0,+1,-1,+1,-1,-1,+1};///Eight Directions
// int dx[]={-2,-2,-1,1,-1,1,2,2};///Knight moves
// int dy[]={1,-1,-2,-2,2,2,-1,1};///Knight moves

ll powerM(ll x, ll y, ll M = mod) { // default argument
  ll v = 1;
  x = x % M;
  while (y > 0) {
    if (y & 1)
      v = (v * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return v;
}

ll power(ll x, ll y) {
  ll v = 1;
  while (y > 0) {
    if (y & 1)
      v = v * x;
    y = y >> 1;
    x = x * x;
  }
  return v;
}

vt<int> spf(maxN);
void spfsieve() {
  for (int i = 2; i < maxN; i += 2)
    spf[i] = 2;
  for (int i = 3; i < maxN; i += 2) {
    if (spf[i] == 0) {
      spf[i] = i;
      for (int j = 2 * i; j < maxN; j += i) {
        if (spf[j] == 0)
          spf[j] = i;
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in4.txt", "r", stdin);
  freopen("out1.txt", "w", stdout);
#endif
  FIO spfsieve();
  ll n;
  cin >> n;
  ll ans = 0;
  for (int c = 1; c < n; ++c) {
    ll v = n - c;
    if (v == 1) {
      ans++;
      continue;
    }
    ll div = 0;
    for (int i = 1; i * i <= v; ++i) {
      if (v % i == 0) {
        div++;
        if (i != v / i)
          div++;
      }
    }
    ans += (div);
  }
  cout << ans;
  return 0;
}