Problem p03775 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define m0(x) memset(x, 0, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define int long long

using namespace std;

int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
int dxx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dyy[8] = {1, -1, 0, 1, -1, 0, 1, -1};

ll gcd(ll x, ll y) {
  ll m = max(x, y), n = min(x, y);
  if (m % n == 0)
    return n;
  else
    return gcd(m % n, n);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

ll myPow(ll x, ll n, ll m) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}

long long nCr(int n, int r) {
  if (r > n / 2)
    r = n - r; // because C(n, r) == C(n, n - r)
  long long ans = 1;
  int i;

  for (i = 1; i <= r; i++) {
    ans *= n - r + i;
    ans /= i;
  }

  return ans;
}

ll F(ll a, ll b) {
  ll c1 = 0, c2 = 0;
  while (a > 0) {
    a /= 10;
    c1++;
  }
  while (b > 0) {
    b /= 10;
    c2++;
  }
  return max(c1, c2);
}

bool used[20000000];
signed main() {
  m0(used);
  ll N;
  cin >> N;
  ll res = INF;
  ll m = (ll)sqrt(N) + 1;
  FOR(i, 1, m + 1000) {
    if (N % i == 0) {
      if (used[i]) {
        cout << res << endl;
        return 0;
      }
      used[i] = true;
      used[N / i] = true;
      res = min(res, F(i, N / i));
      if (i < 20000000 && N / i < 20000000)
        used[i] = used[N / i] = true;
    }
  }
  cout << res << endl;
  return 0;
}


Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define m0(x) memset(x, 0, sizeof(x))
#define fill(x, y) memset(x, y, sizeof(x))
#define int long long

using namespace std;

int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
int dxx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dyy[8] = {1, -1, 0, 1, -1, 0, 1, -1};

ll gcd(ll x, ll y) {
  ll m = max(x, y), n = min(x, y);
  if (m % n == 0)
    return n;
  else
    return gcd(m % n, n);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

ll myPow(ll x, ll n, ll m) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}

long long nCr(int n, int r) {
  if (r > n / 2)
    r = n - r; // because C(n, r) == C(n, n - r)
  long long ans = 1;
  int i;

  for (i = 1; i <= r; i++) {
    ans *= n - r + i;
    ans /= i;
  }

  return ans;
}

ll F(ll a, ll b) {
  ll c1 = 0, c2 = 0;
  while (a > 0) {
    a /= 10;
    c1++;
  }
  while (b > 0) {
    b /= 10;
    c2++;
  }
  return max(c1, c2);
}

bool used[20000000];
signed main() {
  m0(used);
  ll N;
  cin >> N;
  ll res = INF;
  ll m = (ll)sqrt(N) + 1;
  FOR(i, 1, m + 1000) {
    if (N % i == 0) {
      if (used[i]) {
        cout << res << endl;
        return 0;
      }
      res = min(res, F(i, N / i));
      if (i < 20000000 && N / i < 20000000)
        used[i] = used[N / i] = true;
    }
  }
  cout << res << endl;
  return 0;
}