extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::fmt::{Display, Error, Formatter};
use std::ops::{Index, IndexMut, Add, AddAssign, Sub, SubAssign, Mul, MulAssign};
use std::collections::{VecDeque, BinaryHeap, BTreeMap};
use std::f32::MAX;

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}

fn make_sequence(n: usize, s: i64, w: i64) -> Vec<i64> {
    let mut result = Vec::with_capacity(n);
    let mut g = s;
    for _ in 0 .. n {
        result.push((g / 7) % 10);
        if g % 2 == 0 {
            g = g / 2;
        }else {
            g = (g / 2) ^ w;
        }
    }
    result
}
fn main() {
    loop {
        let_all!(n: usize, s: i64, w: i64, q: i64);
        if n == 0 && s == 0 && w == 0 && q == 0 {
            return;
        }
        let sequence = make_sequence(n, s, w);
        //show(&sequence);
        match q {
            2 | 5 => {
                let mut count = 0;
                let mut count_zero = 0;
                for i in 0 .. n {
                    if sequence[i] == 0 {
                        count_zero += 1;
                    }
                    if sequence[i] % q == 0 {
                        count += (i + 1 - count_zero) as i32;
                    }
                }
                println!("{}", count);
            }
            _ => {
                let mut map = BTreeMap::<i64, i32>::new();
                map.insert(0, 1);
                let mut digits = 1_i64;
                let mut prev = 0_i64;
                let mut count = 0;
                for i in 0 .. n {
                    prev = (prev +  digits * sequence[n - i - 1]) % q;
                    digits = (digits * 10) % q;
                    if let Some(c) = map.get_mut(&prev) {
                        if sequence[n - i - 1] != 0 {
                            count += *c;
                        }
                        *c += 1;
                        continue
                    }
                    map.insert(prev, 1);
                }
                println!("{}", count);
            }
        }
    }
}


Measured memory_bytes: 9008