Language: C++

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define pb push_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef multiset<ll> S;
typedef priority_queue<ll> PQ;
typedef priority_queue<P,vector<P>,greater<P>> SPQ;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
const ll inf=1001001001001001;
const int INF=1001001001;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())outp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size()){rep(j,v[i].size()){if(j)cout<<' ';cout<<v[i][j];}cout<<'\n';}}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll mod;
ll modpow(ll a,ll b){a%=mod;if(b==0)return 1;if(b&1)return a*modpow(a,b-1)%mod;ll k=modpow(a,b/2);return k*k%mod;}

int main(){
    ll n;cin>>n>>mod;
    string s;cin>>s;
    if(gcd(p,10)==1){
        vvi v(mod);
    vi t(n);
    ll sum=0;
    ll k=1,ans=0;
    for(ll i=n-1;i>=0;i--){
        sum+=(s[i]-'0')*k;
        k*=10;
        k%=mod;
        sum%=mod;
        v[sum].pb(n-1-i);
        t[n-1-i]=sum;
        if(sum==0)ans++;
    }
    rep(i,mod)ans+=(v[i].size()*(v[i].size()-1))/2;
    out(ans);
    }
    else{
        #include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define pb push_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef multiset<ll> S;
typedef priority_queue<ll> PQ;
typedef priority_queue<P,vector<P>,greater<P>> SPQ;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
const ll inf=1001001001001001;
const int INF=1001001001;
const int mod=1000000007;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())outp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size()){rep(j,v[i].size()){if(j)cout<<' ';cout<<v[i][j];}cout<<'\n';}}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){a%=mod;if(b==0)return 1;if(b&1)return a*modpow(a,b-1)%mod;ll k=modpow(a,b/2);return k*k%mod;}

int main(){
    int n,p;cin>>n>>p;
    string s;cin>>s;
    vvi v(p);
    vi t(n);
    ll sum=0;
    rep(i,n){
        sum*=10;
        sum+=s[i]-'0';
        sum%=p;
        v[sum].pb(i);
        t[i]=sum;
    }
    ll ans=0;
    rep(i,n){
        ans+=lb(v[t[i]],i);
        if(t[i]==0)ans++;
    }
    out(ans);
}

    }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:38:12: error: 'p' was not declared in this scope
   38 |     if(gcd(p,10)==1){
      |            ^
a.cc:80:1: error: a template declaration cannot appear at block scope
   80 | template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
      | ^~~~~~~~
a.cc:82:1: error: a template declaration cannot appear at block scope
   82 | template<class T> void out(T a){cout<<a<<'\n';}
      | ^~~~~~~~
a.cc:84:1: error: a template declaration cannot appear at block scope
   84 | template<class T> void outvp(T v){rep(i,v.size())outp(v[i]);}
      | ^~~~~~~~
a.cc:86:1: error: a template declaration cannot appear at block scope
   86 | template<class T> void outvv(T v){rep(i,v.size()){rep(j,v[i].size()){if(j)cout<<' ';cout<<v[i][j];}cout<<'\n';}}
      | ^~~~~~~~
a.cc:88:21: error: a function-definition is not allowed here before '{' token
   88 | ll modpow(ll a,ll b){a%=mod;if(b==0)return 1;if(b&1)return a*modpow(a,b-1)%mod;ll k=modpow(a,b/2);return k*k%mod;}
      |                     ^
a.cc:90:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
   90 | int main(){
      |         ^~
a.cc:90:9: note: remove parentheses to default-initialize a variable
   90 | int main(){
      |         ^~
      |         --
a.cc:90:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:90:11: error: a function-definition is not allowed here before '{' token
   90 | int main(){
      |           ^