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


// Problem : D - Sum of Divisors
// Contest : AtCoder - AtCoder Beginner Contest 172
// URL : https://atcoder.jp/contests/abc172/tasks/abc172_d
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define debug(x) cerr << #x << " = " << x << '\\n'
#define debug_arr(a, n)                                                        \\
  for (int i = 0; i < n; i++)                                                  \\
  cerr << a[i] << " "
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define vll vector<ll>
#define all(x) (x).begin(), (x).end()
#define inf 1000000000
#define mod 1000000007

const int max_n = 1e7 + 9;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
ll power(ll a, ll b) {
  ll prod = 1;
  while (b) {
    if (b & 1)
      prod = (prod * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return prod;
}
int n;
ll dp[max_n];
ll smallest_prime[max_n];
int main() {
  speed;
  cin >> n;

  for (ll i = 2; i <= n; i++) {
    smallest_prime[i] = i;
  }

  for (ll i = 2; i <= n; i++) {
    if (smallest_prime[i] == i) {
      for (ll j = i * i; j <= n; j += i) {
        smallest_prime[j] = i;
      }
    }
  }

  dp[1] = 1;
  for (ll i = 2; i <= n; i++) {
    ll cnt = 0, temp = i;
    while (temp % smallest_prime[i] == 0) {
      cnt++;
      temp /= smallest_prime[i];
    }

    dp[i] = dp[temp] * (cnt + 1);
  }

  ll sum = 0;
  for (ll i = 1; i <= n; i++) {
    sum += i * dp[i];
  }

  cout << sum << "\\n";
  return 0;
}

Error: TLE

Fixed submission:


// Problem : D - Sum of Divisors
// Contest : AtCoder - AtCoder Beginner Contest 172
// URL : https://atcoder.jp/contests/abc172/tasks/abc172_d
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define debug(x) cerr << #x << " = " << x << '\\n'
#define debug_arr(a, n)                                                        \\
  for (int i = 0; i < n; i++)                                                  \\
  cerr << a[i] << " "
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define vll vector<ll>
#define all(x) (x).begin(), (x).end()
#define inf 1000000000
#define mod 1000000007

const int max_n = 1e7 + 9;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
ll power(ll a, ll b) {
  ll prod = 1;
  while (b) {
    if (b & 1)
      prod = (prod * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return prod;
}
int n;
ll dp[max_n];
int main() {
  cin >> n;

  dp[1] = 1;
  for (ll i = 2; i <= n; i++) {
    ll temp = i, flag = 0;
    for (ll j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        ll cnt = 0;
        while (temp % j == 0) {
          temp /= j, cnt++;
        }
        flag = 1;
        dp[i] = dp[temp] * (cnt + 1);
        break;
      }
    }
    if (!flag) {
      dp[i] = 2;
    }
  }

  ll sum = 0;
  for (ll i = 1; i <= n; i++) {
    sum += i * dp[i];
  }

  cout << sum << "\\n";
  return 0;
}