Zigzag Numbers
Time limit2sMemory limit128 MB
Count numbers in [A, B], up to 500 digits, that are divisible by M and whose adjacent digit comparisons alternate up then down.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Math, Number theory, String matching
- Solved
- No attempts yet
Problem
A positive integer is called a zigzag number when, reading its digits from left to right, the comparison between each pair of adjacent digits alternates between increasing and decreasing.
For example, is a zigzag number because its digits go , that is increase → decrease → increase. Likewise, is a zigzag number because it goes decrease → increase → decrease → increase. On the other hand, , , , and are not zigzag numbers. Every single-digit integer is considered a zigzag number.
Write a program that counts how many integers between and (inclusive) are multiples of and are also zigzag numbers.
Input
The first line contains , the second line contains , and the third line contains . (, )
Output
Print, modulo , the number of integers between and (inclusive) that are multiples of and are zigzag numbers.