#SP0000244. Đường đi giữa các số nguyên tố (Prime Path)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đường đi giữa các số nguyên tố (Prime Path)
Prime Path
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given two four-digit primes and , a move changes exactly one digit of the current number to a different digit. The resulting number must still be a four-digit prime (its thousands digit cannot become zero). Each move costs .
Find the minimum number of moves required to transform into . If no transformation is possible, print exactly Impossible. If , the answer is .
Input
A single line contains two integers , each a prime between and .
Output
Print an integer equal to the minimum number of digit changes. If no valid transformation exists, print exactly Impossible (case-sensitive).
Subtasks
-
Subtask 1 (20 points): ; both are four-digit primes; .
-
Subtask 2 (30 points): ; both are four-digit primes; and their decimal representations differ in at most two positions.
-
Subtask 3 (50 points): ; both are four-digit primes; the numbers of differing decimal digit positions may be , or .
Examples
Input
1033 8179
Output
6
Explanation
A valid transformation is . Each arrow replaces one digit, and every intermediate number is a four-digit prime. These six arrows give a cost of . Breadth-first search checks all shorter move counts first and confirms there is no valid path with fewer than six moves, so the output is 6.