Problem p03016 (C++)

Buggy submission (status: Time Limit Exceeded):
//
// Created by yamunaku on 2019/11/05.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define CFS                                                                    \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
#define CST(x) cout << fixed << setprecision(x)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> mti;
typedef vector<ll> vl;
typedef vector<vector<ll>> mtl;

ll b, m;

ll modpow(ll x, ll a) {
  ll ans = 1;
  while (a > 0) {
    if (a % 2)
      ans = ans * x % m;
    a /= 2;
    x = x * x % m;
  }
  return ans;
}

void matprod(mtl &ret, mtl &x, mtl &y) {
  int n = x.size();
  mtl ans = mtl(n, vl(n, 0));
  rep(i, n) {
    rep(j, n) {
      rep(k, n) { ans[i][j] = (ans[i][j] + x[i][k] * y[k][j] % m) % m; }
    }
  }
  ret = ans;
}

void matpow(mtl &ret, mtl &x, ll a) {
  int n = x.size();
  mtl ans = mtl(n, vl(n, 0));
  rep(i, n) ans[i][i] = 1;
  while (a > 0) {
    if (a % 2)
      matprod(ans, ans, x);
    a /= 2;
    matprod(x, x, x);
  }
  ret = ans;
}

ll solve(ll n, ll c, ll t) {
  mtl x = {{1, 0, 0}, {b, 1, 0}, {0, 1, t}};
  matpow(x, x, n);
  return (x[2][0] + x[2][1] * c % m) % m;
}

int main() {
  ll s, a;
  cin >> s >> a >> b >> m;
  ll ans = 0;
  ll now = 0;
  for (ll t = 10, k = 1; now < s && k <= 18; t *= 10, k++) {
    if (b * now + a < t) {
      ll w = max(0ll, min((t - 1 - (b * now + a)) / b + 1, s - now));
      ans = ans * modpow(10, w * k) % m;
      ans = (ans + solve(w, (b * now + a) % m, t % m)) % m;
      now += w;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
//
// Created by yamunaku on 2019/11/05.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define CFS                                                                    \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
#define CST(x) cout << fixed << setprecision(x)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> mti;
typedef vector<ll> vl;
typedef vector<vector<ll>> mtl;

ll b, m;

ll modpow(ll x, ll a) {
  ll ans = 1;
  while (a > 0) {
    if (a % 2)
      ans = ans * x % m;
    a /= 2;
    x = x * x % m;
  }
  return ans;
}

void matprod(mtl &ret, mtl &x, mtl &y) {
  int n = x.size();
  mtl ans = mtl(n, vl(n, 0));
  rep(i, n) {
    rep(j, n) {
      rep(k, n) { ans[i][j] = (ans[i][j] + x[i][k] * y[k][j] % m) % m; }
    }
  }
  ret = ans;
}

void matpow(mtl &ret, mtl &x, ll a) {
  int n = x.size();
  mtl ans = mtl(n, vl(n, 0));
  rep(i, n) ans[i][i] = 1;
  while (a > 0) {
    if (a % 2)
      matprod(ans, ans, x);
    a /= 2;
    matprod(x, x, x);
  }
  ret = ans;
}

ll solve(ll n, ll c, ll t) {
  mtl x = {{1, 0, 0}, {b, 1, 0}, {0, 1, t}};
  matpow(x, x, n);
  return (x[2][0] + x[2][1] * c % m) % m;
}

int main() {
  ll s, a;
  cin >> s >> a >> b >> m;
  ll ans = 0;
  ll now = 0;
  for (ll t = 10, k = 1; now < s && k <= 18; t *= 10, k++) {
    if (b * now + a < t) {
      ll w = max(0ll, min((t - 1 - (b * now + a)) / b + 1, s - now));
      rep(i, k) { ans = ans * modpow(10, w) % m; }
      ans = (ans + solve(w, (b * now + a) % m, t % m)) % m;
      now += w;
    }
  }
  cout << ans << endl;
  return 0;
}