Similar numbers

4.5

6 votes
Implementation, Algorithms, Basics of bit manipulation, Dynamic Programming, 2D dynamic programming
Problem

You are given a telephone number of length N. Assume that the two phone numbers are similar if the following conditions are met:

  1. They are different.
  2. The number of positions that contains different digits does not exceed K.
  3. The difference between the corresponding figures in the numbers does not exceed 1.

Your task is to find how many such numbers are similar to this phone number. Since the answer can be very large, print it modulo 109+7.

Input format

  • The first line indicates two space-separated numbers N,K.
  • The next line contains N digits.

Output format
Print one number denoting the answer modulo 109+7.

Constraints
1N104
1K100

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Next telephone numbers suit us: 001, 002, 003, 011, 013, 021, 022, 023, 102, 111, 112, 113, 122.

Editor Image

?