PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000090. Khôi phục cấp số cộng bị mất (Lost Arithmetic Progression)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 AA and BB with positive common differences were created, and CC consists of exactly the numbers common to both. Only BB and CC remain known. Progression BB has first term bb, difference qq, and yy terms; CC has first term cc, difference rr, and zz terms. Count the finite arithmetic progressions AA whose intersection with BB is exactly CC. 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 109+710^9+7.

Input

The first line contains b,q,yb,q,y describing BB. The second line contains c,r,zc,r,z describing CC.

Output

Print 0, -1, or the finite count modulo 109+710^9+7, as specified.

Subtasks

  • Subtask 1 (20 points): ∣b∣,∣c∣≤100|b|,|c|\le100; 1≤q,r≤201\le q,r\le20; 2≤y,z≤202\le y,z\le20.
  • Subtask 2 (30 points): ∣b∣,∣c∣≤105|b|,|c|\le10^5; 1≤q,r≤1051\le q,r\le10^5; 2≤y,z≤1052\le y,z\le10^5.
  • Subtask 3 (50 points): −109≤b,c≤109-10^9\le b,c\le10^9; 1≤q,r≤1091\le q,r\le10^9; 2≤y,z≤1092\le y,z\le10^9.

Examples

Input

-9 3 11
0 6 3

Output

10

Explanation

B={−9,−6,−3,0,3,6,9,12,15,18,21}B=\{-9,-6,-3,0,3,6,9,12,15,18,21\} and C={0,6,12}C=\{0,6,12\}. A valid difference dd must satisfy lcm⁡(d,3)=6\operatorname{lcm}(d,3)=6, giving only d=2d=2 or d=6d=6. For d=2d=2, there are 33 choices at each end, producing 99 progressions; for d=6d=6, there is 11. The total is 1010.