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


// Problem : C - A x B + C
// Contest : AtCoder - AtCoder Beginner Contest 179
// URL : https://atcoder.jp/contests/abc179/tasks/abc179_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("trapv")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-O2")
// #pragma GCC target("AVX")
#define endl '\\n'
#define rep() for (LL i = 0; i < n; i++)
#define f(i, s, n) for (LL i = s; i < n; i++)
#define fe(i, s, n) for (LL i = s; i <= n; i++)
#define rfe(i, s, n) for (LL i = s; i >= n; i--)
#define auto(m) for (auto &it : m)
#define f2(it, v) for (it = v.begin(); it != v.end(); ++it)
#define fori(it, x)                                                            \\
  for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define ip(n, a)                                                               \\
  LL n;                                                                        \\
  cin >> n;                                                                    \\
  LL a[n];                                                                     \\
  rep() { cin >> a[i]; }
#define op(n, a)                                                               \\
  f(i, 0, n) { cout << a[i] << " "; }                                          \\
  cout << endl;
#define i2p(n, k, a)                                                           \\
  LL n, k;                                                                     \\
  cin >> n >> k;                                                               \\
  LL a[n];                                                                     \\
  rep { cin >> a[i]; }
#define TCs(x)                                                                 \\
  LL x;                                                                        \\
  cin >> x;                                                                    \\
  while (x--)
#define TC(x)                                                                  \\
  LL x = 1;                                                                    \\
  while (x--)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define mem(a, x) memset(a, x, sizeof(a))
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define ps(x)                                                                  \\
  std::cout << std::fixed;                                                     \\
  std::cout << std::setprecision(x);
#define clz(a)                                                                 \\
  __builtin_clz(a) // count leading zeroes
                   // (geeksforgeeks.org/builtin-functions-gcc-compiler/)
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a)                                                                \\
  __builtin_popcount(a) // count set bits (for ints only diff for ll)
#define GCD(A, B) __gcd(A, B)
#define d1(x) cout << #x << ": " << x << endl
#define d2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define d3(x, y, z)                                                            \\
  cout << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define d4(a, b, c, d)                                                         \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define d5(a, b, c, d, e)                                                      \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define d6(a, b, c, d, e, f)                                                   \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \\
       << #f << ": " << f << endl
#define c1(a)                                                                  \\
  ;                                                                            \\
  LL a;                                                                        \\
  cin >> a;
#define c2(a, b)                                                               \\
  ;                                                                            \\
  LL a, b;                                                                     \\
  cin >> a >> b;
#define c3(a, b, c)                                                            \\
  ;                                                                            \\
  LL a, b, c;                                                                  \\
  cin >> a >> b >> c;
#define c4(a, b, c, d)                                                         \\
  ;                                                                            \\
  LL a, b, c, d;                                                               \\
  cin >> a >> b >> c >> d;
#define c5(a, b, c, d, e)                                                      \\
  ;                                                                            \\
  LL a, b, c, d, e;                                                            \\
  cin >> a >> b >> c >> d >> e;
#define c6(a, b, c, d, e, f)                                                   \\
  ;                                                                            \\
  LL a, b, c, d, e, f;                                                         \\
  cin >> a >> b >> c >> d >> e >> f;
#define c(a, n)                                                                \\
  ;                                                                            \\
  LL a[n];                                                                     \\
  f(i, 0, n) { cin >> a[i]; }
#define ce(a, n)                                                               \\
  ;                                                                            \\
  LL a[n + 1];                                                                 \\
  fe(i, 1, n) { cin >> a[i]; }
#define imin INT_MIN
#define imax INT_MAX

// Typedefs:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
typedef string S;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef vector<long long> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef vector<char> VC;
typedef pair<LL, LL> PLL;
typedef pair<string, LL> PSL;
typedef pair<LL, string> PLS;
typedef pair<char, LL> PCL;
typedef pair<LL, char> PLC;
typedef pair<string, string> PSS;
typedef pair<char, char> PCC;
typedef vector<PLL> VPLL;
typedef vector<PSL> VPSL;
typedef vector<PLS> VPLS;
typedef vector<PCL> VPCL;
typedef vector<PLC> VPLC;
typedef vector<PSS> VPSS;
typedef vector<PCC> VPCC;
typedef map<LL, LL> MLL;
typedef map<string, LL> MSL;
typedef map<char, LL> MCL;
typedef map<char, char> MCC;
typedef set<LL> SL;
typedef set<string> SS;
typedef set<char> SC;

int power(LL x, LL y) {
  if (y == 0)
    return 1;
  LL p = power(x, y / 2);
  p = (p * p);

  return (y % 2 == 0) ? p : (x * p);
}
const long long N = (long long)(1e6 + 1);
const long long MOD = (long long)(1e9 + 7);
const long long INF = (long long)(2e18);
const int MAX = 1000001;

LL factor[MAX] = {0};
void generatePrimeFactors() // GFG
{
  factor[1] = 1;

  for (LL i = 2; i < MAX; i++)
    factor[i] = i;

  for (LL i = 4; i < MAX; i += 2)
    factor[i] = 2;

  // A modified version of Sieve of Eratosthenes to
  // store the smallest prime factor that divides
  // every number.
  for (LL i = 3; i * i < MAX; i++) {
    // check if it has no prime factor.
    if (factor[i] == i) {
      // Initializes of j starting from i*i
      for (int j = i * i; j < MAX; j += i) {
        // if it has no prime factor before, then
        // stores the smallest prime divisor
        if (factor[j] == j)
          factor[j] = i;
      }
    }
  }
}

// function to calculate number of factors
LL calculateNoOFactors(LL n) // GFG
{
  if (n == 1)
    return 1;

  LL ans = 1;

  // stores the smallest prime number
  // that divides n
  LL dup = factor[n];

  // stores the count of number of times
  // a prime number divides n.
  LL c = 1;

  // reduces to the next number after prime
  // factorization of n
  LL j = n / factor[n];

  // false when prime factorization is done
  while (j != 1) {
    // if the same prime number is dividing n,
    // then we increase the count
    if (factor[j] == dup)
      c += 1;

    /* if its a new prime factor that is factorizing n,
       then we again set c=1 and change dup to the new
       prime factor, and apply the formula explained
       above. */
    else {
      dup = factor[j];
      ans = ans * (c + 1);
      c = 1;
    }

    // prime factorizes a number
    j = j / factor[j];
  }

  // for the last prime factor
  ans = ans * (c + 1);

  return ans;
}
void solve() {
  generatePrimeFactors();
  LL n;
  cin >> n;
  LL ans = 0;
  fe(i, 1, n - 1) {
    LL m = n - i;
    ans += calculateNoOFactors(m);
    // d2(m,ans);
  }
  cout << ans << endl;
}
int main() {
  fastIO TC(t) { solve(); }
  return 0;
}

Error: TLE

Fixed submission:


// Problem : C - A x B + C
// Contest : AtCoder - AtCoder Beginner Contest 179
// URL : https://atcoder.jp/contests/abc179/tasks/abc179_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("trapv")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-O2")
// #pragma GCC target("AVX")
#define endl '\\n'
#define rep() for (LL i = 0; i < n; i++)
#define f(i, s, n) for (LL i = s; i < n; i++)
#define fe(i, s, n) for (LL i = s; i <= n; i++)
#define rfe(i, s, n) for (LL i = s; i >= n; i--)
#define auto(m) for (auto &it : m)
#define f2(it, v) for (it = v.begin(); it != v.end(); ++it)
#define fori(it, x)                                                            \\
  for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define ip(n, a)                                                               \\
  LL n;                                                                        \\
  cin >> n;                                                                    \\
  LL a[n];                                                                     \\
  rep() { cin >> a[i]; }
#define op(n, a)                                                               \\
  f(i, 0, n) { cout << a[i] << " "; }                                          \\
  cout << endl;
#define i2p(n, k, a)                                                           \\
  LL n, k;                                                                     \\
  cin >> n >> k;                                                               \\
  LL a[n];                                                                     \\
  rep { cin >> a[i]; }
#define TCs(x)                                                                 \\
  LL x;                                                                        \\
  cin >> x;                                                                    \\
  while (x--)
#define TC(x)                                                                  \\
  LL x = 1;                                                                    \\
  while (x--)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define mem(a, x) memset(a, x, sizeof(a))
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define ps(x)                                                                  \\
  std::cout << std::fixed;                                                     \\
  std::cout << std::setprecision(x);
#define clz(a)                                                                 \\
  __builtin_clz(a) // count leading zeroes
                   // (geeksforgeeks.org/builtin-functions-gcc-compiler/)
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a)                                                                \\
  __builtin_popcount(a) // count set bits (for ints only diff for ll)
#define GCD(A, B) __gcd(A, B)
#define d1(x) cout << #x << ": " << x << endl
#define d2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define d3(x, y, z)                                                            \\
  cout << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define d4(a, b, c, d)                                                         \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define d5(a, b, c, d, e)                                                      \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define d6(a, b, c, d, e, f)                                                   \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \\
       << #f << ": " << f << endl
#define c1(a)                                                                  \\
  ;                                                                            \\
  LL a;                                                                        \\
  cin >> a;
#define c2(a, b)                                                               \\
  ;                                                                            \\
  LL a, b;                                                                     \\
  cin >> a >> b;
#define c3(a, b, c)                                                            \\
  ;                                                                            \\
  LL a, b, c;                                                                  \\
  cin >> a >> b >> c;
#define c4(a, b, c, d)                                                         \\
  ;                                                                            \\
  LL a, b, c, d;                                                               \\
  cin >> a >> b >> c >> d;
#define c5(a, b, c, d, e)                                                      \\
  ;                                                                            \\
  LL a, b, c, d, e;                                                            \\
  cin >> a >> b >> c >> d >> e;
#define c6(a, b, c, d, e, f)                                                   \\
  ;                                                                            \\
  LL a, b, c, d, e, f;                                                         \\
  cin >> a >> b >> c >> d >> e >> f;
#define c(a, n)                                                                \\
  ;                                                                            \\
  LL a[n];                                                                     \\
  f(i, 0, n) { cin >> a[i]; }
#define ce(a, n)                                                               \\
  ;                                                                            \\
  LL a[n + 1];                                                                 \\
  fe(i, 1, n) { cin >> a[i]; }
#define imin INT_MIN
#define imax INT_MAX

// Typedefs:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
typedef string S;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef vector<long long> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef vector<char> VC;
typedef pair<LL, LL> PLL;
typedef pair<string, LL> PSL;
typedef pair<LL, string> PLS;
typedef pair<char, LL> PCL;
typedef pair<LL, char> PLC;
typedef pair<string, string> PSS;
typedef pair<char, char> PCC;
typedef vector<PLL> VPLL;
typedef vector<PSL> VPSL;
typedef vector<PLS> VPLS;
typedef vector<PCL> VPCL;
typedef vector<PLC> VPLC;
typedef vector<PSS> VPSS;
typedef vector<PCC> VPCC;
typedef map<LL, LL> MLL;
typedef map<string, LL> MSL;
typedef map<char, LL> MCL;
typedef map<char, char> MCC;
typedef set<LL> SL;
typedef set<string> SS;
typedef set<char> SC;

int power(LL x, LL y) {
  if (y == 0)
    return 1;
  LL p = power(x, y / 2);
  p = (p * p);

  return (y % 2 == 0) ? p : (x * p);
}
const long long N = (long long)(1e5 + 1);
const long long N2 = (long long)(1e6 + 1);
const long long MOD = (long long)(1e9 + 7);
const long long INF = (long long)(2e18);

void solve() {
  LL n;
  cin >> n;
  LL ans = 0;
  fe(i, 1, n - 1) {
    LL m = n - i;
    LL x = sqrt(m);
    for (LL j = 1; j <= x; j++) {
      if (m % j == 0) {
        if (m / j == j) {

          ans++;
          // d3(m,j,ans);
        } else {
          ans += 2;
          // d3(m,j,ans);
        }
      }
    }
  }
  cout << ans << endl;
}
int main() {
  fastIO TC(t) { solve(); }
  return 0;
}