#GL0000090. Khôi phục cấp số cộng bị mất (Lost Arithmetic Progression)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Khôi phục cấp số cộng bị mất (Lost Arithmetic Progression)
Lost Arithmetic Progression
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Two finite arithmetic progressions and with positive common differences were created, and consists of exactly the numbers common to both. Only and remain known. Progression has first term , difference , and terms; has first term , difference , and terms. Count the finite arithmetic progressions whose intersection with is exactly . Progressions differ if their first term, common difference, or length differs. Output 0 if none exist, -1 if infinitely many exist, or the finite count modulo .
Input
The first line contains describing . The second line contains describing .
Output
Print 0, -1, or the finite count modulo , as specified.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input
-9 3 11
0 6 3
Output
10
Explanation
and . A valid difference must satisfy , giving only or . For , there are choices at each end, producing progressions; for , there is . The total is .