Problem p03775 (C++)

Buggy submission (status: Runtime Error):
// {{{
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
// }}}

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

static constexpr int mod = (int)1e9 + 7;
static constexpr int inf = 100100100;
static constexpr ll linf = 1e18;
static constexpr double eps = 1e-9;
static constexpr double pi = 3.14159265359;

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (ll i = n; i >= 0; --i)
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define pb push_back
#define ist insert
#define fst first
#define snd second

template <typename T> vector<T> trial_division(T num) {
  assert(num > 1);
  vector<T> factor;
  T n = sqrt(num) + 1;
  for (T i = 2; i < n; ++i) {
    while (num % i == 0) {
      num /= i;
      factor.push_back(i);
    }
  }
  if (num > 1) {
    factor.push_back(num);
  }
  return factor;
}

int main() {
  // cin.tie(0);
  // ios_base::sync_with_stdio(false);
  ll N;
  cin >> N;
  auto D = trial_division(N);
  ll ans = linf;
  for (ll i = 0; i < (1 << D.size()); ++i) {
    ll A = 1;
    ll B = 1;
    for (ll j = 0; j < D.size(); ++j) {
      if ((1 << j) & i) {
        A *= D[j];
      } else {
        B *= D[j];
      }
    }
    ans = min(ans, (ll)max(to_string(A).size(), to_string(B).size()));
  }
  cout << ans << endl;
}


Fixed submission:
// {{{
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
// }}}

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

static constexpr int mod = (int)1e9 + 7;
static constexpr int inf = 100100100;
static constexpr ll linf = 1e18;
static constexpr double eps = 1e-9;
static constexpr double pi = 3.14159265359;

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (ll i = n; i >= 0; --i)
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define pb push_back
#define ist insert
#define fst first
#define snd second

template <typename T> vector<T> trial_division(T num) {
  assert(num > 1);
  vector<T> factor;
  T n = sqrt(num) + 1;
  for (T i = 2; i < n; ++i) {
    while (num % i == 0) {
      num /= i;
      factor.push_back(i);
    }
  }
  if (num > 1) {
    factor.push_back(num);
  }
  return factor;
}

int main() {
  // cin.tie(0);
  // ios_base::sync_with_stdio(false);
  ll N;
  cin >> N;
  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }
  auto D = trial_division(N);
  ll ans = linf;
  for (ll i = 0; i < (1 << D.size()); ++i) {
    ll A = 1;
    ll B = 1;
    for (ll j = 0; j < D.size(); ++j) {
      if ((1 << j) & i) {
        A *= D[j];
      } else {
        B *= D[j];
      }
    }
    ans = min(ans, (ll)max(to_string(A).size(), to_string(B).size()));
  }
  cout << ans << endl;
}