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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define intarr(arr, n)                                                         \\
  ll arr[n];                                                                   \\
  for (ll i = 0; i < n; i++)                                                   \\
  cin >> arr[i]
#define inpvec(vec, n)                                                         \\
  for (ll i = 0; i < n; i++) {                                                 \\
    ll var;                                                                    \\
    cin >> var;                                                                \\
    vec.push_back(var);                                                        \\
  }
#define ff first
#define ss second
#define pb push_back
#define mkp make_pair
#define vec vector<ll>
#define pi pair<ll, ll>
#define changeline cout << endl
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define rev(i, a, b) for (ll i = a; i >= b; i--)
#define all(v) v.begin(), v.end()
#define mod2 998244353
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
#define mem(arr, val) memset(arr, val, sizeof(arr));
const ll mod = 1e9 + 7;
#define piii 3.14159265358979323846264338327950
#define print(a, n, m)                                                         \\
  for (ll i = n; i < m; i++) {                                                 \\
    cout << a[i] << " ";                                                       \\
  }                                                                            \\
  cout << endl

bool prime(ll n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (ll i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
bool subsequence_checker(string str1, string str2, ll m, ll n) {
  if (m == 0)
    return true;
  if (n == 0)
    return false;
  if (str1[m - 1] == str2[n - 1])
    return subsequence_checker(str1, str2, m - 1, n - 1);
  return subsequence_checker(str1, str2, m, n - 1);
}
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.ff == b.ff)
    return a.ss > b.ss;
  return (a.ff < b.ff);
}

//////////////////////////////////////////////////// Lets Do It
///////////////////////////////////////////////////////////

void SieveOfEratosthenes(ll n, bool prime[], bool primesquare[], ll a[]) {
  for (ll i = 2; i <= n; i++)
    prime[i] = true;

  for (ll i = 0; i <= (n * n + 1); i++)
    primesquare[i] = false;

  // 1 is not a prime number
  prime[1] = false;

  for (ll p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then
    // it is a prime
    if (prime[p] == true) {
      // Update all multiples of p
      for (ll i = p * 2; i <= n; i += p)
        prime[i] = false;
    }
  }

  ll j = 0;
  for (ll p = 2; p <= n; p++) {
    if (prime[p]) {
      // Storing primes in an array
      a[j] = p;

      // Update value in primesquare[p*p],
      // if p is prime.
      primesquare[p * p] = true;
      j++;
    }
  }
}

// Function to count divisors
ll countDivisors(ll n) {

  if (n == 1)
    return 1;

  bool prime[n + 1], primesquare[n * n + 1];

  ll a[n]; // for storing primes upto n

  SieveOfEratosthenes(n, prime, primesquare, a);
  ll ans = 1;

  for (ll i = 0;; i++) {

    if (a[i] * a[i] * a[i] > n)
      break;

    ll cnt = 1;           // cnt is power of prime a[i] in n.
    while (n % a[i] == 0) // if a[i] is a factor of n
    {
      n = n / a[i];
      cnt = cnt + 1; // incrementing power
    }

    ans = ans * cnt;
  }

  if (prime[n])
    ans = ans * 2;
  else if (primesquare[n])
    ans = ans * 3;

  else if (n != 1)
    ans = ans * 4;

  return ans;
}

vec d(10000002);
void solve() {

  ll n;
  cin >> n;
  loop(i, 1, n + 1) {
    for (ll j = i; j <= n; j += i)
      d[j]++;
  }
  ll ans = 0;
  loop(i, 1, n + 1) { ans += i * d[i]; }
  cout << ans;

  return;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  ll t;
  // cin>>t;
  t = 1;

  while (t--) {
    solve();
    cout << endl;
  }

  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define intarr(arr, n)                                                         \\
  ll arr[n];                                                                   \\
  for (ll i = 0; i < n; i++)                                                   \\
  cin >> arr[i]
#define inpvec(vec, n)                                                         \\
  for (ll i = 0; i < n; i++) {                                                 \\
    ll var;                                                                    \\
    cin >> var;                                                                \\
    vec.push_back(var);                                                        \\
  }
#define ff first
#define ss second
#define pb push_back
#define mkp make_pair
#define vec vector<ll>
#define pi pair<ll, ll>
#define changeline cout << endl
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define rev(i, a, b) for (ll i = a; i >= b; i--)
#define all(v) v.begin(), v.end()
#define mod2 998244353
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
#define mem(arr, val) memset(arr, val, sizeof(arr));
const ll mod = 1e9 + 7;
#define piii 3.14159265358979323846264338327950
#define print(a, n, m)                                                         \\
  for (ll i = n; i < m; i++) {                                                 \\
    cout << a[i] << " ";                                                       \\
  }                                                                            \\
  cout << endl

bool prime(ll n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (ll i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
bool subsequence_checker(string str1, string str2, ll m, ll n) {
  if (m == 0)
    return true;
  if (n == 0)
    return false;
  if (str1[m - 1] == str2[n - 1])
    return subsequence_checker(str1, str2, m - 1, n - 1);
  return subsequence_checker(str1, str2, m, n - 1);
}
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.ff == b.ff)
    return a.ss > b.ss;
  return (a.ff < b.ff);
}

//////////////////////////////////////////////////// Lets Do It
///////////////////////////////////////////////////////////

void SieveOfEratosthenes(ll n, bool prime[], bool primesquare[], ll a[]) {
  for (ll i = 2; i <= n; i++)
    prime[i] = true;

  for (ll i = 0; i <= (n * n + 1); i++)
    primesquare[i] = false;

  // 1 is not a prime number
  prime[1] = false;

  for (ll p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then
    // it is a prime
    if (prime[p] == true) {
      // Update all multiples of p
      for (ll i = p * 2; i <= n; i += p)
        prime[i] = false;
    }
  }

  ll j = 0;
  for (ll p = 2; p <= n; p++) {
    if (prime[p]) {
      // Storing primes in an array
      a[j] = p;

      // Update value in primesquare[p*p],
      // if p is prime.
      primesquare[p * p] = true;
      j++;
    }
  }
}

// Function to count divisors
ll countDivisors(ll n) {

  if (n == 1)
    return 1;

  bool prime[n + 1], primesquare[n * n + 1];

  ll a[n]; // for storing primes upto n

  SieveOfEratosthenes(n, prime, primesquare, a);
  ll ans = 1;

  for (ll i = 0;; i++) {

    if (a[i] * a[i] * a[i] > n)
      break;

    ll cnt = 1;           // cnt is power of prime a[i] in n.
    while (n % a[i] == 0) // if a[i] is a factor of n
    {
      n = n / a[i];
      cnt = cnt + 1; // incrementing power
    }

    ans = ans * cnt;
  }

  if (prime[n])
    ans = ans * 2;
  else if (primesquare[n])
    ans = ans * 3;

  else if (n != 1)
    ans = ans * 4;

  return ans;
}

void solve() {

  ll n;
  cin >> n;
  ll ans = 0;

  loop(i, 1, n + 1) {
    ll c = countDivisors(i);
    ans += i * c;
  }
  cout << ans;
  return;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  ll t;
  // cin>>t;
  t = 1;

  while (t--) {
    solve();
    cout << endl;
  }

  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
  return 0;
}