Problem p03454 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define int ll
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
typedef long long ll;
typedef pair<int, int> pii;
#define Rep(i, a, b) for (register int i = (a); i <= int(b); ++i)
#define Dep(i, a, b) for (register int i = (a); i >= int(b); --i)
#define rep(i, a, b) for (register int i = (a); i < int(b); ++i)
#define mem(x, v) memset(x, v, sizeof(x))
/*inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}*/
#define gc getchar
#define pc putchar
#define fi first
#define se second
#define debug(x) cout << #x " = " << x << endl;
#define pp(x, y) cout << "pp: " << x << " " << y << endl;
#define rank __RANK
inline ll read() {
  register ll x = 0, f = 1;
  register char c = gc();
  for (; !isdigit(c); c = gc())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = gc())
    x = (x << 1) + (x << 3) + (c ^ 48);
  return x * f;
}
#define rd read
void write(ll x) {
  if (x < 0)
    x = -x, pc('-');
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
void writeln(ll x) {
  write(x);
  puts("");
}
const int mod = 1e9 + 7;
inline int qpow(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1)
      ans = 1ll * ans * a % mod;
  return ans;
}
#define MN 40000005
int S, a[MN];
inline void add(ll &x, int y) { (x += y) >= mod ? x -= mod : 0; }
ll ans;
signed main() {
  S = rd();
  for (int i = 1; i <= 4e7; ++i)
    a[i] = a[i / 10] + 1;
  int r = 1, sum = 0;
  for (int i = 1; i < 1e7; ++i) {
    while (sum < S)
      sum += a[r], ++r;
    if (sum == S)
      ans = (ans + 1) % mod;
    sum -= a[i];
  }
  for (int i = 1; i <= S / 8; ++i) {
    if (S % i != 0) {
      ans = (ans + 1) % mod;
    } else if (S % i == 0) {
      ans =
          (ans + (qpow(10, S / i) - qpow(10, S / i - 1) - i + 1) % mod + mod) %
          mod;
    }
  }
  writeln(ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define int ll
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
typedef long long ll;
typedef pair<int, int> pii;
#define Rep(i, a, b) for (register int i = (a); i <= int(b); ++i)
#define Dep(i, a, b) for (register int i = (a); i >= int(b); --i)
#define rep(i, a, b) for (register int i = (a); i < int(b); ++i)
#define mem(x, v) memset(x, v, sizeof(x))
/*inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}*/
#define gc getchar
#define pc putchar
#define fi first
#define se second
#define debug(x) cout << #x " = " << x << endl;
#define pp(x, y) cout << "pp: " << x << " " << y << endl;
#define rank __RANK
inline ll read() {
  register ll x = 0, f = 1;
  register char c = gc();
  for (; !isdigit(c); c = gc())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = gc())
    x = (x << 1) + (x << 3) + (c ^ 48);
  return x * f;
}
#define rd read
void write(ll x) {
  if (x < 0)
    x = -x, pc('-');
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
void writeln(ll x) {
  write(x);
  puts("");
}
const int mod = 1e9 + 7;
inline int qpow(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1)
      ans = 1ll * ans * a % mod;
  return ans;
}
#define MN 40000005
int S;
unsigned a[MN];
inline void add(ll &x, int y) { (x += y) >= mod ? x -= mod : 0; }
ll ans;
signed main() {
  S = rd();
  for (int i = 1; i <= 4e7; ++i)
    a[i] = a[i / 10] + 1;
  int r = 1, sum = 0;
  for (int i = 1; i < 1e7; ++i) {
    while (sum < S)
      sum += a[r], ++r;
    if (sum == S)
      ans = (ans + 1) % mod;
    sum -= a[i];
  }
  for (int i = 1; i <= S / 8; ++i) {
    if (S % i != 0) {
      ans = (ans + 1) % mod;
    } else if (S % i == 0) {
      ans =
          (ans + (qpow(10, S / i) - qpow(10, S / i - 1) - i + 1) % mod + mod) %
          mod;
    }
  }
  writeln(ans);
}