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


#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define ull unsigned long long int
#define db double
#define ld long double
#define MOD 1000000007
#define inf (1LL << 62)
#define pi acos(-1.0)
#define si(a) scanf("%lld", &a)
#define sd(n) scanf("%lf", &n)
#define for1(i, stop) for (ll i = 1; i <= stop; i++)
#define for0(i, stop) for (ll i = 0; i < stop; i++)
#define rep1(i, start) for (ll i = start; i >= 1; i--)
#define rep0(i, start) for (ll i = (start - 1); i >= 0; i--)
#define loop(i, start, stop, inc) for (ll i = start; i <= stop; i += inc)
#define pb(v, a) v.push_back(a)
#define pll pair<ll, ll>
#define mp make_pair
#define pbp(v, a, i) v.push_back(make_pair(a, i))
#define srt(v) sort(v.begin(), v.end())
#define rv(v) reverse(v.begin(), v.end())
#define ms(n, i) memset(n, i, sizeof(n))
#define debug printf("Debug\\n");
#define en '\\n'

#define _fastio                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
ll divisors[10000003];
int main() {

  ll n, sm = 0;
  ;
  cin >> n;
  for (ll i = 1; i <= n; ++i)
    for (ll j = i; j <= n; j += i)
      ++divisors[j];
  for (ll i = 1; i <= n; i++)
    sm += i * divisors[i];
  cout << sm << en;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define ull unsigned long long int
#define db double
#define ld long double
#define MOD 1000000007
#define inf (1LL << 62)
#define pi acos(-1.0)
#define si(a) scanf("%lld", &a)
#define sd(n) scanf("%lf", &n)
#define for1(i, stop) for (ll i = 1; i <= stop; i++)
#define for0(i, stop) for (ll i = 0; i < stop; i++)
#define rep1(i, start) for (ll i = start; i >= 1; i--)
#define rep0(i, start) for (ll i = (start - 1); i >= 0; i--)
#define loop(i, start, stop, inc) for (ll i = start; i <= stop; i += inc)
#define pb(v, a) v.push_back(a)
#define pll pair<ll, ll>
#define mp make_pair
#define pbp(v, a, i) v.push_back(make_pair(a, i))
#define srt(v) sort(v.begin(), v.end())
#define rv(v) reverse(v.begin(), v.end())
#define ms(n, i) memset(n, i, sizeof(n))
#define debug printf("Debug\\n");
#define en '\\n'

#define _fastio                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

map<ll, ll> m;
vector<ll> a, b;

#define Max 10000005
bool st[Max];
vector<ll> prime;

void Sieve() {
  ll sq = sqrt(Max);
  st[1] = 1;
  for (ll i = 4; i < Max; i += 2) {
    st[i] = 1;
  }
  prime.push_back(2);
  for (ll i = 3; i < sq; i += 2) {
    if (st[i] != 1) {
      for (ll j = i * i; j <= Max; j += i + i)
        st[i] = 1;
    }
  }
  for (ll i = 3; i < Max; i += 2) {
    if (st[i] == 1)
      prime.push_back(i);
  }
}

int main() {
  Sieve();
  ll n, ans, t, sm = 0;
  scanf("%lld", &t);
  for (ll ca = 1; ca <= t; ca++) {
    vector<pair<ll, ll>> fac;
    n = ca;
    ans = 1;
    ll sq = sqrt(n);
    ll num = n;
    for (ll i = 0; i < prime.size() && prime[i] <= sq; i++) {
      if (num % prime[i] == 0) {
        ll cnt = 0;
        while (num % prime[i] == 0) {
          num /= prime[i];
          cnt++;
        }
        ans = ans * (cnt + 1);
      }
    }
    if (num != 1) {
      ans = ans * (2);
    }
    sm += (n * ans);
  }
  printf("%lld\\n", sm);
  return 0;
}