PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GL0000337. Chuỗi cặp số may mắn (Lucky Chains)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Chuỗi cặp số may mắn (Lucky Chains)
Lucky Chains
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A pair of positive integers is lucky if .
Consider . The lucky-chain length is the number of consecutive lucky pairs from the beginning, ending immediately before the first pair whose GCD is greater than .
Output 0 when the initial pair is already not lucky, and -1 if every pair in the infinite sequence is lucky.
You are given pairs with ; their difference stays constant as both numbers increase.
Input
The first line contains , followed by lines containing pairs .
Output
Print lines: the lucky-chain length for each pair, or -1 if it is infinite.
Subtasks
-
Subtask 1 (20 points): , .
-
Subtask 2 (30 points): , .
-
Subtask 3 (50 points): , .
Examples
Example 1
Input
4
5 15
13 37
8 9
10009 20000
Output
0
1
-1
79
Explanation
- The GCD of is already , so no increment is needed.
- One increment changes to , whose GCD is .
- The difference in stays , so no common divisor greater than can appear.
- The last pair has difference . Its first value, , leaves remainder modulo both primes. The first multiple of is increments away and the first multiple of is increments away. Thus the answer is .