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

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

typedef long long int ll;
typedef vector<int> vi;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

#define pvec(v)                                                                \\
  for (auto e : v)                                                             \\
    cout << e << " ";
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define INF 1000000005
#define MOD 1000000007
#define all(v) v.begin(), v.end()

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

ll power(ll x, ll y, ll p = MOD) {
  ll res = 1LL;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1LL)
      res = (res * x) % p;
    y = y >> 1LL;
    x = (x * x) % p;
  }
  return res;
}

int main() {
  IO int N;
  cin >> N;

  // {
  // 	int cnt = 1;
  // 	for(int C = 1; C < N - 1; ++C) {
  // 		int factors = 2;
  // 		int X = N - C;
  // 		for(int i = 2; i*i <= X; ++i) {
  // 			if(X%i == 0 ) {
  // 				if(i*i != X) factors+=2;
  // 				else factors+=1;
  // 			}
  // 		}
  // 		// cout << X << " " << factors << " " << endl;
  // 		cnt+=factors;
  // 	}

  // 	cout << cnt << endl;
  // }

  int cnt = 0;
  for (int A = 1; A <= 1000000; ++A) {
    for (int B = 1; A * B <= 1000000; ++B) {
      int C = N - A * B;
      if (C > 0)
        ++cnt;
    }
  }
  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long int ll;
typedef vector<int> vi;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

#define pvec(v)                                                                \\
  for (auto e : v)                                                             \\
    cout << e << " ";
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define INF 1000000005
#define MOD 1000000007
#define all(v) v.begin(), v.end()

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

ll power(ll x, ll y, ll p = MOD) {
  ll res = 1LL;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1LL)
      res = (res * x) % p;
    y = y >> 1LL;
    x = (x * x) % p;
  }
  return res;
}

int main() {
  IO int N;
  cin >> N;

  int cnt = 1;
  for (int C = 1; C < N - 1; ++C) {
    int factors = 2;
    int X = N - C;
    for (int i = 2; i * i <= X; ++i) {
      if (X % i == 0) {
        if (i * i != X)
          factors += 2;
        else
          factors += 1;
      }
    }
    // cout << X << " " << factors << " " << endl;
    cnt += factors;
  }

  cout << cnt << endl;
  return 0;
}