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

/**
 *@Author : Shreyas Pandey
 *Team : AntiPython
 *[2020-09-19 10:16]
 **/

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vll vector<ll>
#define vvll vector<vll> // vector<vector<vll>>
#define vld vector<ld>
#define vvld vector<vld> // vector <vector <long double>>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define endl "\\n"
#define flash                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define test                                                                   \\
  ll t;                                                                        \\
  read(t);                                                                     \\
  while (t--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define fst first
#define scd second

#define forn(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define for1(i, n) for (ll(i) = 1; (i) <= (n); ++(i))
#define forr(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))
#define forab(i, a, b, c) for (ll(i) = a; (i) <= (b); (i) += (c))

using namespace std;
void online_judge() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}

////////////////////////////////////////////////////////////////// Inputs
///////////////////////////////////////////////////////////////////////////////////////
template <typename T> void read(T &x) { cin >> x; }

template <typename T, typename T0> void read(T &x, T0 &y) { cin >> x >> y; }

template <typename T, typename T0, typename T1> void read(T &x, T0 &y, T1 &z) {
  cin >> x >> y >> z;
}

template <typename T, typename T0, typename T1, typename T2>
void read(T &x, T0 &y, T1 &z, T2 &w) {
  cin >> x >> y >> z >> w;
}

template <typename T, typename T0> void read(pair<T, T0> &p) {
  cin >> p.fst >> p.scd;
}

template <typename T> void read(vector<T> &oneD) {
  for (ll i = 0; i < oneD.size(); i++) {
    read(oneD[i]);
  }
}

template <typename T> void read(T oneD[], int n) {
  for (ll i = 0; i < n; i++) {
    read(oneD[i]);
  }
}

///////////////////////////////////////////////////////////////////////////
///Outputs
///////////////////////////////////////////////////////////////////////////////

template <typename T> void write(T &x) { cout << x << " "; }

template <typename T, typename T0> void write(T &x, T0 &y) {
  cout << x << " " << y << "\\n";
}

template <typename T, typename T0, typename T1> void write(T &x, T0 &y, T1 &z) {
  cout << x << " " << y << " " << z << "\\n";
}

template <typename T, typename T0, typename T1, typename T2>
void write(T &x, T0 &y, T1 &z, T2 &w) {
  cout << x << " " << y << " " << z << " " << w << "\\n";
}

template <typename T, typename T0> void write(pair<T, T0> &p) {
  write(p.fst);
  write(p.scd);
  cout << endl;
}

template <typename T> void write(vector<T> &oneD) {
  for (ll i = 0; i < oneD.size(); i++) {
    write(oneD[i]);
  }
  cout << endl;
}

template <typename T> void write(T oneD[], int n) {
  for (ll i = 0; i < n; i++) {
    write(oneD[i]);
  }
  cout << endl;
}

template <typename T, typename T0> void write(map<T, T0> &mpp) {
  for (auto it : mpp) {
    write(it.fst);
    cout << ": ";
    write(it.scd);
    cout << "\\n";
  }
  cout << endl;
}

///////////////////////////////////////////////////////////////////////// Maths
/////////////////////////////////////////////////////////////////////////////////

vll sieve;
void Sieve(int N) {
  const ll maxn = N;
  sieve.resize(maxn);
  forn(i, maxn) sieve[i] = i;
  sieve[1] = -1;
  sieve[0] = -1;
  forab(i, 2, maxn, 1) if (i == sieve[i]) for (ll j = 2 * i; j < maxn;
                                               j += i) if (sieve[j] == j)
      sieve[j] = i;
}

ll extended_GCD(ll a, ll b, ll &x, ll &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  ll x1, y1;
  ll gcd = extended_GCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod = MOD) {
  ll x, y;
  extended_GCD(a, mod, x, y);
  if (x < 0)
    x += mod;
  return x;
}
ll int sum_form(ll int k) { return ((k * (k + 1)) / 2); }
ll int binarySearch(ll int l, ll int r, ll int n) {
  ll ans = -1;
  while (l <= r) {
    ll int mid = l + (r - l) / 2;
    ll sum_l = sum_form(mid);
    ll sum_r = sum_form(n) - sum_l;
    if (sum_l == sum_r) {
      ans = mid;
      return ans;
    } else if (sum_l > sum_r) {
      r = mid - 1;
    } else {
      ans = mid;
      l = mid + 1;
    }
  }
  return ans;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////Segment
///Tree//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  for (int i = 1; i < n; ++i) {
    ans += (n - 1) / i;
  }
  cout << ans << "\\n";
}

int main() {
  flash online_judge();
  solve();
  return 0;
}


Error: TLE

Fixed submission:

/**
 *@Author : Shreyas Pandey
 *Team : AntiPython
 *[2020-09-19 10:16]
 **/

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vll vector<ll>
#define vvll vector<vll> // vector<vector<vll>>
#define vld vector<ld>
#define vvld vector<vld> // vector <vector <long double>>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define endl "\\n"
#define flash                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define test                                                                   \\
  ll t;                                                                        \\
  read(t);                                                                     \\
  while (t--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define fst first
#define scd second

#define forn(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define for1(i, n) for (ll(i) = 1; (i) <= (n); ++(i))
#define forr(i, n) for (ll(i) = (n)-1; (i) >= 0; --(i))
#define forab(i, a, b, c) for (ll(i) = a; (i) <= (b); (i) += (c))

using namespace std;
void online_judge() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}

////////////////////////////////////////////////////////////////// Inputs
///////////////////////////////////////////////////////////////////////////////////////
template <typename T> void read(T &x) { cin >> x; }

template <typename T, typename T0> void read(T &x, T0 &y) { cin >> x >> y; }

template <typename T, typename T0, typename T1> void read(T &x, T0 &y, T1 &z) {
  cin >> x >> y >> z;
}

template <typename T, typename T0, typename T1, typename T2>
void read(T &x, T0 &y, T1 &z, T2 &w) {
  cin >> x >> y >> z >> w;
}

template <typename T, typename T0> void read(pair<T, T0> &p) {
  cin >> p.fst >> p.scd;
}

template <typename T> void read(vector<T> &oneD) {
  for (ll i = 0; i < oneD.size(); i++) {
    read(oneD[i]);
  }
}

template <typename T> void read(T oneD[], int n) {
  for (ll i = 0; i < n; i++) {
    read(oneD[i]);
  }
}

///////////////////////////////////////////////////////////////////////////
///Outputs
///////////////////////////////////////////////////////////////////////////////

template <typename T> void write(T &x) { cout << x << " "; }

template <typename T, typename T0> void write(T &x, T0 &y) {
  cout << x << " " << y << "\\n";
}

template <typename T, typename T0, typename T1> void write(T &x, T0 &y, T1 &z) {
  cout << x << " " << y << " " << z << "\\n";
}

template <typename T, typename T0, typename T1, typename T2>
void write(T &x, T0 &y, T1 &z, T2 &w) {
  cout << x << " " << y << " " << z << " " << w << "\\n";
}

template <typename T, typename T0> void write(pair<T, T0> &p) {
  write(p.fst);
  write(p.scd);
  cout << endl;
}

template <typename T> void write(vector<T> &oneD) {
  for (ll i = 0; i < oneD.size(); i++) {
    write(oneD[i]);
  }
  cout << endl;
}

template <typename T> void write(T oneD[], int n) {
  for (ll i = 0; i < n; i++) {
    write(oneD[i]);
  }
  cout << endl;
}

template <typename T, typename T0> void write(map<T, T0> &mpp) {
  for (auto it : mpp) {
    write(it.fst);
    cout << ": ";
    write(it.scd);
    cout << "\\n";
  }
  cout << endl;
}

///////////////////////////////////////////////////////////////////////// Maths
/////////////////////////////////////////////////////////////////////////////////

vll sieve;
void Sieve(int N) {
  const ll maxn = N;
  sieve.resize(maxn);
  forn(i, maxn) sieve[i] = i;
  sieve[1] = -1;
  sieve[0] = -1;
  forab(i, 2, maxn, 1) if (i == sieve[i]) for (ll j = 2 * i; j < maxn;
                                               j += i) if (sieve[j] == j)
      sieve[j] = i;
}

ll extended_GCD(ll a, ll b, ll &x, ll &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  ll x1, y1;
  ll gcd = extended_GCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod = MOD) {
  ll x, y;
  extended_GCD(a, mod, x, y);
  if (x < 0)
    x += mod;
  return x;
}
ll int sum_form(ll int k) { return ((k * (k + 1)) / 2); }
ll int binarySearch(ll int l, ll int r, ll int n) {
  ll ans = -1;
  while (l <= r) {
    ll int mid = l + (r - l) / 2;
    ll sum_l = sum_form(mid);
    ll sum_r = sum_form(n) - sum_l;
    if (sum_l == sum_r) {
      ans = mid;
      return ans;
    } else if (sum_l > sum_r) {
      r = mid - 1;
    } else {
      ans = mid;
      l = mid + 1;
    }
  }
  return ans;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////Segment
///Tree//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void solve() {
  int n;
  cin >> n;
  int cost = 0;
  for (int i = 1; i < n; ++i) {
    ll x = n - i;
    for (int j = 1; j * j <= x; j++) {
      if (x % j == 0) {
        if (j == x / j) {
          cost++;
        } else {
          cost += 2;
        }
      }
    }
  }
  cout << cost << "\\n";
}

int main() {
  flash online_judge();
  solve();
  return 0;
}