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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, l, r) for (ll i = (l); i <= (ll)(r); i++)
#define rep3(i, l, r) for (ll i = (l); i >= (ll)(r); i--)
#define dup(x, y) (((x) + (y)-1) / (y)) // a/bの除算の切り上げ
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int inf = 1001001001;
const ll INF = 1LL << 60;
const ll mod = 1000000007;
const ld pi = acos(-1);

// a^b=b^a
// a^(b^c)=(a^b)^c
// a^a=0
// a+b=a^b + 2(a&b)
//(4a)^(4a+1)^(4a+2)^(4a+3)=0

// union by size + path having
class UnionFind {
public:
  vector<ll> par; // 各元の親を表す配列
  vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)

  // Constructor
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
    for (ll i = 0; i < sz_; i++)
      par[i] = i; // 初期では親は自分自身
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
    for (ll i = 0; i < sz_; i++)
      par[i] = i; // 初期では親は自分自身
  }

  // Member Function
  // Find
  ll root(ll x) { // 根の検索
    while (par[x] != x) {
      x = par[x] = par[par[x]]; // x の親の親を x の親とする
    }
    return x;
  }

  // Union(Unite, Merge)
  bool unite(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    // merge technique（データ構造をマージするテク．小を大にくっつける)
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool same(ll x, ll y) { // 連結判定
    return root(x) == root(y);
  }

  ll size(ll x) { // 素集合のサイズ
    return siz[root(x)];
  }
};

ll modpow(ll x, ll n) {
  x = x % mod;
  if (n == 0)
    return 1;

  else if (n % 2 == 1) {
    return (x * modpow(x, n - 1)) % mod;
  } else
    return modpow((x * x) % mod, n / 2) % mod;
}

ll com(ll n, ll k) {
  ll x = 1;
  for (int i = n - k + 1; i <= n; i++) {
    x = x * i % mod;
  }

  ll y = 1;
  for (int i = 1; i <= k; i++) {
    y = y * i % mod;
  }
  y = modpow(y, mod - 2);

  return x * y % mod;
}

bool is_prime(const unsigned n) {
  switch (n) {
  case 0: // fall-through
  case 1:
    return false;
  case 2: // fall-through
  case 3:
    return true;
  }

  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (unsigned i = 5; i * i <= n; i += 6) {
    if (n % i == 0)
      return false;
    if (n % (i + 2) == 0)
      return false;
  }

  return true;
}

int digsum(int n) {
  int res = 0;
  while (n > 0) {
    res +=
        n %
        10; // ここをres++;に変えたら桁数を求める関数になる,res+=n%10;なら格桁の和
    n /= 10;
  }
  return res;
}
vector<ll> divisor(ll n) {
  vector<ll> ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}

int main() {

  ll n;
  cin >> n;
  vector<ll> cnt(n);
  for (ll i = 1; i <= n; i++) {
    for (ll j = i; j <= n; j += i) {
      cnt[j]++;
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += (i * cnt[i]);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, l, r) for (ll i = (l); i <= (ll)(r); i++)
#define rep3(i, l, r) for (ll i = (l); i >= (ll)(r); i--)
#define dup(x, y) (((x) + (y)-1) / (y)) // a/bの除算の切り上げ
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int inf = 1001001001;
const ll INF = 1LL << 60;
const ll mod = 1000000007;
const ld pi = acos(-1);

// a^b=b^a
// a^(b^c)=(a^b)^c
// a^a=0
// a+b=a^b + 2(a&b)
//(4a)^(4a+1)^(4a+2)^(4a+3)=0

// union by size + path having
class UnionFind {
public:
  vector<ll> par; // 各元の親を表す配列
  vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)

  // Constructor
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
    for (ll i = 0; i < sz_; i++)
      par[i] = i; // 初期では親は自分自身
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
    for (ll i = 0; i < sz_; i++)
      par[i] = i; // 初期では親は自分自身
  }

  // Member Function
  // Find
  ll root(ll x) { // 根の検索
    while (par[x] != x) {
      x = par[x] = par[par[x]]; // x の親の親を x の親とする
    }
    return x;
  }

  // Union(Unite, Merge)
  bool unite(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    // merge technique（データ構造をマージするテク．小を大にくっつける)
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool same(ll x, ll y) { // 連結判定
    return root(x) == root(y);
  }

  ll size(ll x) { // 素集合のサイズ
    return siz[root(x)];
  }
};

ll modpow(ll x, ll n) {
  x = x % mod;
  if (n == 0)
    return 1;

  else if (n % 2 == 1) {
    return (x * modpow(x, n - 1)) % mod;
  } else
    return modpow((x * x) % mod, n / 2) % mod;
}

ll com(ll n, ll k) {
  ll x = 1;
  for (int i = n - k + 1; i <= n; i++) {
    x = x * i % mod;
  }

  ll y = 1;
  for (int i = 1; i <= k; i++) {
    y = y * i % mod;
  }
  y = modpow(y, mod - 2);

  return x * y % mod;
}

bool is_prime(const unsigned n) {
  switch (n) {
  case 0: // fall-through
  case 1:
    return false;
  case 2: // fall-through
  case 3:
    return true;
  }

  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (unsigned i = 5; i * i <= n; i += 6) {
    if (n % i == 0)
      return false;
    if (n % (i + 2) == 0)
      return false;
  }

  return true;
}

int digsum(int n) {
  int res = 0;
  while (n > 0) {
    res +=
        n %
        10; // ここをres++;に変えたら桁数を求める関数になる,res+=n%10;なら格桁の和
    n /= 10;
  }
  return res;
}
vector<ll> divisor(ll n) {
  vector<ll> ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}

int main() {

  ll n;
  ll ans = 0;
  cin >> n;
  if (n < 1000000) {
    rep(i, n) {
      vector<ll> x;
      x = divisor(i + 1);
      ans += (x.size() * (i + 1));
    }
  }

  else if (n >= 1000000 && n < 1500000) {
    ans = 7235062807366;

    for (ll i = 1000001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 1500000 && n < 2000000) {
    ans = 16734924755096;

    for (ll i = 1500001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 2000000 && n < 2500000) {
    ans = 30326413365285;

    for (ll i = 2000001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 2500000 && n < 3000000) {
    ans = 48081994935671;

    for (ll i = 2500001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 3000000 && n < 4000000) {
    ans = 70058785460280;

    for (ll i = 3000001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 3500000 && n < 4000000) {
    ans = 96301520878991;

    for (ll i = 3500001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 4000000 && n < 5000000) {
    ans = 126850549017804;

    for (ll i = 4000001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 4500000 && n < 5000000) {
    ans = 161737467121483;

    for (ll i = 4500001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  }

  else if (n >= 5000000 && n < 6000000) {
    ans = 200992441246692;

    for (ll i = 5000001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 5500000 && n < 6000000) {
    ans = 244642548171917;

    for (ll i = 5500001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 6000000 && n < 7000000) {
    ans = 292711324081941;

    for (ll i = 6000001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 6500000 && n < 7000000) {
    ans = 345219904513529;

    for (ll i = 6500001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 7000000 && n < 8000000) {
    ans = 402188826344049;

    for (ll i = 7000001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 7500000 && n < 8000000) {
    ans = 463635895079489;

    for (ll i = 7500001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  }

  else if (n >= 8000000 && n < 9000000) {
    ans = 529581632274134;

    for (ll i = 8000001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 8500000 && n < 9000000) {
    ans = 600036645526326;

    for (ll i = 8500001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 9000000 && n < 10000000) {
    ans = 675021515919670;

    for (ll i = 9000001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n >= 9500000 && n < 10000000) {
    ans = 754546104527913;

    for (ll i = 9500001; i <= n; i++) {
      vector<ll> x;
      x = divisor(i);
      ans += (x.size() * i);
    }
  } else if (n == 10000000) {
    cout << 838627288460105 << endl;
    return 0;
  }

  cout << ans << endl;
}