#P4882. lty's Favorite Number

    ID: 18123 Type: Default 1000ms 256MiB

lty's Favorite Number

lty's Favorite Number

lty has a special liking for numbers that "contain" the digits 9 and 6 not in the ordinary sense, but with the following strict properties (all three conditions must be met):

  1. The number is an N-digit number without any leading zeros.
  2. The number contains at least M occurrences in total of the digits 9 and 6. For example, the number 986996 contains three 9's and two 6's, totalling 5 occurrences.
  3. There exists at least one sequence of three consecutive digits (denoted as A, B, C) in the number that satisfies at least one of the following conditions:
    • The sum of the three digits equals 9 or 6: $$ A+B+C=9 \quad \text{or} \quad A+B+C=6$$
    • If \(C \neq 0\), then the remainder when \(A^2+B^2\) is divided by \(C\) equals 9 or 6: $$ (A^2+B^2) \bmod C = 9 \quad \text{or} \quad (A^2+B^2) \bmod C = 6 $$

You are given two integers N and M and a number. Determine whether the number is one of lty's favorites by checking if it meets all the above conditions. Print YES if it does, and NO otherwise.

inputFormat

The input consists of two lines:

  1. The first line contains two space-separated integers: N and M, where N is the number of digits and M is the minimum total count of digits 9 and 6 required.
  2. The second line contains the number (as a string of digits) to be checked.

outputFormat

Output a single line containing YES if the number satisfies all three conditions, or NO otherwise.

sample

5 2
91236
YES