#GL0000288. Hai cấp số cộng (Two Arithmetic Progressions)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Hai cấp số cộng (Two Arithmetic Progressions)
Two Arithmetic Progressions
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given two infinite arithmetic progressions, whose terms are and , where are nonnegative integers. Both common differences are positive, while the initial terms may be negative.
Given an integer interval , count the distinct integers in this interval which appear in both progressions. Count each integer once.
Input
A single line contains six integers in this order: .
Output
Print a single integer: the number of qualifying values .
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input
2 0 3 3 5 21
Output
3
Explanation
The common values in the interval are 6, 12, and 18. For 6 the progression indices are 3 and 1; for 12 they are 6 and 3; for 18 they are 9 and 5. Exactly three distinct values qualify.
Input
2 4 3 0 6 17
Output
2
Explanation
The common values between 6 and 17 are 6 and 12. For 6 the indices are 1 and 2, and for 12 they are 4 and 4. No other number in the given interval belongs to both progressions.