Problem:
Score : 500 points

Problem Statement

Takahashi has a string S of length N consisting of digits from 0 through 9.

He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.

Here substrings starting with a 0 also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.

Compute this count to help Takahashi.

Constraints

1 \leq N \leq 2 \times 10^5

S consists of digits.

|S| = N

2 \leq P \leq 10000

P is a prime number.

Input

Input is given from Standard Input in the following format:

N P
S

Output

Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten.

Sample Input 1

4 3
3543

Sample Output 1

6

Here S = 3543. There are ten non-empty (contiguous) substrings of S:

3: divisible by 3.

35: not divisible by 3.

354: divisible by 3.

3543: divisible by 3.

5: not divisible by 3.

54: divisible by 3.

543: divisible by 3.

4: not divisible by 3.

43: not divisible by 3.

3: divisible by 3.

Six of these are divisible by 3, so print 6.

Sample Input 2

4 2
2020

Sample Output 2

10

Here S = 2020. There are ten non-empty (contiguous) substrings of S, all of which are divisible by 2, so print 10.

Note that substrings beginning with a 0 also count.

Sample Input 3

20 11
33883322005544116655

Sample Output 3

68

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define mp(a,b) make_pair(a,b)
#define ff first
#define setp(a) setprecision(a)<<fixed
#define ss second
#define fori(v) for(int i=0; i<v; i++)
#define forj(v) for(int j=0; j<v; j++)
#define fork(v) for(int k=0; k<v; k++)
#define forl(v) for(int l=0; l<v; l++)
#define fort(v) for(int t=0; t<v; t++)
#define forz(v) for(int z=0; z<v; z++)
#define forx(v) for(int x=0; x<v; x++)
#define ll long long
#define lll __int128
#define MAX (int)(4*pow(10,5) + 100)
#define pb(a) push_back(a)
#define ch (int)(4*pow(10,4) + 100)
#define ld long double
// #define cout out
// #define cin in
ll inf = pow(10,18);
ll modulo = pow(10,9)+7 ;
double eps = 1e-9;
ifstream in;
ofstream out;

ll arr[MAX];
ll pv[MAX];
ll invpv[MAX];


ll pow_mod(ll a, ll b){
	a = a%modulo;
	if(!a){
		return 0;
	}
	ll mx = 0;
	ll has = 1;
	for(ll i = 62; i>-1; i--){
		has*=has;
		has%=modulo;
		if(((ll)1<<i)&b)
			has*=a, has%=modulo;
	}
	return has;
}

 

void div_conq(ll& ans, ll l, ll r, string& a){
	if(l > r){
		return;
	}
//	cout<<"\n\n\n we at "<<l<<" "<<r<<"\n\n\n\n";
	ll mid = (l+r)/2;
	ll s=  0;
	ll mult = 1;
	for(ll i = mid; i>=l; i--){
		ll cur = a[i]-'0';
		cur *= mult;
		cur%=modulo;
		s+=cur;
		s%=modulo;
//		cout<<s<<" at "<<i<<endl;
		arr[s]++;
		
		mult*=10;
		mult%=modulo;
	}
	
	ans+=arr[0];
//	cout<<"we added "<<arr[0]<<endl;
	
	s = 0;
	for(ll i = mid+1; i<=r; i++){
		s*=10;
		s+=a[i]-'0';
		s%=modulo;
		ll lz = s;
		lz = modulo-lz;
		lz %= modulo;
		ll dist = i-mid;
		ll ten = invpv[dist];
		if(ten == 0){
			ans+=(mid-l+1);
			continue;
		}
		lz*=ten;
		lz%=modulo;
		ans+=arr[lz];
//		cout<<"we at "<<i<<" we added "<<arr[s]<<" because we want "<<s<<endl;
		
	}
	
	s=  0;
	mult = 1;
	for(ll i = mid; i>=l; i--){
		ll cur = a[i]-'0';
		cur *= mult;
		cur%=modulo;
		s+=cur;
//		cout<<s<<" at "<<i<<endl;
		s%=modulo;
//		cout<<s<<" at "<<i<<endl;
		arr[s]--;
		
		mult*=10;
		mult%=modulo;
	}
	
	div_conq(ans, l, mid-1, a);
	div_conq(ans, mid+1, r, a);
}
 
void deal(){
	ll n, p;
	cin>>n>>p;
	modulo = p;
	string a;
	cin>>a;
	
	pv[0] = 1, invpv[0] = 1;
	for(ll i = 1; i<MAX; i++){
		pv[i] = pv[i-1] * 10;
		pv[i] %= modulo;
		invpv[i] = pow_mod(pv[i], modulo-2);
	}
	
	ll ans = 0;
	div_conq(ans, 0, n-1, a);
	cout<<ans;
}
 
 
int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	deal();
}
 
 
 
/* 
6 8 
1 2
2 3
3 4
4 1
2 5
3 5
1 6
4 6
*/