Zigzag Numbers

Time limit2sMemory limit128 MB

Summary
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, 29472947 is a zigzag number because its digits go 2→9→4→72 \to 9 \to 4 \to 7, that is increase → decrease → increase. Likewise, 7194671946 is a zigzag number because it goes decrease → increase → decrease → increase. On the other hand, 123123, 7144671446, 7144271442, and 8888 are not zigzag numbers. Every single-digit integer is considered a zigzag number.

Write a program that counts how many integers between AA and BB (inclusive) are multiples of MM and are also zigzag numbers.

Input

The first line contains AA, the second line contains BB, and the third line contains MM. (1≤A≤B≤105001 \le A \le B \le 10^{500}, 1≤M≤5001 \le M \le 500)

Output

Print, modulo 1000010000, the number of integers between AA and BB (inclusive) that are multiples of MM and are zigzag numbers.

Examples2

  1. Example 1

    Input
    100
    200
    5
    
    Expected output
    13
    
  2. Example 2

    Input
    6
    1234567
    3
    
    Expected output
    246