Problem p01555 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
string s3 = "Fizz";
string s5 = "Buzz";
string s15 = "FizzBuzz";
typedef unsigned long long int ll;
string num(ll a) {
  string temp;
  ll keta = 1;
  while (a > 0) {
    char b = '0' + a % 10;
    temp += b;
    a /= 10;
  }
  ll n = temp.size();
  string s;
  for (ll i = 0; i < n; i++) {
    s += temp[n - 1 - i];
  }
  return s;
}
ll prenum(ll n) {
  ll keta = 1;
  ll sum = 0;
  ll m = 1;
  while (keta * 10 <= n) {
    ll n3 = keta * 3;
    ll n5 = keta * 9 / 5;
    ll n15 = keta * 9 / 15;
    sum += (keta * 9 + n15 - n3 - n5) * m;
    sum += (n3 + n5) * 4;
    keta *= 10;
    m++;
  }
  ll n3 = (n - keta) / 3;
  ll n5 = (n - 1) / 5 - (keta - 1) / 5;
  ll n15 = (n - 1) / 15 - (keta - 1) / 15;
  sum += (n - keta + n15 - n3 - n5) * m;
  sum += (n3 + n5) * 4;

  return sum;
}
void solve(ll s) {
  ll st = 0, e = 100000000000000000;
  while (e - st > 1) {
    ll m = (e + st) / 2;
    if (prenum(m) < s) {
      st = m;
    } else {
      e = m;
    }
  }

  string ans;
  ll se = prenum(st);
  for (ll i = st; ans.size() <= 30 + s - se; i++) {
    if (i % 15 == 0) {
      ans += s15;
    } else if (i % 3 == 0) {
      ans += s3;
    } else if (i % 5 == 0) {
      ans += s5;
    } else {
      ans += num(i);
    }
  }

  for (int i = 0; i < 20; i++) {
    cout << ans[i + s - se - 1];
  }
  cout << endl;
  //	cout<<ans<<endl;
  //	cout<<st<<' '<<e<<endl;
  //	cout<<prenum(st)<<endl;
  //	cout<<prenum(e)<<endl;
  //	cout<<se<<' '<<s<<' '<<prenum(se)<<endl;
  return;
}

int main() {
  //	cout<<num(89814814814814814)<<endl;
  ll s;
  cin >> s;
  solve(s);

  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
string s3 = "Fizz";
string s5 = "Buzz";
string s15 = "FizzBuzz";
typedef long long int ll;
string num(int a) {
  string temp;
  int keta = 1;
  while (a > 0) {
    char b = '0' + a % 10;
    temp += b;
    a /= 10;
  }
  int n = temp.size();
  string s;
  for (int i = 0; i < n; i++) {
    s += temp[n - 1 - i];
  }
  return s;
}
ll prenum(ll n) {
  ll keta = 1;
  ll sum = 0;
  ll m = 1;
  while (keta * 10 <= n) {
    ll n3 = keta * 3;
    ll n5 = keta * 9 / 5;
    ll n15 = keta * 9 / 15;
    sum += (keta * 9 + n15 - n3 - n5) * m;
    sum += (n3 + n5) * 4;
    keta *= 10;
    m++;
  }
  ll n3 = (n - keta) / 3;
  ll n5 = (n - 1) / 5 - (keta - 1) / 5;
  ll n15 = (n - 1) / 15 - (keta - 1) / 15;
  sum += (n - keta + n15 - n3 - n5) * m;
  sum += (n3 + n5) * 4;

  return sum;
}
void solve(ll s) {
  ll st = 0, e = 100000000000000000;
  while (e - st > 1) {
    ll m = (e + st) / 2;
    if (prenum(m) < s) {
      st = m;
    } else {
      e = m;
    }
  }
  string ans;
  for (int i = st; i < st + 20; i++) {
    if (i % 15 == 0) {
      ans += s15;
    } else if (i % 3 == 0) {
      ans += s3;
    } else if (i % 5 == 0) {
      ans += s5;
    } else {
      ans += num(i);
    }
  }
  ll se = prenum(st);
  for (int i = s - se - 1; i < s - se + 19; i++) {
    cout << ans[i];
  }
  cout << endl;
  return;
}

int main() {

  ll s;
  cin >> s;
  solve(s);

  return 0;
}