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

// 4-7-20
#pragma GCC optimize("-O2")
#define _CRT_SECURE_NO_WARNINGS 1

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using namespace std ::chrono;

#define scu(n) scanf("%u", &n)
#define scl(n) scanf("%d", &n)
#define scll(n) scanf("%lld", &n)
#define sz(a) (int)a.size()
#define maxN 10000005
#define uu first
#define vv second
#define ll long long
#define U unsigned
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define Pi acos(-1.0)
#define endl "\\n"
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define EPS 1e-9
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define TEST                                                                   \\
  int tc;                                                                      \\
  cin >> tc;                                                                   \\
  while (tc--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

// Vector Macros
#define pb push_back
#define eb emplace_back
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUM(c) accumulate(ALL(c), 0)

// Loop Macros
#define For(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define fr(i, n) for (int i = 0; i < n; i++)

// Debug Macros
#define open freopen("input.txt", "r", stdin);
#define close freopen("output.txt", "w", stdout);
#define deb(a) cout << #a << " : " << a << " ";
#define debln(a) cout << #a << " : " << a << endl;
// Print Macros
#define print(v)                                                               \\
  for (auto x : v) {                                                           \\
    cout << x << " ";                                                          \\
  }                                                                            \\
  cout << endl;
#define PRINT(v)                                                               \\
  for (auto x : v) {                                                           \\
    cout << x.uu << " " << x.vv << endl;                                       \\
  }

/*
Time Calculation :
auto st = high_resolution_clock::now();
auto en = high_resolution_clock::now();
auto duration = duration_cast < milliseconds > (en-st);
cout<< "consumed time in ms " << duration.count() <<endl;
*/

/*
4
23
1*1 + 2*2 + 3*2 + 4*3 = 23
*/

void solve() {
  int n;
  cin >> n;

  int arr[n + 10] = {0};

  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= n; j += i)
      arr[j]++;
  }

  ll ans = 0;
  for (ll i = 1; i <= n; ++i)
    ans += i * (long long)arr[i];

  cout << ans << endl;
}

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);

  solve();

  return 0;
}


Error: TLE

Fixed submission:

// 4-7-20
#pragma GCC optimize("-O2")
#define _CRT_SECURE_NO_WARNINGS 1

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using namespace std ::chrono;

#define scu(n) scanf("%u", &n)
#define scl(n) scanf("%d", &n)
#define scll(n) scanf("%lld", &n)
#define sz(a) (int)a.size()
#define maxN 10000005
#define uu first
#define vv second
#define ll long long
#define U unsigned
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define Pi acos(-1.0)
#define endl "\\n"
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define EPS 1e-9
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define TEST                                                                   \\
  int tc;                                                                      \\
  cin >> tc;                                                                   \\
  while (tc--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

// Vector Macros
#define pb push_back
#define eb emplace_back
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUM(c) accumulate(ALL(c), 0)

// Loop Macros
#define For(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define fr(i, n) for (int i = 0; i < n; i++)

// Debug Macros
#define open freopen("input.txt", "r", stdin);
#define close freopen("output.txt", "w", stdout);
#define deb(a) cout << #a << " : " << a << " ";
#define debln(a) cout << #a << " : " << a << endl;
// Print Macros
#define print(v)                                                               \\
  for (auto x : v) {                                                           \\
    cout << x << " ";                                                          \\
  }                                                                            \\
  cout << endl;
#define PRINT(v)                                                               \\
  for (auto x : v) {                                                           \\
    cout << x.uu << " " << x.vv << endl;                                       \\
  }

/*
Time Calculation :
auto st = high_resolution_clock::now();
auto en = high_resolution_clock::now();
auto duration = duration_cast < milliseconds > (en-st);
cout<< "consumed time in ms " << duration.count() <<endl;
*/

/*
4
23
1*1 + 2*2 + 3*2 + 4*3 = 23
*/

const ll N = 1e5 + 5;
ll prime_factor[N];
vector<ll> primes;
ll NOD[10001005];

void Sieve() {
  for (ll i = 2; i <= N; ++i) {
    if (prime_factor[i] != 0)
      continue;
    prime_factor[i] = i;

    primes.push_back(i);
    for (ll j = i + i; j <= N; j += i)
      prime_factor[j] = i; // largest prime factor of j
  }
}

void Prime_factoriation() {
  NOD[1] = 1;

  for (ll i = 2; i <= 10000100; ++i) {
    ll x = i, res = 1;

    for (ll j = 0; j < N && primes[j] * primes[j] <= N; j++) {
      ll cnt = 1;
      while (x % primes[j] == 0) {
        // debln(x)
        x /= primes[j];
        cnt++;
        // deb(x) deb(primes[j]) deb(cnt)
      }
      res *= cnt;
    }

    if (x != 1)
      res *= 2;

    // debln(res)
    NOD[i] = res;
    // cout<<"ssfafrasd"<<endl;
  }

  // for(int i=1; i<=10000000; ++i)
  // cout<<i<<" "<<NOD[i]<<endl;
}

void solve() {
  Sieve();
  Prime_factoriation();

  ll n;
  cin >> n;

  ll sum = 0;
  for (ll i = n; i >= 1; --i) {
    sum += (i * NOD[i]);
    // deb(i) deb(NOD[i]) debln(sum)
  }
  cout << sum << endl;
}

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);

  solve();

  return 0;
}