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 <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <chrono>
#include <random>
#include <thread>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define all(c) c.begin(), c.end()
#define repeat(i, n) for (int i = 0; i < static_cast<int>(n); i++)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "{";
    for (auto it = v.begin(); it != v.end(); ++it) {
        if (it != v.begin())
            os << ",";
        os << *it;
    }
    return os << "}";
}

template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& v)
{
    os << "{";
    os << v.first << ", " << v.second;
    return os << "}";
}

template <typename T>
T input()
{
    T x;
    cin >> x;
    return x;
}

template <typename T>
vector<T> input_vector(size_t n)
{
    vector<T> x(n);
    for (int i = 0; i < n; i++)
        cin >> x[i];

    return x;
}

//template <uint_fast64_t MOD>
//struct mod_int {
//    mod_int() noexcept
//        : m_value(0)
//    {
//    }
//    constexpr mod_int(uint_fast64_t x) noexcept
//        : m_value((x + MOD) % MOD)
//    {
//    }
//
//    constexpr mod_int(long long x) noexcept
//        : m_value((x + MOD) % MOD)
//    {
//    }
//    constexpr mod_int(int x) noexcept
//        : m_value((x + MOD) % MOD)
//    {
//    }
//
//    constexpr mod_int operator+(const mod_int rhs) const noexcept
//    {
//        return mod_int(m_value) += rhs;
//    }
//    constexpr mod_int operator-(const mod_int rhs) const noexcept
//    {
//        return mod_int(m_value) -= rhs;
//    }
//    constexpr mod_int operator*(const mod_int rhs) const noexcept
//    {
//        return mod_int(m_value) *= rhs;
//    }
//    constexpr mod_int operator/(const mod_int rhs) const noexcept
//    {
//        return mod_int(m_value) /= rhs;
//    }
//    constexpr mod_int& operator+=(const mod_int rhs) noexcept
//    {
//        m_value += rhs.m_value;
//        if (m_value >= MOD)
//            m_value %= MOD;
//        return *this;
//    }
//    constexpr mod_int operator-=(const mod_int rhs) noexcept
//    {
//        if (m_value < rhs.m_value)
//            m_value += MOD;
//        m_value -= rhs.m_value;
//        return *this;
//    }
//    constexpr mod_int operator*=(const mod_int rhs) noexcept
//    {
//        m_value = m_value * rhs.m_value % MOD;
//        return *this;
//    }
//    constexpr mod_int operator/=(mod_int rhs) noexcept
//    {
//        uint_fast64_t exp = MOD - 2;
//        while (exp > 0) {
//            if (exp % 2 == 1)
//                *this *= rhs;
//            rhs *= rhs;
//            exp /= 2;
//        }
//
//        return *this;
//    }
//
//    mod_int inv() const noexcept
//    {
//        return mod_int(1) /= *this;
//    }
//    uint_fast64_t& value()
//    {
//        return m_value;
//    }
//
//private:
//    uint_fast64_t m_value;
//};
//
//template <uint_fast64_t MOD>
//ostream& operator<<(ostream& os, const mod_int<MOD>& v)
//{
//    return os << v.value();
//}
//
//template <uint_fast64_t MOD>
//istream& operator>>(istream& is, mod_int<MOD>& v)
//{
//    return is >> v.value();
//}

// ---------------------------------------------------------------------------

using point = complex<double>;
using ll = long long;

ll MOD = 2;
struct mod_int {
    mod_int() noexcept
        : m_value(0)
    {
    }
    mod_int(uint_fast64_t x) noexcept
        : m_value((x + MOD) % MOD)
    {
    }

    mod_int(long long x) noexcept
        : m_value((x + MOD) % MOD)
    {
    }
    mod_int(int x) noexcept
        : m_value((x + MOD) % MOD)
    {
    }

    mod_int operator+(const mod_int rhs) const noexcept
    {
        return mod_int(m_value) += rhs;
    }
    mod_int operator-(const mod_int rhs) const noexcept
    {
        return mod_int(m_value) -= rhs;
    }
    mod_int operator*(const mod_int rhs) const noexcept
    {
        return mod_int(m_value) *= rhs;
    }
     mod_int operator/(const mod_int rhs) const noexcept
    {
        return mod_int(m_value) /= rhs;
    }
     mod_int& operator+=(const mod_int rhs) noexcept
    {
        m_value += rhs.m_value;
        if (m_value >= MOD)
            m_value %= MOD;
        return *this;
    }
     mod_int operator-=(const mod_int rhs) noexcept
    {
        if (m_value < rhs.m_value)
            m_value += MOD;
        m_value -= rhs.m_value;
        return *this;
    }
     mod_int operator*=(const mod_int rhs) noexcept
    {
        m_value = m_value * rhs.m_value % MOD;
        return *this;
    }
     mod_int operator/=(mod_int rhs) noexcept
    {
        uint_fast64_t exp = MOD - 2;
        while (exp > 0) {
            if (exp % 2 == 1)
                *this *= rhs;
            rhs *= rhs;
            exp /= 2;
        }

        return *this;
    }

    mod_int inv() const noexcept
    {
        return mod_int(1) /= *this;
    }
    uint_fast64_t value() const
    {
        return m_value;
    }

private:
    uint_fast64_t m_value;
};

ostream& operator<<(ostream& os, const mod_int& v)
{
    return os << v.value();
}

//istream& operator>>(istream& is, mod_int& v)
//{
//    return is >> v.value();
//}

int my_main(int argc, char** argv)
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    ll n;
    cin >> n >> MOD;
    string s;
    cin >> s;

    vector<mod_int> a;
    a.push_back(0);

    mod_int r = 1;
    for (int i = s.size() - 1; i >= 0; i--) {
        int k = s[i] - '0';
        a.push_back(a.back() + r * k);
        r *= 10;
    }

    vector<ll> c(MOD + 1);
    for (mod_int m : a) {
        c[m.value()] += 1;
    }
    ll ret = 0;
    for (ll a : c) {
        ret += a * (a - 1) / 2;
    }
    cout << ret << endl;
    dump(c);
    return 0;
}

// ----------------------------------------------------------------------------
// Test driver

#ifdef DEBUG
#define MAY_RUN_TESTCASE
#endif

#ifdef MAY_RUN_TESTCASE
#include <fstream>

#if __has_include(<unistd.h>)
// Mac, Linux
#include <unistd.h>
#elif __has_include(<io.h>)
// Windows
#include <io.h>
#else
// Other?
#error "unistd.h or io.h not found"
#endif

bool test_file_exists(const std::string& str)
{
    std::ifstream fs(str);
    return fs.is_open();
}

#endif

int main(int argc, char** argv)
{
#ifndef MAY_RUN_TESTCASE
    return my_main(argc, argv);
#else
    if (argc == 1) {
        return my_main(argc, argv);
    } else if (argc == 2) {

        char* stdin_file = argv[1];
        freopen(stdin_file, "r", stdin);
        return my_main(argc, argv);

    } else if (argc == 5) {

        std::string stdin_file = argv[1];
        std::string expected_file = argv[2];
        std::string stdout_file = argv[3];
        std::string stderr_file = argv[4];

        if (!test_file_exists(stdin_file)) {
            std::cerr << stdin_file << " not found" << std::endl;
            return 3;
        }

        if (!test_file_exists(expected_file)) {
            std::cerr << expected_file << " not found" << std::endl;
            return 3;
        }

        int original_stdin = dup(fileno(stdin));
        int original_stdout = dup(fileno(stdout));
        int original_stderr = dup(fileno(stderr));

        freopen(stdin_file.c_str(), "r", stdin);
        freopen(stdout_file.c_str(), "w", stdout);
        freopen(stderr_file.c_str(), "w", stderr);

        int ret = my_main(argc, argv);

        fflush(stdout);
        fflush(stderr);

        dup2(original_stderr, fileno(stderr));
        dup2(original_stdout, fileno(stdout));
        dup2(original_stdin, fileno(stdin));

        if (ret != 0) {
            std::cerr << "main returns " << ret << std::endl;
            return ret;
        }

        std::ifstream inp(stdin_file);
        std::ifstream out(stdout_file);
        std::ifstream err(stderr_file);
        std::ifstream exp(expected_file);

        // Clear is needed if the file is empty.
        std::cout << "----- input -----" << std::endl;
        std::cout << inp.rdbuf() << std::endl;
        std::cout.clear();
        std::cout << "-----------------" << std::endl
                  << std::endl;

        std::cout << "----- output ----" << std::endl;
        std::cout << out.rdbuf() << std::endl;
        std::cout.clear();
        std::cout << "-----------------" << std::endl
                  << std::endl;

        std::cout << "---- expected ---" << std::endl;
        std::cout << exp.rdbuf() << std::endl;
        std::cout.clear();
        std::cout << "-----------------" << std::endl
                  << std::endl;

        std::cout << "----- stderr ----" << std::endl;
        std::cout << err.rdbuf() << std::endl;
        std::cout.clear();
        std::cout << "-----------------" << std::endl;

        inp.seekg(0);
        out.seekg(0);
        exp.seekg(0);
        err.seekg(0);

        std::string output_str, expected_str;
        {
            std::stringstream output_ss;
            output_ss << out.rdbuf();
            output_str = output_ss.str();

            std::stringstream expected_ss;
            expected_ss << exp.rdbuf();
            expected_str = expected_ss.str();
        }

        // Remove trailing spaces
        output_str.erase(output_str.find_last_not_of(" \n\r\t") + 1);
        expected_str.erase(expected_str.find_last_not_of(" \n\r\t") + 1);

        if (output_str == expected_str)
            return 0;
        else
            return 1;
    }
    return 1;
#endif
}