Problem B. Bit by bit

Author:A. Klenin   Time limit:2 sec
Input file:input.txt   Memory limit:64 Mb
Output file:output.txt  

Statement

Some applications (such as compression algorithms) require searching a memory area for arbitrary bit strings.

Your program must find the first occurrence of the given bit string in the sequence of numbers generated by an arithmetic progression: a, a + b, a + 2b, …, a + N × b. Numbers are stored in a continuous memory area as 32-bit unsigned integers in the little-endian byte order. Bits in byte are counted from the least significant to the most significant one.

For example, number 123456 will be stored as a sequence of four bytes with hexadecimal values "40 E2 01 00", interpreted as bit string "00000010 01000111 10000000 00000000" (spaces inserted for readability only). So the occurrence of the bit substring "1001" would be at position 7.

Note that the occurrence can cross boundaries between numbers.

Input file format

First line of input file contains integers a b N. Second line of input file contains a string of '0' and '1' characters, each character representing one bit of the string to search for.

Output file format

Output file must contain a single integer — the position of the bit string in the memory area, starting from 1. If the bit string is not found, output file must contain 0 (zero).

Constraints

0 ≤ a, b, N, a + N × b ≤ 232 − 1

0 ≤ N ≤ 5 × 106

Bit string consists of 1 to 32 bits.

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
1 1 1
1
1
2
0 1 1
001
31
3
1 2 11
111
97

0.064s 0.011s 13