Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;

vector<int> makePrimeLst(int N) {
  if (N < 2)
    return vector<int>(0);
  vector<int> ans;
  vector<int> alive(N + 1, 1);
  for (int i = 2; (long long)i * i <= (long long)N; i++) {
    if (alive[i])
      ans.push_back(i);
    for (int t = i; t <= N; t += i)
      alive[t] = 0;
  }
  for (int i = ans.back() + 1; i <= N; i++)
    if (alive[i])
      ans.push_back(i);
  return ans;
}

// 素因数分解
map<lint, int> primeFactorize(lint N, vector<int> &primeLst) {
  map<lint, int> ans;
  for (auto v : primeLst) {
    while (!(N % v)) {
      N /= v;
      ans[v]++;
    }
    if (N == 1)
      return ans;
  }
  ans[N]++;
  return ans;
}

lint n, s;

lint func(lint b, lint n) {
  if (n < b)
    return n;
  return n % b + func(b, n / b);
}

int main() {
  cin >> n >> s;
  lint m = n - s;
  if (m < 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> primes = makePrimeLst((int)sqrt(m));
  map<lint, int> f = primeFactorize(m, primes);

  vector<lint> b;

  vector<lint> p, nv;
  int num = 1;
  for (auto v : f) {
    p.push_back(v.first);
    nv.push_back(v.second);
    num *= (nv.back() + 1);
  }
  for (int i = 0; i < num; i++) {
    lint prod = 1;
    int tmp = i;
    for (int j = 0; j < p.size(); j++) {
      int times = tmp % (nv[j] + 1);
      tmp /= (nv[j] + 1);
      while (times) {
        prod *= p[j];
        times--;
      }
    }
    b.push_back(prod + 1);
  }
  sort(b.begin(), b.end());
  for (auto ans : b) {
    if (func(ans, n) == s) {
      cout << ans << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;

vector<int> makePrimeLst(int N) {
  if (N < 2)
    return vector<int>(0);
  vector<int> ans;
  vector<int> alive(N + 1, 1);
  for (int i = 2; (long long)i * i <= (long long)N; i++) {
    if (alive[i])
      ans.push_back(i);
    for (int t = i; t <= N; t += i)
      alive[t] = 0;
  }
  for (int i = ans.back() + 1; i <= N; i++)
    if (alive[i])
      ans.push_back(i);
  return ans;
}

// 素因数分解
map<lint, int> primeFactorize(lint N, vector<int> &primeLst) {
  map<lint, int> ans;
  for (auto v : primeLst) {
    while (!(N % v)) {
      N /= v;
      ans[v]++;
    }
    if (N == 1)
      return ans;
  }
  ans[N]++;
  return ans;
}

lint n, s;

lint func(lint b, lint n) {
  if (n < b)
    return n;
  return n % b + func(b, n / b);
}

int main() {
  cin >> n >> s;
  lint m = n - s;
  if (m < 0) {
    cout << -1 << endl;
    return 0;
  }
  if (m == 0) {
    cout << n + 1 << endl;
    return 0;
  }
  vector<int> primes = makePrimeLst((int)sqrt(m));
  map<lint, int> f = primeFactorize(m, primes);

  vector<lint> b;

  vector<lint> p, nv;
  int num = 1;
  for (auto v : f) {
    p.push_back(v.first);
    nv.push_back(v.second);
    num *= (nv.back() + 1);
  }
  for (int i = 0; i < num; i++) {
    lint prod = 1;
    int tmp = i;
    for (int j = 0; j < p.size(); j++) {
      int times = tmp % (nv[j] + 1);
      tmp /= (nv[j] + 1);
      while (times) {
        prod *= p[j];
        times--;
      }
    }
    b.push_back(prod + 1);
  }
  sort(b.begin(), b.end());
  for (auto ans : b) {
    if (func(ans, n) == s) {
      cout << ans << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}