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

#include <iostream>
#include <vector>
void stupid() {
  unsigned int N;
  std::vector<unsigned int> numDivisor;
  unsigned long long sum;
  std::cin >> N;
  numDivisor = std::vector<unsigned int>(N, 1);
  for (int i = 1; i < N; ++i) {
    for (int j = i; j < N; ++j) {
      if ((j + 1) % (i + 1) == 0) {
        numDivisor[j]++;
      }
    }
  }
  sum = 0;
  for (int i = 0; i < N; ++i) {
    sum += (i + 1) * numDivisor[i];
  }
  std::cout << sum;
}

void stupid2() {
  unsigned int N;
  std::vector<unsigned int> numDivisor;
  std::vector<unsigned int> squares;
  unsigned long long sum;
  std::cin >> N;
  int indexSquares;
  int numSquares;
  numDivisor = std::vector<unsigned int>(N, 0);
  numDivisor[0] = 1;
  for (int i = 1; (i - 1) * (i - 1) <= N; ++i) {
    squares.push_back(i * i);
  }
  numSquares = squares.size();
  indexSquares = 0;

  for (int i = 1; i < N; ++i) {
    if (indexSquares < squares.size() && squares[indexSquares + 1] == (i + 1)) {
      indexSquares++;
    }
    for (int j = 0; j <= indexSquares; ++j) {
      if ((i + 1) % (j + 1) == 0) {
        // std::cout<<i+1<<":"<<j+1<<std::endl;
        ++numDivisor[i];
      }
    }
    numDivisor[i] *= 2;
    if (indexSquares < squares.size() && squares[indexSquares] == (i + 1)) {
      numDivisor[i]--;
    }
  }

  // for(int i=0;i<N;++i){
  //     std::cout<<i+1<<":"<<numDivisor[i]<<std::endl;
  // }

  sum = 0;
  for (int i = 0; i < N; ++i) {
    sum += (i + 1) * numDivisor[i];
  }
  std::cout << sum;
}

void correct() {
  unsigned int N;
  unsigned long long sum;
  std::cin >> N;
  sum = 0;
  for (int i = 1; i <= N; ++i) {
    unsigned long long div = (int)(N / i);
    sum += i * (div) * (div + 1) / 2;
  }
  std::cout << sum;
}

int main() {
  // stupid();
  // stupid2();
  correct();
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
void stupid() {
  unsigned int N;
  std::vector<unsigned int> numDivisor;
  unsigned long long sum;
  std::cin >> N;
  numDivisor = std::vector<unsigned int>(N, 1);
  for (int i = 1; i < N; ++i) {
    for (int j = i; j < N; ++j) {
      if ((j + 1) % (i + 1) == 0) {
        numDivisor[j]++;
      }
    }
  }
  sum = 0;
  for (int i = 0; i < N; ++i) {
    sum += (i + 1) * numDivisor[i];
  }
  std::cout << sum;
}

void stupid2() {
  unsigned int N;
  std::vector<unsigned int> numDivisor;
  std::vector<unsigned int> squares;
  unsigned long long sum;
  std::cin >> N;
  int indexSquares;
  int numSquares;
  numDivisor = std::vector<unsigned int>(N, 0);
  numDivisor[0] = 1;
  for (int i = 1; (i - 1) * (i - 1) <= N; ++i) {
    squares.push_back(i * i);
  }
  numSquares = squares.size();
  indexSquares = 0;

  for (int i = 1; i < N; ++i) {
    if (indexSquares < squares.size() && squares[indexSquares + 1] == (i + 1)) {
      indexSquares++;
    }
    for (int j = 0; j <= indexSquares; ++j) {
      if ((i + 1) % (j + 1) == 0) {
        // std::cout<<i+1<<":"<<j+1<<std::endl;
        ++numDivisor[i];
      }
    }
    numDivisor[i] *= 2;
    if (indexSquares < squares.size() && squares[indexSquares] == (i + 1)) {
      numDivisor[i]--;
    }
  }

  // for(int i=0;i<N;++i){
  //     std::cout<<i+1<<":"<<numDivisor[i]<<std::endl;
  // }

  sum = 0;
  for (int i = 0; i < N; ++i) {
    sum += (i + 1) * numDivisor[i];
  }
  std::cout << sum;
}

int main() {
  // stupid();
  stupid2();
  return 0;
}