A pseudorandom bit generator keeps an integer state z with 0≤z<m. When the machine is switched on, the state is set to some integer in [0,m−1]; this starting value is called the seed. Each time a bit is requested, the generator first updates its state and then returns a bit, using three fixed integer constants a, c, and k:
z := floor((z * a + c) / k) mod m
if z < floor(m / 2):
return 0
else:
return 1
The generator was queried n times, producing a bit sequence b1,b2,…,bn. Given the constants and this sequence, determine how many of the possible seeds could have produced exactly this sequence.
The first line contains five integers a, c, k, m, and n, with 0≤a,c<m, 1≤k<m, 2≤m≤106, and 1≤n≤105.
The second line contains a string of exactly n characters, each 0 or 1; the i-th character is the bit bi.
Print a single integer: the number of seeds z with 0≤z<m that could have been the initial state, i.e. that produce exactly the given bit sequence.