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

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define fi first
#define se second
#define inf (INT_MAX / 2 - 1)
#define infl (1LL << 60)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) begin(a), end(a)
#define y0 y5656
#define y1 y7878
#define aaa system("pause");
#define dbg(x) cerr << (#x) << ": " << (x) << '\\n', aaa
#define dbga(x, n)                                                             \\
  cerr << (#x) << "[]: ";                                                      \\
  for (int _ = 0; _ < n; _++)                                                  \\
    cerr << x[_] << ' ';                                                       \\
  cerr << '\\n', aaa
#define dbgs(x)                                                                \\
  cerr << (#x) << "[stl]: ";                                                   \\
  for (auto _ : x)                                                             \\
    cerr << _ << ' ';                                                          \\
  cerr << '\\n', aaa
#define dbgp(x) cerr << (#x) << ": " << x.fi << ' ' << x.se << '\\n', aaa
#define maxn 10000000

using namespace std;

int n;
bool viz[maxn + 5];
vi prime;

void ciur() {
  int i, j;
  viz[0] = viz[1] = 1;
  for (i = 2; i * i <= n; i++)
    if (!viz[i])
      for (j = i * i; j <= n; j += i)
        viz[j] = 1;
  for (i = 0; i <= n; i++) {
    if (!viz[i])
      prime.pb(i);
    viz[i] = 0;
  }
}

int put[664579 + 5];
ll ans;

void df(int x, ll now) {
  ans += 1LL * x * now;
  viz[x] = 1;
  for (int i = 0; i < sz(prime); i++) {
    if (1LL * x * prime[i] > n)
      break;
    if (!viz[x * prime[i]]) {
      ll nn = now / (put[i] + 1) * (put[i] + 2);
      put[i]++;
      df(x * prime[i], nn);
      put[i]--;
    }
  }
}

int main() {
  cin >> n;
  ciur();
  df(1, 1);
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define fi first
#define se second
#define inf (INT_MAX / 2 - 1)
#define infl (1LL << 60)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) begin(a), end(a)
#define y0 y5656
#define y1 y7878
#define aaa system("pause");
#define dbg(x) cerr << (#x) << ": " << (x) << '\\n', aaa
#define dbga(x, n)                                                             \\
  cerr << (#x) << "[]: ";                                                      \\
  for (int _ = 0; _ < n; _++)                                                  \\
    cerr << x[_] << ' ';                                                       \\
  cerr << '\\n', aaa
#define dbgs(x)                                                                \\
  cerr << (#x) << "[stl]: ";                                                   \\
  for (auto _ : x)                                                             \\
    cerr << _ << ' ';                                                          \\
  cerr << '\\n', aaa
#define dbgp(x) cerr << (#x) << ": " << x.fi << ' ' << x.se << '\\n', aaa
#define maxn 10000000

using namespace std;

int n;
bool viz[maxn + 5];
vi prime;

void ciur() {
  int i, j;
  viz[0] = viz[1] = 1;
  for (i = 2; i * i <= n; i++)
    if (!viz[i])
      for (j = i * i; j <= n; j += i)
        viz[j] = 1;
  for (i = 0; i <= n; i++) {
    if (!viz[i])
      prime.pb(i);
    viz[i] = 0;
  }
}

int put[664579 + 5];
ll ans;

void df(int x, ll now) {
  ans += 1LL * x * now;
  viz[x] = 1;
  for (int i = 0; i < sz(prime); i++)
    if (1LL * x * prime[i] <= n && !viz[x * prime[i]]) {
      ll nn = now / (put[i] + 1) * (put[i] + 2);
      put[i]++;
      df(x * prime[i], nn);
      put[i]--;
    }
}

int main() {
  cin >> n;
  ciur();
  df(1, 1);
  cout << ans;
  return 0;
}