Problem p03454 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define rep(i, l, r) for (int i = l; i < r; i++)
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define pb(c) push_back(c)
#define vii vector<pair<int, int>>
#define ii pair<int, int>
#define max(a, b) (((a) < (b)) ? b : a)
#define min(a, b) ((a > b) ? b : a)
#define mp(i, j) make_pair(i, j)
#define ull unsigned long long int
#define ll long long int
#define pi 3.141592653589793238
#define mod ((int)1e9 + 7)
#define inf ((ll)1e15)
#define eps 1e-14,
#define maxlg 18
#define left(i) ((i) << 1)
#define right(i) (((i) << 1) + 1)
#define maxn 10005100
int n[maxn];

int get(int i) {
  if (i < maxn)
    return n[i];

  else
    return (1ll * n[maxn - 1] * n[i - (maxn - 1)]) % mod;
}

vi divs;
vi getAllDivisors(int num) {
  vector<int> divs;
  int square_root = (int)sqrt(num) + 1;
  for (int i = 1; i < square_root; i++) {
    if (num % i == 0) {
      if (i * i != num) {
        divs.pb(i);
      }
      divs.pb(num / i);
    }
  }
  return divs;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#ifndef ONLINE_JUDGE

#endif
  n[0] = 1;
  rep(i, 1, maxn) n[i] = (10l * n[i - 1]) % mod;
  int s;
  cin >> s;
  int l = 1, r = 0;
  int dl = 1, dr = 1;
  int sum = 0;
  ll ans = 0;
  while (dl < 8) {

    if (sum + dr < s) {
      r++;
      sum += dr;
      if ((r + 1) / n[dr] > 0) {
        dr++;
      }

    } else if (sum + dr > s) {
      l++;
      sum -= dl;
      if (l / n[dl] > 0) {
        dl++;
      }

    } else if (sum + dr == s) {
      l++;
      r++;
      sum = sum - dl + dr;
      if (l / n[dl] > 0) {
        dl++;
      }
      if ((r + 1) / n[dr] > 0) {
        dr++;
      }
      ans = (ans + 1);
      if (ans > mod)
        ans -= mod;
    }
  }

  ans = (ans + s / 8) % mod;
  divs = getAllDivisors(s);
  rep(i, 0, divs.size()) {
    if (divs[i] >= 8)
      ans = (ans + get(divs[i]) - get(divs[i] - 1) - s / divs[i] + mod) % mod;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define rep(i, l, r) for (int i = l; i < r; i++)
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define pb(c) push_back(c)
#define vii vector<pair<int, int>>
#define ii pair<int, int>
#define max(a, b) (((a) < (b)) ? b : a)
#define min(a, b) ((a > b) ? b : a)
#define mp(i, j) make_pair(i, j)
#define ull unsigned long long int
#define ll long long int
#define pi 3.141592653589793238
#define mod ((int)1e9 + 7)
#define inf ((ll)1e15)
#define eps 1e-14,
#define maxlg 18
#define left(i) ((i) << 1)
#define right(i) (((i) << 1) + 1)
#define maxn 10005100
int n[maxn];

int get(int i) {
  if (i < maxn)
    return n[i];

  int def = i - (maxn - 1);
  ll ans = n[maxn - 1];
  while (def > 0) {
    if (def > maxn)
      ans = (ans * n[maxn - 1]) % mod, def -= (maxn - 1);
    else
      ans = (ans * n[def]) % mod, def = 0;
  }
  return ans % mod;
}

vi divs;
vi getAllDivisors(int num) {
  vector<int> divs;
  int square_root = (int)sqrt(num) + 1;
  for (int i = 1; i < square_root; i++) {
    if (num % i == 0) {
      if (i * i != num) {
        divs.pb(i);
      }
      divs.pb(num / i);
    }
  }
  return divs;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#ifndef ONLINE_JUDGE

#endif
  n[0] = 1;
  rep(i, 1, maxn) n[i] = (10l * n[i - 1]) % mod;
  int s;
  cin >> s;
  int l = 1, r = 0;
  int dl = 1, dr = 1;
  int sum = 0;
  ll ans = 0;
  while (dl < 8) {

    if (sum + dr < s) {
      r++;
      sum += dr;
      if ((r + 1) / n[dr] > 0) {
        dr++;
      }

    } else if (sum + dr > s) {
      l++;
      sum -= dl;
      if (l / n[dl] > 0) {
        dl++;
      }

    } else if (sum + dr == s) {
      l++;
      r++;
      sum = sum - dl + dr;
      if (l / n[dl] > 0) {
        dl++;
      }
      if ((r + 1) / n[dr] > 0) {
        dr++;
      }
      ans = (ans + 1);
      if (ans > mod)
        ans -= mod;
    }
  }

  ans = (ans + s / 8) % mod;
  divs = getAllDivisors(s);
  rep(i, 0, divs.size()) {
    if (divs[i] >= 8)
      ans = (ans + get(divs[i]) - get(divs[i] - 1) - s / divs[i] + mod) % mod;
  }
  cout << ans;
  return 0;
}