This page is still under construction.

Parts of this page are still being built. What you see may change.

Speed Reading Course

Time limit2sMemory limit512 MB

Summary
Count how many positions i where c contains the given m-bit word w, with c defined by an arithmetic progression modulo n against threshold p.
Level

Hard9 of 10

Topics
Number theory, String matching, Prefix sum, Math
Solved
No attempts yet

Problem

Byteasar has enrolled in a speed reading course, which taught him many exercises for improving perception. His favorite one is finding a pattern in a sequence of symbols. For this exercise, Byteasar has a computer generate a very long sequence of zeros and ones as follows. He chooses integers nn, aa, bb, and pp such that nn and aa are coprime, and the computer generates a sequence c0,c1,…,cn−1c_0, c_1, \dots, c_{n-1}, where ci=0c_i = 0 if and only if (ai+b) mod n<p(ai+b) \bmod n < p. Finally, Byteasar comes up with another, shorter sequence of mm symbols w0,w1,…,wm−1w_0, w_1, \dots, w_{m-1}. Set up with these, his task is to find all occurrences of the shorter sequence in the one generated by the computer as quickly as possible. He has asked for your help in writing a program that will verify whether he indeed found all the occurrences.

Input

The first line of the standard input contains five integers, nn, aa, bb, pp, and mm (2≤n≤1 000 000 0002 \le n \le 1\,000\,000\,000, 1≤p,a,b,m<n1 \le p, a, b, m < n, 1≤m≤1 000 0001 \le m \le 1\,000\,000), separated by single spaces. The numbers aa and nn are coprime. In the second line, there is a word w0,w1,…,wm−1w_0, w_1, \dots, w_{m-1}, consisting of mm symbols, each either 0 or 1. The following mutually exclusive classes form a subset of all the test inputs:

  • in tests worth 8% of the total score, n≤1000n \le 1000 holds;
  • in other tests worth 8% of the total score, n≤1 000 000n \le 1\,000\,000 holds;
  • in yet other tests worth 66% of the total score, m≤1000m \le 1000 holds.

Output

The first and only line of the standard output should contain an integer equal to the number of occurrences of the sequence w0,w1,…,wm−1w_0, w_1, \dots, w_{m-1} in the sequence c0,c1,…,cn−1c_0, c_1, \dots, c_{n-1}.

Notes

For n=9n = 9, a=5a = 5, b=6b = 6, and p=4p = 4, the computer generates the sequence as follows:

ii012345678
ai+bai + b61116212631364146
(ai+b) mod n(ai + b) \bmod n627384051
cic_i101011010

The sequence 101 occurs thrice in the sequence 101011010.

Examples1

  1. Example 1

    Input
    9 5 6 4 3
    101
    
    Expected output
    3