Speed Reading Course
Time limit2sMemory limit512 MB
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 , , , and such that and are coprime, and the computer generates a sequence , where if and only if . Finally, Byteasar comes up with another, shorter sequence of symbols . 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, , , , , and (, , ), separated by single spaces. The numbers and are coprime. In the second line, there is a word , consisting of 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, holds;
- in other tests worth 8% of the total score, holds;
- in yet other tests worth 66% of the total score, holds.
Output
The first and only line of the standard output should contain an integer equal to the number of occurrences of the sequence in the sequence .
Notes
For , , , and , the computer generates the sequence as follows:
The sequence 101 occurs thrice in the sequence 101011010.