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

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;

#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, k, n) for (int i = k; i <= n; ++i)
#define REPR(i, k, n) for (int i = k; i >= n; --i)
#define pb push_back
#define watch(x) cout << (#x) << " is " << (x) << endl
#define sz(a) int((a).size())
#define endl '\\n' // remove for interactive

const int INF = 1e9;
const ll mod = 1e9 + 7;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

const int N = 1e6 + 10;
int spf[N];
int d[N];

void solve() {

  for (int i = 2; i <= 1e6; i++)
    if (!spf[i])
      for (int j = i; j <= 1e6; j += i)
        if (!spf[j])
          spf[j] = i;

  REP(i, 1, 1e6) {
    int mult = 1;
    int tr = i;
    while (tr != 1) {
      int sel = spf[tr];
      int cnt = 0;
      while (tr % sel == 0) {
        cnt++;
        tr /= sel;
      }
      mult = 1LL * mult * (cnt + 1);
    }
    d[i] = mult;
  }
  // watch(d[4]);
  int n;
  cin >> n;
  ll ans = 0;
  // watch(d[4]);
  REP(i, 1, n - 1) {
    int tr = n - i;
    ans += d[tr];
  }
  cout << ans;
}

int main() {
  // clock_t start, end;
  // start = clock();
  ios_base::sync_with_stdio(0); // cant use scanf, printf
  cin.tie(0);
  cout.tie(
      0); // no longer auto flush cout before each cin, remove for interactive
  int t = 1;
  // cin>>t;
  REP(i, 1, t) { solve(); }
  // end = clock();
  // double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
  // watch(time_taken);
  ll x = INF;
  x++;
  return 0;
}

/*before sub
TEST: 0(1 by 1 too), 1, equal, -ve, bounds(random and ez)
ERR/WA: loop vars, out of bounds, endl, finish taking inputs, large doubles,
larger INF, corr mod, t, early/late mod RE: double dec, integer overflow(ll or
1LL mult)
*/


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;

#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, k, n) for (int i = k; i <= n; ++i)
#define REPR(i, k, n) for (int i = k; i >= n; --i)
#define pb push_back
#define watch(x) cout << (#x) << " is " << (x) << endl
#define sz(a) int((a).size())
#define endl '\\n' // remove for interactive

const int INF = 1e9;
const ll mod = 1e9 + 7;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

void solve() {
  int n;
  cin >> n;
  ll ans = 0;
  REP(i, 1, n - 1) {
    int tr = n - i;
    for (int j = 1; j * j <= tr; j++) {
      if (tr % j == 0) {
        if (tr / j == j)
          ans++;
        else
          ans += 2;
      }
    }
  }
  cout << ans;
}

int main() {
  // clock_t start, end;
  // start = clock();
  ios_base::sync_with_stdio(0); // cant use scanf, printf
  cin.tie(0);
  cout.tie(
      0); // no longer auto flush cout before each cin, remove for interactive
  int t = 1;
  // cin>>t;
  REP(i, 1, t) { solve(); }
  // end = clock();
  // double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
  // watch(time_taken);
  ll x = INF;
  x++;
  return 0;
}

/*before sub
TEST: 0(1 by 1 too), 1, equal, -ve, bounds(random and ez)
ERR/WA: loop vars, out of bounds, endl, finish taking inputs, large doubles,
larger INF, corr mod, t, early/late mod RE: double dec, integer overflow(ll or
1LL mult)
*/