Problem p03016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;

int Mod;

struct Matrix {
  int n, m;
  ll a[10][10];

  Matrix(int _n = 0, int _m = 0, int x = 0) {
    memset(a, 0, sizeof a);
    n = _n, m = _m;
    for (int i = 1; i <= n; ++i)
      a[i][i] = x;
  }

  Matrix operator*(const Matrix &b) {
    Matrix res = Matrix(n, b.m);
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j)
        if (a[i][j])
          for (int k = 1; k <= b.m; ++k)
            (res.a[i][k] += a[i][j] * b.a[j][k] % Mod) %= Mod;
    return res;
  }

  Matrix operator^(ll k) {
    Matrix res = Matrix(n, n, 1), x = *this;
    while (k) {
      if (k & 1)
        res = res * x;
      x = x * x;
      k >>= 1;
    }
    return res;
  }
};

inline ll fsp(ll x, ll k) {
  x = x % Mod;
  ll res = 1;
  while (k) {
    if (k & 1)
      res = res * x % Mod;
    x = x * x % Mod;
    k >>= 1;
  }
  return res;
}

int main() {
  freopen("work.in", "r", stdin);
  freopen("work.out", "w", stdout);
  ll n, a, d;
  cin >> n >> a >> d >> Mod;
  ll b = 1, ans = 0;
  for (int i = 1; i <= 18; ++i, b = b * 10) {
    ll L = b, R = 10 * b - 1, s;
    if (R < a)
      continue;
    if (L <= a)
      L = 1;
    else
      L = (L - a - 1) / d + 2;
    R = (R - a) / d + 1;
    L = max(L, 1ULL), R = min(R, 1ULL * n);
    if (L > R)
      continue;
    s = (a + (L - 1) * (d));
    s %= Mod;
    Matrix base = Matrix(3, 3, 0);
    base.a[1][1] = b * 10 % Mod;
    base.a[2][1] = base.a[2][2] = base.a[3][2] = base.a[3][3] = 1;
    base = base ^ (R - L + 1);
    ans =
        ((ans * fsp(b * 10, (R - L + 1)) % Mod + s % Mod * base.a[2][1] % Mod) %
             Mod +
         d % Mod * base.a[3][1] % Mod) %
        Mod;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;

int Mod;

struct Matrix {
  int n, m;
  ll a[10][10];

  Matrix(int _n = 0, int _m = 0, int x = 0) {
    memset(a, 0, sizeof a);
    n = _n, m = _m;
    for (int i = 1; i <= n; ++i)
      a[i][i] = x;
  }

  Matrix operator*(const Matrix &b) {
    Matrix res = Matrix(n, b.m);
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j)
        if (a[i][j])
          for (int k = 1; k <= b.m; ++k)
            (res.a[i][k] += a[i][j] * b.a[j][k] % Mod) %= Mod;
    return res;
  }

  Matrix operator^(ll k) {
    Matrix res = Matrix(n, n, 1), x = *this;
    while (k) {
      if (k & 1)
        res = res * x;
      x = x * x;
      k >>= 1;
    }
    return res;
  }
};

inline ll fsp(ll x, ll k) {
  x = x % Mod;
  ll res = 1;
  while (k) {
    if (k & 1)
      res = res * x % Mod;
    x = x * x % Mod;
    k >>= 1;
  }
  return res;
}

int main() {
  ll n, a, d;
  cin >> n >> a >> d >> Mod;
  ll b = 1, ans = 0;
  for (int i = 1; i <= 18; ++i, b = b * 10) {
    ll L = b, R = 10 * b - 1, s;
    if (R < a)
      continue;
    if (L <= a)
      L = 1;
    else
      L = (L - a - 1) / d + 2;
    R = (R - a) / d + 1;
    L = max(L, 1ULL), R = min(R, 1ULL * n);
    if (L > R)
      continue;
    s = (a + (L - 1) * (d));
    s %= Mod;
    Matrix base = Matrix(3, 3, 0);
    base.a[1][1] = b * 10 % Mod;
    base.a[2][1] = base.a[2][2] = base.a[3][2] = base.a[3][3] = 1;
    base = base ^ (R - L + 1);
    ans =
        ((ans * fsp(b * 10, (R - L + 1)) % Mod + s % Mod * base.a[2][1] % Mod) %
             Mod +
         d % Mod * base.a[3][1] % Mod) %
        Mod;
  }
  cout << ans << endl;
  return 0;
}