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)

/*

WINNERS NEVER QUIT AND QUITTERS NEVER WIN!!

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<long, long> pl;
typedef pair<ll, ll> pll;
typedef vector<long> vl;
typedef vector<bool> vb;
typedef vector<ll> vll;
typedef vector<vl> vvl;
typedef vector<vb> vvb;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef unordered_map<ll, ll> umll;

#define FOR(i, a, b) for (long long i = a; i < b; ++i)
#define REV(i, a, b) for (long long i = a; i >= b; i--)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define tc                                                                     \\
  ll tests;                                                                    \\
  cin >> tests;                                                                \\
  while (tests--)
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define coutv(v)                                                               \\
  for (auto it : (v))                                                          \\
    cout << it << " ";                                                         \\
  newl;
#define cout2d(v)                                                              \\
  for (auto it : (v)) {                                                        \\
    for (auto j : it)                                                          \\
      cout << j << " ";                                                        \\
    newl;                                                                      \\
  }
#define cinv(v, n)                                                             \\
  vll(v)(n);                                                                   \\
  FOR(i, 0, (n)) { cin >> v[i]; }
#define cinvg(v, n)                                                            \\
  (v).resize(n);                                                               \\
  FOR(i, 0, (n)) { cin >> v[i]; }
#define cin2d(v, n, m)                                                         \\
  vvll(v)(n, vll(m, 0));                                                       \\
  FOR(i, 0, n) {                                                               \\
    FOR(j, 0, m) { cin >> v[i][j]; }                                           \\
  }
#define cin2dg(v, n, m)                                                        \\
  (v).resize(n, vll(m));                                                       \\
  FOR(i, 0, n) {                                                               \\
    FOR(j, 0, m) { cin >> v[i][j]; }                                           \\
  }
#define newl cout << "\\n"
#define mod 1000000007
#define INF LLONG_MAX / 2

int main() {
  io;
  ll n, ans = 0;
  cin >> n;
  FOR(j, 1, n + 1) {
    ll y = n / j;
    ans += (y * (y + 1) * j) / 2;
  }
  cout << ans;
  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>

using namespace std;

typedef long long ll;
typedef pair<long, long> pl;
typedef pair<ll, ll> pll;
typedef vector<long> vl;
typedef vector<bool> vb;
typedef vector<ll> vll;
typedef vector<vl> vvl;
typedef vector<vb> vvb;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<string> vs;

#define FOR(i, a, b) for (long long i = a; i < b; ++i)
#define REV(i, a, b) for (long long i = a; i >= b; i--)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(), v.end()
#define tc                                                                     \\
  ll tests;                                                                    \\
  cin >> tests;                                                                \\
  while (tests--)
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define coutv(v)                                                               \\
  for (auto it : (v))                                                          \\
    cout << it << " ";                                                         \\
  newl;
#define cout2d(v)                                                              \\
  for (auto it : (v)) {                                                        \\
    for (auto j : it)                                                          \\
      cout << j << " ";                                                        \\
    newl;                                                                      \\
  }
#define cinv(v, n)                                                             \\
  vll(v)(n);                                                                   \\
  FOR(i, 0, (n)) { cin >> v[i]; }
#define cinvg(v, n)                                                            \\
  (v).resize(n);                                                               \\
  FOR(i, 0, (n)) { cin >> v[i]; }
#define cin2d(v, n, m)                                                         \\
  vvll(v)(n, vll(m, 0));                                                       \\
  FOR(i, 0, n) {                                                               \\
    FOR(j, 0, m) { cin >> v[i][j]; }                                           \\
  }
#define cin2dg(v, n, m)                                                        \\
  (v).resize(n, vll(m));                                                       \\
  FOR(i, 0, n) {                                                               \\
    FOR(j, 0, m) { cin >> v[i][j]; }                                           \\
  }
#define newl cout << "\\n"
#define mod 1000000007
#define INF 1e1

vll dp;

int main() {
  io;
  ll n, ans = 0;
  cin >> n;
  dp.resize(n + 1);
  FOR(i, 1, n + 1) {
    for (ll j = 1; j * j <= i; j++) {
      if (j * j == i) {
        dp[i]++;
        continue;
      }
      if (i % j == 0) {
        dp[i] += 2;
      }
    }
  }

  FOR(i, 1, n + 1) { ans += i * dp[i]; }
  cout << ans;
  return 0;
}