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;
#define endl '\\n'
#define maxl __LONG_LONG_MAX__
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define rev1(i, n) for (int i = n; i > 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define trace(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \\
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \\
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \\
       << z << endl;
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl;
#define cout1(a) cout << a << endl
#define cout2(a, b) cout << a << ' ' << b << endl
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define coutvp(v, i) cout << v[i].fi << " " << v[i].se << endl
#define couta(a, n)                                                            \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << a[i] << " ";                                                       \\
  }
#define n_ones(x) __builtin_popcountll(x)
#define n_trailzero(x) __builtin_ctzll(x)
#define n_leadingzero(x) __builtin_clz(x)
#define sz(a) (ll)(a.size())
#define all(v) v.begin(), v.end()
#define FASTIO                                                                 \\
  { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a / gcd(a, b)) * b
#define PI 3.14159265358979323846
#define see(args...)                                                           \\
  {                                                                            \\
    cerr << "LINE " << __LINE__;                                               \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cerr << endl;                                                              \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << ' ' << *it << " = " << a;
  err(++it, args...);
}
template <typename T, typename U> inline void min_self(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> inline void max_self(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> pair) {
  return out << "(" << pair.first << ", " << pair.second << ")";
}
template <class T> ostream &operator<<(ostream &out, vector<T> vec) {
  out << "(";
  for (auto &v : vec)
    out << v << ", ";
  return out << ")";
}
template <class T> ostream &operator<<(ostream &out, set<T> vec) {
  out << "(";
  for (auto &v : vec)
    out << v << ", ";
  return out << ")";
}
template <class L, class R> ostream &operator<<(ostream &out, map<L, R> vec) {
  out << "(";
  for (auto &v : vec)
    out << "[" << v.first << ", " << v.second << "]";
  return out << ")";
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  return in >> a.first >> a.second;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &i : a)
    in >> i;
  return in;
}

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vector<ll>> vvll;
typedef stack<ll> stk;
typedef queue<ll> que;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_setll;
typedef tree<char, null_type, less<char>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_setc;

ll max(int a, ll b) { return max((ll)a, b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
ll max(ll a, int b) { return max(a, (ll)b); }

// cout<<fixed<<setprecision(20)<<pi<<endl;

ll mod = 1e9 + 7;
ll Test, cas;

#include <bits/stdc++.h>
using namespace std;

void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  vll div(n + 1, 2);
  div[1] = 1;
  for (ll i = 2; i * i <= n; i++) {
    for (ll j = i * i; j <= n; j += i) {
      div[j]++;
      if (j / i > i)
        div[j]++;
    }
  }
  rep1(i, n + 1) { ans += i * div[i]; }
  cout1(ans);
  return;
}

int main() {
  FASTIO;
  Test = 1;
  // cin>>Test;
  for (cas = 1; cas <= Test; cas++)
    solve();
  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;
#define endl '\\n'
#define maxl __LONG_LONG_MAX__
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define rev1(i, n) for (int i = n; i > 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define trace(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \\
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \\
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \\
       << z << endl;
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl;
#define cout1(a) cout << a << endl
#define cout2(a, b) cout << a << ' ' << b << endl
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define coutvp(v, i) cout << v[i].fi << " " << v[i].se << endl
#define couta(a, n)                                                            \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << a[i] << " ";                                                       \\
  }
#define n_ones(x) __builtin_popcountll(x)
#define n_trailzero(x) __builtin_ctzll(x)
#define n_leadingzero(x) __builtin_clz(x)
#define sz(a) (ll)(a.size())
#define all(v) v.begin(), v.end()
#define FASTIO                                                                 \\
  { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a / gcd(a, b)) * b
#define PI 3.14159265358979323846
#define see(args...)                                                           \\
  {                                                                            \\
    cerr << "LINE " << __LINE__;                                               \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cerr << endl;                                                              \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << ' ' << *it << " = " << a;
  err(++it, args...);
}
template <typename T, typename U> inline void min_self(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> inline void max_self(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> pair) {
  return out << "(" << pair.first << ", " << pair.second << ")";
}
template <class T> ostream &operator<<(ostream &out, vector<T> vec) {
  out << "(";
  for (auto &v : vec)
    out << v << ", ";
  return out << ")";
}
template <class T> ostream &operator<<(ostream &out, set<T> vec) {
  out << "(";
  for (auto &v : vec)
    out << v << ", ";
  return out << ")";
}
template <class L, class R> ostream &operator<<(ostream &out, map<L, R> vec) {
  out << "(";
  for (auto &v : vec)
    out << "[" << v.first << ", " << v.second << "]";
  return out << ")";
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  return in >> a.first >> a.second;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &i : a)
    in >> i;
  return in;
}

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vector<ll>> vvll;
typedef stack<ll> stk;
typedef queue<ll> que;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_setll;
typedef tree<char, null_type, less<char>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_setc;

ll max(int a, ll b) { return max((ll)a, b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
ll max(ll a, int b) { return max(a, (ll)b); }

// cout<<fixed<<setprecision(20)<<pi<<endl;

ll mod = 1e9 + 7;
ll Test, cas;

#include <bits/stdc++.h>
using namespace std;

void SieveOfEratosthenes(int n, bool prime[], bool primesquare[], int a[]) {
  // Create a boolean array "prime[0..n]" and
  // initialize all entries it as true. A value
  // in prime[i] will finally be false if i is
  // Not a prime, else true.
  for (int i = 2; i <= n; i++)
    prime[i] = true;

  // Create a boolean array "primesquare[0..n*n+1]"
  // and initialize all entries it as false. A value
  // in squareprime[i] will finally be true if i is
  // square of prime, else false.
  for (int i = 0; i <= (n * n + 1); i++)
    primesquare[i] = false;

  // 1 is not a prime number
  prime[1] = false;

  for (int p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then
    // it is a prime
    if (prime[p] == true) {
      // Update all multiples of p
      for (int i = p * 2; i <= n; i += p)
        prime[i] = false;
    }
  }

  int j = 0;
  for (int p = 2; p <= n; p++) {
    if (prime[p]) {
      // Storing primes in an array
      a[j] = p;

      // Update value in primesquare[p*p],
      // if p is prime.
      primesquare[p * p] = true;
      j++;
    }
  }
}

// Function to count divisors
int countDivisors(int n) {
  // If number is 1, then it will have only 1
  // as a factor. So, total factors will be 1.
  if (n == 1)
    return 1;

  bool prime[n + 1], primesquare[n * n + 1];

  int a[n]; // for storing primes upto n

  // Calling SieveOfEratosthenes to store prime
  // factors of n and to store square of prime
  // factors of n
  SieveOfEratosthenes(n, prime, primesquare, a);

  // ans will contain total number of distinct
  // divisors
  int ans = 1;

  // Loop for counting factors of n
  for (int i = 0;; i++) {
    // a[i] is not less than cube root n
    if (a[i] * a[i] * a[i] > n)
      break;

    // Calculating power of a[i] in n.
    int cnt = 1;          // cnt is power of prime a[i] in n.
    while (n % a[i] == 0) // if a[i] is a factor of n
    {
      n = n / a[i];
      cnt = cnt + 1; // incrementing power
    }

    // Calculating number of divisors
    // If n = a^p * b^q then total divisors of n
    // are (p+1)*(q+1)
    ans = ans * cnt;
  }

  // if a[i] is greater than cube root of n

  // First case
  if (prime[n])
    ans = ans * 2;

  // Second case
  else if (primesquare[n])
    ans = ans * 3;

  // Third casse
  else if (n != 1)
    ans = ans * 4;

  return ans; // Total divisors
}

void solve() {
  ll n;
  cin >> n;
  ll cnt = 0;
  for (int i = 1; i <= n; i++) {
    ll c = 0;
    for (int j = 1; j <= sqrt(i); j++) {
      if (i % j == 0) {
        if (i / j == j) {
          c++;
        } else {
          c += 2;
        }
      }
    }
    cnt += i * c;
  }
  cout1(cnt);
  return;
}

int main() {
  FASTIO;
  Test = 1;
  // cin>>Test;
  for (cas = 1; cas <= Test; cas++)
    solve();
  return 0;
}