Language: C++

#include <bits/stdc++.h>
using namespace std;

#define ll long long

ll L, A, B, mod;
ll pow10[30];

ll power(ll a, ll b) {
	if (b == 0) return 1;
	ll pp = power(a, b/2);
	pp = (pp*pp) % mod;
	if(b%2 == 0) return pp;
	return (pp*a)%mod;
}

ll f1(ll l, ll k) {
    if(l == 1) return 1;

    if(l % 2 == 0) {
        l /= 2;
        return (f1(l, k)*(1+power(pow10[k], l)))%mod;
    }

    return (f1(l-1, k)*pow10[k] + 1)%mod;
}

ll g1(ll l, ll k) {
    if(l == 1) return 0;
    if(l == 2) return 1;

    if(l % 2 == 0) {
        l /= 2;
        return (((g1(l, k)*power(pow10[k], l))%mod) + g1(l+1, k) + ((f1(l, k)*((l-1)%mod)) % mod))%mod;
    }

    return (g1(l-1, k)*pow10[k] + ((l-1)%mod)) % mod;
}

ll hmany[30];
ll astrt[30];
ll prev[30];
ll ppp[30];
ll answ[30];

int main() {
    cin >> L >> A >> B >> mod;
    pow10[0] = 1;
    for(int i = 1;i <= 20;i++) pow10[i] = (10 * pow10[i-1])%mod;

    long long ans = 0, p10=1;
    for(ll k = 1;k <= 18;k++) {
            p10 *= 10;
            ll tp = floor((p10-A-1)*1.0/B) + 1;
            if(tp > 0) {
                hmany[k]=tp;
            }
    }
    for(int i = 18;i > 1;i--) {
        hmany[i] -= hmany[i-1];
    }

    ll yt = 0;bool ttt = false;
    for(int i = 1;i <= 18;i++) {
        if(ttt) {
            hmany[i]=0;continue;
        }
        yt += hmany[i];
        if(yt > L) {
            yt -= hmany[i];
            hmany[i] = L - yt;
            ttt=true;
        }
    }

    bool start  = false;
    for(int i = 1;i <= 18;i++) {
        if(hmany[i] != 0) {
            if(!start) {
                astrt[i] = A%mod;
                start = true;
                prev[i] = (hmany[i])%mod;
            } else {
                astrt[i] = ((A % mod) + ((prev[i-1]*(B%mod))%mod))%mod;
                prev[i] = ((hmany[i]%mod)+prev[i-1])%mod;;
            }
        }
    }

    for(int k = 1;k <= 18;k++) {
        if(hmany[k] != 0) {
            ll l = hmany[k];
            ll a = astrt[k];
            answ[k] = ((a*f1(l, k))%mod + ((B%mod)*g1(l, k))%mod)%mod;
        }
    }

    ll pq = 0;
    for(int i = 18;i >= 1;i--) {

        answ[i] = (answ[i]*power(10, pq)) % mod;
        pq += i*hmany[i];
    }

    long long answer=0;
    for(int i=1;i<=18;i++) answer = (answer + answ[i])%mod;
    cout<< answer<< endl;
    return 0;
}

Compiler output:
a.cc:7:4: warning: built-in function 'pow10' declared as non-function [-Wbuiltin-declaration-mismatch]
    7 | ll pow10[30];
      |    ^~~~~
a.cc: In function 'int main()':
a.cc:82:17: error: reference to 'prev' is ambiguous
   82 |                 prev[i] = (hmany[i])%mod;
      |                 ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:42:4: note:                 'long long int prev [30]'
   42 | ll prev[30];
      |    ^~~~
a.cc:84:43: error: reference to 'prev' is ambiguous
   84 |                 astrt[i] = ((A % mod) + ((prev[i-1]*(B%mod))%mod))%mod;
      |                                           ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:42:4: note:                 'long long int prev [30]'
   42 | ll prev[30];
      |    ^~~~
a.cc:85:17: error: reference to 'prev' is ambiguous
   85 |                 prev[i] = ((hmany[i]%mod)+prev[i-1])%mod;;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:42:4: note:                 'long long int prev [30]'
   42 | ll prev[30];
      |    ^~~~
a.cc:85:43: error: reference to 'prev' is ambiguous
   85 |                 prev[i] = ((hmany[i]%mod)+prev[i-1])%mod;;
      |                                           ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:42:4: note:                 'long long int prev [30]'
   42 | ll prev[30];
      |    ^~~~