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

#include <algorithm>
#include <chrono>
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using namespace chrono;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

// #define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define loop(x, n) for (int x = 0; x < n; ++x)
#define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)

#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 100001;

string get_str() {
  string temp;
  cin >> temp;
  return temp;
}
int get_int() {
  int temp;
  cin >> temp;
  return temp;
}

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

int main() {
  int m;

  cin >> m;
  ll ans = 0;

  for (ll i = 1; i <= m; ++i) {
    ans += i * fac(m / i);
  }

  /*
      for(int i=1; i <= m; ++i) {
          for(int j=1; j <= m; ++j) {
              if(i%j == 0) ans += 1;
          }
      }
  */
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using namespace chrono;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

// #define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define loop(x, n) for (int x = 0; x < n; ++x)
#define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)

#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 100001;

string get_str() {
  string temp;
  cin >> temp;
  return temp;
}
int get_int() {
  int temp;
  cin >> temp;
  return temp;
}

ll func(int n) {

  if (n == 1)
    return 1;

  vector<int> primes;
  int x = 0;
  ll ans = 1;

  while (n % 2 == 0) {
    primes.pb(2);
    x++;
    n = n / 2;
  }

  ans *= (x + 1);

  for (int i = 3; i <= sqrt(n); i = i + 2) {
    x = 0;
    while (n % i == 0) {
      primes.pb(i);
      n = n / i;
      x++;
    }
    ans *= (x + 1);
  }

  if (n > 2) {
    primes.pb(n);
    ans *= 2;
  }

  // for(auto i : primes) cout << i << endl;
  return ans;
}

int main() {
  int m;

  cin >> m;
  ll ans = 0;

  for (int i = 1; i <= m; ++i) {
    // cout << i << " ";
    ans += (i)*func(i);
    // cout << (i) * func(i) << endl;
  }

  cout << ans << endl;
}