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

// Author : Harshdeep Sharma , IIT Indore
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
// #define MOD  998244353
#define ll long long
const double PI = atan(1.0) * 4;
// const ll INF = (int)1e9 ;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, -1, 0, 1};
const ll INF = (ll)2e18 + 50;
const int maximum = numeric_limits<int>::min();
const int minimum = numeric_limits<int>::max();
const int MOD = 1e9 + 7;
const int N = 1e7 + 50;
ll c[N];

// function to calculate any numbered power of some number without modulo
ll power(ll a, ll e) {
  ll res = 1LL;
  while (e > 0) {
    if (e % 2 == 1)
      res = res * a;
    a = a * a;
    e /= 2;
  }
  return res;
}
// function to check whether a bracketed sequence is balanced or not
bool isBalanced(const string &exp) {
  // Initialising Variables
  bool flag = true;
  int count = 0;

  // Traversing the Expression
  for (int i = 0; i < exp.length(); i++) {

    if (exp[i] == '(') {
      count++;
    } else {
      count--;
    }
    if (count < 0) {
      flag = false;
      break;
    }
  }
  if (count != 0) {
    flag = false;
  }

  return flag;
}
// function to sort by second in a set of pairs
bool sortbysec(const pair<pair<int, int>, int> &a,
               const pair<pair<int, int>, int> &b) {
  return (a.second < b.second);
}

void uniformly_distribute_k_numbers_in_a_square_grid(int n, int k) {
  int p = 0, q = 0;
  vector<vector<int>> a(n, vector<int>(n));
  for (int i = 1; i <= k; ++i) {
    a[p][q] = 1;
    p = p + 1;
    q = (q + 1) % n;
    if (p == n) {
      p = 0;
      q = (q + 1) % n;
    }
  }
}

// function to calculate gcd
ll gcd(int a, int b) {
  // Everything divides 0
  if (a == 0)
    return b;
  if (b == 0)
    return a;

  // base case
  if (a == b)
    return a;

  // a is greater
  if (a > b)
    return gcd(a - b, b);
  return gcd(a, b - a);
}

int mex(const vector<int> &a) {
  int res;
  int n = a.size();
  vector<int> cnt(n + 1);
  for (int i = 0; i < n; i++) {
    ++cnt[a[i]];
  }
  for (int i = 0; i < n; i++) {
    if (cnt[i] == 0) {
      res = i;
      break;
    }
  }
  return res;
}
// void C_i_j(int n) {

// 	for (int i = 0; i < n; i++) {
// 		for (int j = 0; j <= i; j++) {
// 			if (i == j || j == 0)
// 				c[i][j] = 1;
// 			else
// 				c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
// 		}
// 	}
// }

ll summation(int n) { return n * (n + 1) / 2; }

void f(int n) {

  for (int i = 1; i < n; i++) {
    c[i] = 1;
  }
  for (int i = 2; i < n; i++) {
    // if (c[i] == 0) {
    for (int j = i; j < n; j += i) {
      c[j]++;
    }
    // }
  }
}

// main funtion
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  f(N);
  // int t ;
  // cin >> t ;
  // while (t--) {
  int n;
  cin >> n;
  ll ans = 0;

  for (int i = 1; i <= n; i++) {
    ans += i * c[i];
  }

  cout << ans << "\\n";

  // }

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define MOD 998244353
// #define MOD ll(1e9) + 7
#define ll long long
const double PI = atan(1.0) * 4;
// const int N = 1e5 + 5 ;
// const ll INF = (int)1e9 ;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
// const ll INF = (ll)2e18 + 50;
const ll INF = 1e9;
// const ll MOD = 1e9 + 7 ;
const int M = 1e7 + 50;

void gg() {
  cout << 0 << "\\n";
  exit(0);
}

ll power(ll a, ll e) {
  ll res = 1LL;
  while (e > 0) {
    if (e % 2 == 1)
      res = res * a % MOD;
    a = a * a % MOD;
    e /= 2;
  }
  return res;
}

vector<int> complement(const vector<int> &vec) {
  int n = vec.size();
  vector<bool> occur(n + 1, true);
  vector<int> ans;
  for (int i = 0; i < (int)vec.size(); i++) {
    occur[vec[i]] = false;
  }
  for (int i = 1; i <= n; i++) {
    if (occur[i])
      ans.push_back(i);
  }
  return ans;
}
bool compareSeq(const string &S, int x, int y, int n) {
  for (int i = 0; i < n; i++) {
    if (S[x] < S[y])
      return false;
    else if (S[x] > S[y])
      return true;
    x = (x + 1) % n;
    y = (y + 1) % n;
  }
  return true;
}

// Function to find starting index
// of lexicographically smallest sequence
int smallestSequence(const string &S, int n) {
  int index = 0;
  for (int i = 1; i < n; i++)

    // if new sequence is smaller
    if (compareSeq(S, index, i, n))

      // change index of current min
      index = i;

  return index;
}

// Function to print lexicographically
// smallest sequence
void printSmallestSequence(const string &S, int n) {
  int starting_index = smallestSequence(S, n);
  for (int i = 0; i < n; i++)
    cout << S[(starting_index + i) % n];
}
int countDivisors(int n) {
  int cnt = 0;
  for (int i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      // If divisors are equal,
      // count only one
      if (n / i == i)
        cnt++;

      else // Otherwise count both
        cnt = cnt + 2;
    }
  }
  return cnt;
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // int t ;
  // cin >> t;
  // while (t--) {
  int n;
  cin >> n;
  // vector<int> a(M) ;
  // for (int i = 0 ; i < M ; i++) {
  // 	a[i] = i + 1 ;
  // }
  // vector<int> cnt(M) ;
  // for (int x : a) {
  // 	if (cnt[x]) {
  // 		cnt[x] = 2 ;
  // 		continue ;
  // 	}
  // 	for (int i = x ; i < M ; i += x) {
  // 		cnt[i]++ ;
  // 	}
  // }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += i * countDivisors(i);
  }
  cout << ans << "\\n";

  // }

  // }
  return 0;
}