Problem p03454 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
// 82

using namespace std;

const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e5;

ll sp[100];
int p10[MAXN + 1];

inline int lgput(int a, int b) {
  int ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = (1LL * ans * a) % MOD;
    b >>= 1;
    a = (1LL * a * a) % MOD;
  }
  return ans;
}

int main() {
  // ifstream cin("A.in");
  // ofstream cout("A.out");
  int i, s, d;
  ios::sync_with_stdio(false);
  cin >> s;
  p10[0] = 1;
  for (i = 1; i <= MAXN; i++) {
    p10[i] = (1LL * p10[i - 1] * 10) % MOD;
  }
  for (i = 1; i <= 15; i++) {
    sp[i] = sp[i - 1] + 1LL * i * 9 * p10[i - 1];
  }
  int ans = 0;
  for (d = 1; d * d <= s; d++) {
    if (s % d == 0) {
      if (s / d <= 1LL * 9 * p10[d - 1] || d > 9) {
        ans = (ans + 1LL * 9 * p10[d - 1] - s / d + 1) % MOD;
      }
      if (d * d < s) {
        int a = lgput(10, s / d - 1);
        if (d <= 1LL * 9 * a || s / d > 9) {
          ans = (ans + 1LL * 9 * a - d + 1) % MOD;
        }
      }
    }
  }
  for (i = 1; i <= 7; i++) {
    int dim = 9 * p10[i - 1];
    int sum = 0;
    for (int j = 1; j <= dim && sum + i < s; j++) {
      sum += i;
      int rez = 0;
      for (int pas = 1 << 5; pas; pas >>= 1) {
        if (rez + pas <= 8 && sp[rez + pas] - sp[i] + sum <= s)
          rez += pas;
      }
      if ((s - sum - (sp[rez] - sp[i])) % (rez + 1) == 0) {
        ans++;
      }
    }
  }
  for (i = 8; i + i + 1 <= s; i++) {
    int sum = 0;
    while (sum + i < s) {
      sum += i;
      if ((s - sum) % (i + 1) == 0)
        ans++;
    }
  }
  cout << ans;
  // cin.close();
  // cout.close();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
// 82

using namespace std;

const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e5;

ll sp[100];
int p10[MAXN + 1];

inline int lgput(int a, int b) {
  int ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = (1LL * ans * a) % MOD;
    b >>= 1;
    a = (1LL * a * a) % MOD;
  }
  return ans;
}

int main() {
  // ifstream cin("A.in");
  // ofstream cout("A.out");
  int i, s, d;
  ios::sync_with_stdio(false);
  cin >> s;
  p10[0] = 1;
  for (i = 1; i <= MAXN; i++) {
    p10[i] = (1LL * p10[i - 1] * 10) % MOD;
  }
  for (i = 1; i <= 15; i++) {
    sp[i] = sp[i - 1] + 1LL * i * 9 * p10[i - 1];
  }
  int ans = 0;
  for (d = 1; d * d <= s; d++) {
    if (s % d == 0) {
      if (s / d <= 1LL * 9 * p10[d - 1] || d > 9) {
        ans = (ans + 1LL * 9 * p10[d - 1] - s / d + 1) % MOD;
      }
      if (d * d < s) {
        int a = lgput(10, s / d - 1);
        if (d <= 1LL * 9 * a || s / d > 9) {
          ans = (ans + 1LL * 9 * a - d + 1) % MOD;
        }
      }
    }
  }
  for (i = 1; i <= 7; i++) {
    int dim = 9 * p10[i - 1];
    int sum = 0;
    for (int j = 1; j <= dim && sum + i < s; j++) {
      sum += i;
      int rez = 0;
      for (int pas = 1 << 5; pas; pas >>= 1) {
        if (rez + pas <= 8 && sp[rez + pas] - sp[i] + sum <= s)
          rez += pas;
      }
      if ((s - sum - (sp[rez] - sp[i])) % (rez + 1) == 0) {
        ans++;
      }
    }
  }
  for (i = 1; i <= s / 8; i++) {
    if (s % i)
      ans++;
  }
  cout << ans;
  // cin.close();
  // cout.close();
  return 0;
}