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



// BELIEVE IN YOURSELF
// next_permutation

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pob pop_back
#define vi vector<ll>
#define mp make_pair
#define sz size()
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define maxx 100005
#define se second
#define fi first
#define be begin()
#define en end()
#define sortv(v) sort(v.begin(), v.end());
#define sortr(v) sort(v.rbegin(), v.rend());
#define w()                                                                    \\
  ll _;                                                                        \\
  cin >> _;                                                                    \\
  while (_--)
#define edge pair<ll, ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define Endl "\\n"
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
#define lb(v, x) std::lower_bound(v.begin(), v.end(), x)
#define ub(v, x) std::upper_bound(v.begin(), v.end(), x)

using namespace std;

ll v[1000002];
ll prime[1000002];

void fact() {
  ll n = 1000001;
  ll i;
  v[0] = 0;
  v[1] = 0;
  ll j;
  prime[1] = 1;
  for (i = 1; i * i <= n; i++) {
    if (prime[i] == 0) {
      v[i] += 2;
    }

    for (j = i * i; j <= n; j += i) {
      prime[j] = 1;
      if (i * i == j) {
        v[j] += 1;
      } else {
        v[j] += 2;
      }
    }
  }
}

int main() {
  fast;

  ll n;
  cin >> n;
  fact();
  ll c = 0;
  ll ans = 0;
  // cout<<v[12]<<endl;

  for (c = n - 1; c >= 1; c--) {
    ans += (v[n - c]);
  }

  cout << ans << Endl;

  return 0;
}

Error: TLE

Fixed submission:



// BELIEVE IN YOURSELF
// next_permutation

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pob pop_back
#define vi vector<ll>
#define mp make_pair
#define sz size()
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define maxx 100005
#define se second
#define fi first
#define be begin()
#define en end()
#define sortv(v) sort(v.begin(), v.end());
#define sortr(v) sort(v.rbegin(), v.rend());
#define w()                                                                    \\
  ll _;                                                                        \\
  cin >> _;                                                                    \\
  while (_--)
#define edge pair<ll, ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define Endl "\\n"
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
#define lb(v, x) std::lower_bound(v.begin(), v.end(), x)
#define ub(v, x) std::upper_bound(v.begin(), v.end(), x)

using namespace std;

ll fact(ll n) {
  ll i;
  ll ans = 0;

  for (i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (n / i == i) {
        ans++;
      } else {
        ans += 2;
      }
    }
  }
  return ans;
}

int main() {
  fast;

  ll n;
  cin >> n;

  ll c = 0;
  ll ans = 0;

  for (c = n - 1; c >= 1; c--) {
    ans += fact(n - c);
  }

  cout << ans << Endl;

  return 0;
}