#P5440. Miracle Date

    ID: 18672 Type: Default 1000ms 256MiB

Miracle Date

Miracle Date

We call an 8‐digit string a date if the first 4 digits represent the year, the next 2 digits represent the month, and the last 2 digits represent the day (padded with leading zeros if necessary). In addition, the date must represent a real calendar date, and the year is in the range \(1\sim9999\).

A date that is destined to witness a miracle has the following property: when we interpret the 2-digit day, the 4-digit month+day, and the 8-digit full date as numbers, each one is a prime number (in \(\LaTeX\) format, a prime is defined as a positive integer greater than 1 having no positive divisors other than 1 and itself). Note that even if a date meets these numerical conditions, it does not necessarily mean a miracle will occur.

You are given an 8-character pattern representing a possibly incomplete date. Some of the eight digits may be unknown and are denoted by the character '?' (question mark). Your task is to count how many possible dates (i.e. assignments of digits to '?') satisfy all of the following conditions:

  1. The completed 8-digit string represents a valid date (with year in 1 to 9999, month between 1 and 12 and day valid for the given month considering leap years).
  2. The 2-digit day forms a prime number.
  3. The 4-digit number formed by concatenating the month and day (each padded to 2 digits) is prime.
  4. The full 8-digit number (year+month+day, with year padded to 4 digits) is prime.

Output the count of all possible dates that match the given pattern and satisfy the above conditions.

Note: A date is considered valid if it exists in the Gregorian calendar; the leap year rule applies (a year is a leap year if it is divisible by 400, or divisible by 4 but not by 100).

inputFormat

The input consists of a single line containing an 8-character string. Each character is either a digit ('0'-'9') or a '?' (question mark) indicating an unknown digit.

For example:

00010103

outputFormat

Output a single integer representing the number of possible dates that match the pattern and satisfy the miracle conditions.

sample

00010103
1