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

// Name: Kanisht Agarwal Copyright Think before u copy //
// Connect with me at LinkedIn

#include <bits/stdc++.h>
#define mod 1000000007
#define ull unsigned ll
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define pi acos(-1)
#define nui 100
#define INF 100000000000

#define F(i, a, n) for (ll i = a; i < n; i++)
#define B(i, a, n) for (ll i = n - 1; i >= a; i--)
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define tcf                                                                    \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  for (int w = 1; w <= t; w++)

using namespace std;

ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res *= a;
    }
    a *= a;
    b >>= 1;
  }
  return res;
}

ll fermat_inv(ll y) { return binpow(y, mod - 2); }

ll cal(int n) {
  ll ct = 0;
  for (int i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if ((n / i) != i) {
        ct += 2;
      } else {
        ct++;
      }
    }
  }
  return ct;
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // Problem A:

  // Complexity : O(nlog n) Sieve

  int n;
  cin >> n;
  vector<ll> v(n + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n; j += i) {
      v[j]++;
    }
  }
  ll ans = 0;
  F(i, 1, n + 1) { ans += i * v[i]; }
  cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

// Name: Kanisht Agarwal Copyright Think before u copy //
// Connect with me at LinkedIn

#include <bits/stdc++.h>
#define mod 1000000007
#define ull unsigned ll
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define pi acos(-1)
#define nui 100
#define INF 100000000000

#define F(i, a, n) for (ll i = a; i < n; i++)
#define B(i, a, n) for (ll i = n - 1; i >= a; i--)
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define tcf                                                                    \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  for (int w = 1; w <= t; w++)

using namespace std;

ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res *= a;
    }
    a *= a;
    b >>= 1;
  }
  return res;
}

ll fermat_inv(ll y) { return binpow(y, mod - 2); }

ll cal(int n) {
  ll ct = 0;
  for (int i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if ((n / i) != i) {
        ct += 2;
      } else {
        ct++;
      }
    }
  }
  return ct;
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // Problem A:

  int n;
  cin >> n;
  ll res = 0;
  F(i, 1, n + 1) { res += cal(i) * i; }
  cout << res << "\\n";
  return 0;
}