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

/*
                                        //     //     //\\\\\\\\\\\\\\\\\\
                                        \\\\    //      \\\\        \\\\
                                        //   //       //         \\\\
                                        \\\\  //        \\\\          \\\\
                                        // //         //           \\\\
                                        \\\\//          \\\\            \\\\
                                        //\\\\          //            //
                                        \\\\ \\\\         \\\\           //
                                        //  \\\\        //          //
                                        \\\\   \\\\       \\\\         //
                                        //    \\\\      //        //
                                        \\\\     \\\\     \\\\/////////
*/

#include <bits/stdc++.h>
// #include <boost/math/common_factor.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
// using namespace boost::multiprecision;
#define ff first
#define ss second
#define endl cout << '\\n'
#define fre(i, l, n) for (long long int i = l; i < n; ++i)
#define fr(i, l, n) for (long long int i = l; i <= n; i++)
#define rfre(i, n, l) for (long long int i = n - 1; i >= l; i--)
#define rfr(i, n, l) for (long long int i = n; i >= l; --i)
#define tab '\\t'
#define debug cout << '*';
#define s(a) cin >> a
#define p(a) cout << a
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<long long int, long long int> pll;
typedef pair<int, pair<int, int>> ppii;
typedef vector<int> vec;
typedef vector<long long> vecll;
typedef vector<pair<int, int>> vec_pii;
typedef vector<pair<long long int, long long int>> vec_pll;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<vector<pair<int, int>>> vvpii;
typedef vector<vector<pair<ll, ll>>> vvpll;
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define mod 1000000007
#define Pi 3.14159265358979
#define gcd(a, b) __gcd(a, b)
#define sf(n) scanf("%lld", &(n))
#define pf(n) printf("%lld\\n", (n))
#define min3(a, b, c) (min((c), min((a), (b))))
#define max3(a, b, c) (max((a), max((b), (c))))
#define w(t) while (t--)
#define flt(a) cout << fixed << setprecision(a)
#define cntSetBits(n) __builtin_popcountll(n)
/*****************************
*****************************/

// const ll MAXN = 2000000;
// ll fact[MAXN+5];
// ll invfact[MAXN+5];

// ll power(ll a, ll b)
// {
//     ll res = 1;
//     while(b)
//     {
//         if(b & 1)
//         {
//             res = (res%mod * a%mod)%mod;
//         }
//         a = (a%mod * a%mod)%mod;
//         b >>= 1;
//     }
//     return res;
// }

// void facto()
// {
//     fact[0] = 1;
//     fr(i,1,MAXN)
//     {
//         fact[i] = (fact[i-1]%mod * i%mod)%mod;
//     }
//     invfact[MAXN] = power(fact[MAXN],mod-2);
//     rfre(i,MAXN,0)
//     {
//         invfact[i] = (invfact[i+1]%mod * (i+1)%mod)%mod;
//     }
// }

// ll nCr(ll n,ll r)
// {
//     return (fact[n]%mod * invfact[r]%mod * invfact[n-r]%mod)%mod;
// }

// void intialise()
// {
//     memset(vis,0,sizeof(vis));
//     fre(i,0,MAXN)
//     {
//         adj[i].clear();
//     }
// }

void solve() {
  // intialise();
  ll num;
  s(num);
  ll ans = 0;
  fr(i, 1, num) {
    ll kd = num / i;
    ll pm = 2 * i + (kd - 1) * i;
    ans += (kd * pm / 2);
  }
  p(ans);
  endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // facto();
  ll t = 1;
  // s(t);
  w(t) { solve(); }
  return 0;
}


Error: TLE

Fixed submission:

/*
                                        //     //     //\\\\\\\\\\\\\\\\\\
                                        \\\\    //      \\\\        \\\\
                                        //   //       //         \\\\
                                        \\\\  //        \\\\          \\\\
                                        // //         //           \\\\
                                        \\\\//          \\\\            \\\\
                                        //\\\\          //            //
                                        \\\\ \\\\         \\\\           //
                                        //  \\\\        //          //
                                        \\\\   \\\\       \\\\         //
                                        //    \\\\      //        //
                                        \\\\     \\\\     \\\\/////////
*/

#include <bits/stdc++.h>
// #include <boost/math/common_factor.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
// using namespace boost::multiprecision;
#define ff first
#define ss second
#define endl cout << '\\n'
#define fre(i, l, n) for (long long int i = l; i < n; ++i)
#define fr(i, l, n) for (long long int i = l; i <= n; i++)
#define rfre(i, n, l) for (long long int i = n - 1; i >= l; i--)
#define rfr(i, n, l) for (long long int i = n; i >= l; --i)
#define tab '\\t'
#define debug cout << '*';
#define s(a) cin >> a
#define p(a) cout << a
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef pair<long long int, long long int> pll;
typedef pair<int, pair<int, int>> ppii;
typedef vector<int> vec;
typedef vector<long long> vecll;
typedef vector<pair<int, int>> vec_pii;
typedef vector<pair<long long int, long long int>> vec_pll;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<vector<pair<int, int>>> vvpii;
typedef vector<vector<pair<ll, ll>>> vvpll;
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define mod 1000000007
#define Pi 3.14159265358979
#define gcd(a, b) __gcd(a, b)
#define sf(n) scanf("%lld", &(n))
#define pf(n) printf("%lld\\n", (n))
#define min3(a, b, c) (min((c), min((a), (b))))
#define max3(a, b, c) (max((a), max((b), (c))))
#define w(t) while (t--)
#define flt(a) cout << fixed << setprecision(a)
#define cntSetBits(n) __builtin_popcountll(n)
/*****************************
*****************************/

// const ll MAXN = 2000000;
// ll fact[MAXN+5];
// ll invfact[MAXN+5];

// ll power(ll a, ll b)
// {
//     ll res = 1;
//     while(b)
//     {
//         if(b & 1)
//         {
//             res = (res%mod * a%mod)%mod;
//         }
//         a = (a%mod * a%mod)%mod;
//         b >>= 1;
//     }
//     return res;
// }

// void facto()
// {
//     fact[0] = 1;
//     fr(i,1,MAXN)
//     {
//         fact[i] = (fact[i-1]%mod * i%mod)%mod;
//     }
//     invfact[MAXN] = power(fact[MAXN],mod-2);
//     rfre(i,MAXN,0)
//     {
//         invfact[i] = (invfact[i+1]%mod * (i+1)%mod)%mod;
//     }
// }

// ll nCr(ll n,ll r)
// {
//     return (fact[n]%mod * invfact[r]%mod * invfact[n-r]%mod)%mod;
// }

// void intialise()
// {
//     memset(vis,0,sizeof(vis));
//     fre(i,0,MAXN)
//     {
//         adj[i].clear();
//     }
// }

const ll MAXN = 10000000;

ll prime[MAXN + 5];
ll kd[MAXN + 5];
ll pm[MAXN + 5];
vecll primme;

void sieve() {
  memset(prime, true, sizeof(prime));
  memset(pm, false, sizeof(pm));
  prime[0] = 0;
  prime[1] = 0;
  for (ll i = 2; i * i <= 10000000; i++) {
    if (prime[i]) {
      primme.pb(i);
      for (ll j = i * i; j <= 10000000; j += i) {
        prime[j] = 0;
      }
    }
  }
  for (ll j = 2; j * j <= 10000000; j++) {
    if (prime[j]) {
      pm[j * j] = true;
    }
  }
}

ll factors(ll num) {
  ll ans = 1;
  for (ll i = 0; i < (ll)primme.size(); i++) {
    if (primme[i] * primme[i] * primme[i] > num) {
      break;
    }
    ll cnt = 1;
    while (num % primme[i] == 0) {
      num /= primme[i];
      cnt += 1;
    }
    ans = ans * cnt;
  }
  if (prime[num]) {
    ans *= 2;
  } else if (pm[num]) {
    ans *= 3;
  } else if (num != 1) {
    ans *= 4;
  }
  return ans;
}

void calculate() {
  kd[1] = 1;
  fr(i, 2, 10000000) {
    ll val;
    if (prime[i]) {
      val = 2;
    } else if (pm[i]) {
      val = 3;
    } else {
      val = factors(i);
    }
    kd[i] = kd[i - 1] + val * i;
  }
}

void solve() {
  // intialise();
  ll num;
  s(num);
  p(kd[num]);
  endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // facto();
  sieve();
  calculate();
  ll t = 1;
  // s(t);
  w(t) { solve(); }
  return 0;
}