Language: C++


#include<vector>
#include<string>
#include<map>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, P;
    string S;
    cin >> N >> P >> S;
    vector<ll> digits(N);
    rep(i, 0, N) {
        digits[i] = (ll)(S[i] - '0');
    }
    if (P == 2) {
        ll ans = 0;
        rep(i, 0, N) {
            if (digits[i] % 2 == 0) {
                ans += (ll)(i + 1);
            }
        }
        cout << ans << endl;
        return 0;
    }
    if (P == 5) {
        ll ans = 0;
        rep(i, 0, N) {
            if (digits[i] == 5 || digits[i] == 0) {
                ans += (ll)(i + 1);
            }
        }
        cout << ans << endl;
        return 0;
    }
    vector<ll> U(N + 1, 0);
    ll pow = 1;
    ll ans = 0;
    map<ll, ll> cnt;
    ++cnt[0];
    rrep(i, N, 0) {
        U[i] = (U[i + 1] + (digits[i] * pow) % P) % P;
        ans += cnt[U[i]];
        ++cnt[U[i]];
        pow = (pow * 10) % P;
    }
    cout << ans << endl;
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:14:5: error: 'cin' was not declared in this scope
   14 |     cin.tie(0);
      |     ^~~
a.cc:5:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    4 | #include<map>
  +++ |+#include <iostream>
    5 | #define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
a.cc:15:10: error: incomplete type 'std::ios' {aka 'std::basic_ios<char>'} used in nested name specifier
   15 |     ios::sync_with_stdio(false);
      |          ^~~~~~~~~~~~~~~
a.cc:30:9: error: 'cout' was not declared in this scope
   30 |         cout << ans << endl;
      |         ^~~~
a.cc:30:9: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:30:24: error: 'endl' was not declared in this scope
   30 |         cout << ans << endl;
      |                        ^~~~
a.cc:5:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    4 | #include<map>
  +++ |+#include <ostream>
    5 | #define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
a.cc:40:9: error: 'cout' was not declared in this scope
   40 |         cout << ans << endl;
      |         ^~~~
a.cc:40:9: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:40:24: error: 'endl' was not declared in this scope
   40 |         cout << ans << endl;
      |                        ^~~~
a.cc:40:24: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc:54:5: error: 'cout' was not declared in this scope
   54 |     cout << ans << endl;
      |     ^~~~
a.cc:54:5: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:54:20: error: 'endl' was not declared in this scope
   54 |     cout << ans << endl;
      |                    ^~~~
a.cc:54:20: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'