#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <numeric>

using namespace std;
using ll = long long;

#define show(x) cerr << #x << " = " << x << endl
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}


struct Sorted {
    using T = vector<ll>;

    struct AccMonoid {
        T operator()(const T& a, const T& b) const
        {
            T c(a.size() + b.size());
            merge(a.begin(), a.end(), b.begin(), b.end(), c.begin());
            return c;
        }
        static T identity() { return vector<ll>{}; }
    };
};

template <typename Base>
class SegmentTree
{
public:
    using BaseAlgebra = Base;
    using AccMonoid = typename BaseAlgebra::AccMonoid;
    using T = typename BaseAlgebra::T;

    SegmentTree(const std::vector<T>& val) : data_num(val.size()), height(__lg(2 * data_num - 1)), size(1 << (1 + height)), half(size >> 1), value(size)
    {
        for (int data = 0; data < half; data++) {
            if (data < data_num) {
                value[data + half] = val[data];
            } else {
                value[data + half] = AccMonoid::identity();
            }
        }
        for (int node = half - 1; node >= 1; node--) {
            value[node] = acc(value[2 * node], value[2 * node + 1]);
        }
    }
    int query(const int a, const int b, const ll v) const
    {
        assert(0 <= a and a < b and b <= data_num);
        return queryRec(1, 0, half, a, b, v);
    }

private:
    int queryRec(const int int_index, const int int_left, const int int_right, const int mod_left, const int mod_right, const ll v) const
    {
        if (mod_left <= int_left and int_right <= mod_right) {
            return upper_bound(value[int_index].begin(), value[int_index].end(), v) - lower_bound(value[int_index].begin(), value[int_index].end(), v);
        } else if (int_right <= mod_left or mod_right <= int_left) {
            return 0;
        } else {
            return queryRec(2 * int_index, int_left, (int_left + int_right) / 2, mod_left, mod_right, v) + queryRec(2 * int_index + 1, (int_left + int_right) / 2, int_right, mod_left, mod_right, v);
        }
    }
    const int data_num;  // Num of valid data on leaves.
    const int height;
    const int size;
    const int half;
    vector<T> value;  // Tree for value(length: size)
    const AccMonoid acc{};
};


void gen(vector<ll>& a, const int N, const ll S, const ll W)
{
    ll g = S;
    for (int i = 0; i < N; i++) {
        a[i] = (g / 7) % 10;
        if (g % 2 == 0) {
            g = (g / 2);
        } else {
            g = (g / 2) ^ W;
        }
    }
}

int main()
{
    while (true) {
        int N;
        ll S, W, Q;
        cin >> N >> S >> W >> Q;
        if (N == 0 and S == 0 and W == 0 and Q == 0) {
            break;
        }
        vector<ll> a(N);
        gen(a, N, S, W);

        if (Q != 2 and Q != 5) {
            vector<ll> hash(N + 1, 0);  // a_(N-i)..a_(N-1)
            ll base = 10;
            for (int i = 0; i < N; i++) {
                hash[i + 1] = (hash[i] + a[N - i - 1] * base) % Q;
                base = (base * 10) % Q;
            }
            vector<vector<ll>> sub(N + 1);
            for (int i = 0; i <= N; i++) {
                sub[i] = vector<ll>{hash[i]};
            }
            for (int i = 0; i < N; i++) {
                if (i < N and a[N - i - 1] == 0) {
                    sub[i + 1] = vector<ll>{Q};
                }
            }
            SegmentTree<Sorted> seg(sub);
            ll ans = 0;
            for (int i = 0; i < N; i++) {
                const ll a = hash[i];
                const int num = seg.query(i + 1, N + 1, a);
                ans += num;
            }
            cout << ans << endl;
        } else {
            vector<int> num(N, 0);
            num[N - 1] = (a[N - 1] % Q == 0 ? 1 : 0);
            for (int i = N - 2; i >= 0; i--) {
                num[i] = num[i + 1] + (a[i] % Q == 0 ? 1 : 0);
            }
            ll ans = 0;
            for (int i = 0; i < N; i++) {
                if (a[i] == 0) {
                    continue;
                }
                ans += num[i];
            }
            cout << ans << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 34060