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


/** Which of the favors of your Lord will you deny ? **/

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

#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define MP make_pair
#define F first
#define S second

#define ALL(x) (x).begin(), (x).end()
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
#define READ freopen("alu.txt", "r", stdin)
#define WRITE freopen("vorta.txt", "w", stdout)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <class TIn>
using indexed_set = tree<TIn, null_type, less<TIn>, rb_tree_tag,
                         tree_order_statistics_node_update>;

/**

PBDS
-------------------------------------------------
1) insert(value)
2) erase(value)
3) order_of_key(value) // 0 based indexing
4) *find_by_order(position) // 0 based indexing

**/

template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);

inline void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

const int nmax = 1e7 + 7;
const LL LINF = 1e17;

template <class T> string to_str(T x) {
  stringstream ss;
  ss << x;
  return ss.str();
}

// bool cmp(const PII &A,const PII &B)
//{
//
// }

LL d[nmax];
LL ans[nmax];

void sieve() {
  LL N = 1e7;

  for (LL i = 1; i <= N; i++)
    for (LL j = i; j <= N; j += i)
      d[j]++;

  for (LL i = 1; i <= N; i++)
    ans[i] = i * d[i], ans[i] += ans[i - 1];
}

int main() {
  optimizeIO();

  sieve();

  LL n;
  cin >> n;

  cout << ans[n] << endl;

  return 0;
}

/**

**/

template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}

template <class T> ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}


Error: TLE

Fixed submission:


/** Which of the favors of your Lord will you deny ? **/

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

#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define MP make_pair
#define F first
#define S second

#define ALL(x) (x).begin(), (x).end()
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
#define READ freopen("alu.txt", "r", stdin)
#define WRITE freopen("vorta.txt", "w", stdout)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <class TIn>
using indexed_set = tree<TIn, null_type, less<TIn>, rb_tree_tag,
                         tree_order_statistics_node_update>;

/**

PBDS
-------------------------------------------------
1) insert(value)
2) erase(value)
3) order_of_key(value) // 0 based indexing
4) *find_by_order(position) // 0 based indexing

**/

template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);

inline void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

const int nmax = 2e5 + 7;
const LL LINF = 1e17;

template <class T> string to_str(T x) {
  stringstream ss;
  ss << x;
  return ss.str();
}

// bool cmp(const PII &A,const PII &B)
//{
//
// }

/** Bit Sieve **/

const int pnmax = 1e8 + 7;
LL LIM;
bitset<pnmax> bs; /// can sieve upto 1e8 in ~ 1 sec
vector<LL> primes;

void bit_sieve(LL upperbound) {
  LIM = upperbound + 1;
  bs.set(); /// set all bits to 1
  bs[0] = bs[1] = 0;
  for (LL i = 2; i * i <= LIM; i++) /** If I don't want to know the primes , it
                                       is enough to loop upto sqrt(LIM) here **/
    if (bs[i]) {
      for (LL j = i * i; j <= LIM; j += i)
        bs[j] = 0;
      primes.push_back(i);
    }
}

/// NOD
LL number_of_Divisors(LL N) {
  LL PF_idx = 0, PF = primes[PF_idx], ans = 1; /// start from ans = 1
  while (N != 1 && (PF * PF <= N)) {
    LL power = 0; /// count the power
    while (N % PF == 0) {
      N /= PF;
      power++;
    }
    ans *= (power + 1); /// according to the formula
    PF = primes[++PF_idx];
  }
  if (N != 1)
    ans *= 2; /// (last factor has pow = 1, we add 1 to it)
  return ans;
}

int main() {
  optimizeIO();

  bit_sieve(1e7);

  LL n;
  cin >> n;

  LL ans = 0;

  for (int i = 1; i <= n; i++)
    ans += i * number_of_Divisors(i);

  cout << ans << endl;
  ;

  return 0;
}

/**

**/

template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}

template <class T> ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}