<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

106. The equation

time limit per test: 0.25 sec.
memory limit per test: 4096 KB


There is an equation ax + by + c = 0. Given a,b,c,x1,x2,y1,y2 you must determine, how many integer roots of this equation are satisfy to the following conditions : x1<=x<=x2,   y1<=y<=y2. Integer root of this equation is a pair of integer numbers (x,y).


Input

Input contains integer numbers a,b,c,x1,x2,y1,y2 delimited by spaces and line breaks. All numbers are not greater than 108 by absolute value.


Output

Write answer to the output.


Sample Input

1 1 -3
0 4
0 4
Sample Output

4

<|response|>
1. Abridged Problem Statement  
Given integers a, b, c and two intervals [x1,x2] and [y1,y2], count the number of integer pairs (x,y) satisfying  
  a·x + b·y + c = 0  
and  
  x1 ≤ x ≤ x2, y1 ≤ y ≤ y2.

2. Key Observations  
• Special cases when a=0 and/or b=0 can be handled directly:  
  – If a=b=0, the equation is c=0; if c=0 every point in the rectangle is a solution, otherwise none.  
  – If a=0 but b≠0, solve b·y = –c ⇒ y fixed (if divisible), x free in [x1,x2]. Similarly for b=0.  
• If a≠0 and b≠0, the Diophantine equation a·x + b·y = –c has an integer solution iff gcd(a,b) divides –c.  
• By the extended Euclidean algorithm one finds one particular solution (x0,y0) to a·x + b·y = –c.  
• The general integer solution is  
  x = x0 + (b/g)·t,  
  y = y0 – (a/g)·t,  
  where g = gcd(a,b) and t ∈ ℤ.  
• Imposing x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2 gives two intervals for t. Their intersection (if any) has length equal to the number of valid integer t’s, hence the answer.

3. Full Solution Approach  
Step 1. Handle the trivial cases:  
 – If a=0 and b=0:  
    • If c=0, answer = (x2–x1+1)·(y2–y1+1).  
    • Else answer = 0.  
 – Else if a=0: solve b·y = –c. If –c divisible by b, y is fixed; count x in [x1,x2], else 0.  
 – Else if b=0: similarly.  
Step 2. Now assume a≠0 and b≠0. Compute g = gcd(a,b). If g ∤ (–c), answer = 0.  
Step 3. Use the extended Euclidean algorithm to find one solution (x0,y0) to a·x + b·y = –c.  
Step 4. The full family of solutions is  
 x = x0 + (b/g)·t,  
 y = y0 – (a/g)·t.  
Let dx = b/g, dy = –a/g.  
Step 5. For x in [x1,x2]:  
 x1 ≤ x0 + dx·t ≤ x2  
⇒ t ≥ ceil((x1 – x0)/dx) and t ≤ floor((x2 – x0)/dx)   (if dx>0, swap inequalities if dx<0)  
Similarly for y in [y1,y2]:  
 t ≥ ceil((y0 – y2)/(a/g)) and t ≤ floor((y0 – y1)/(a/g))  (equivalently using dy).  
Compute the intersection [t_low, t_high] of these two intervals.  
Step 6. If t_low ≤ t_high, answer = t_high – t_low + 1; otherwise 0.  

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

// Return (g, x, y) such that a*x + b*y = g = gcd(a,b)
tuple<ll,ll,ll> ext_gcd(ll a, ll b) {
    if (b==0) return {a, 1, 0};
    ll g, x1, y1;
    tie(g, x1, y1) = ext_gcd(b, a % b);
    // back-substitute
    ll x = y1;
    ll y = x1 - (a / b) * y1;
    return {g, x, y};
}

// Floor division: floor(a/b), works for negative a or b
ll floor_div(ll a, ll b) {
    ll q = a / b;
    ll r = a % b;
    // if remainder non-zero and signs of r and b differ, subtract 1
    if (r != 0 && ((r > 0) != (b > 0))) --q;
    return q;
}
// Ceil division: ceil(a/b)
ll ceil_div(ll a, ll b) {
    ll q = a / b;
    ll r = a % b;
    // if remainder non-zero and signs of r and b same, add 1
    if (r != 0 && ((r > 0) == (b > 0))) ++q;
    return q;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll a,b,c;
    ll x1,x2,y1,y2;
    cin >> a >> b >> c;
    cin >> x1 >> x2 >> y1 >> y2;

    // Move constant: a*x + b*y = -c
    c = -c;

    // Case a=b=0
    if (a==0 && b==0) {
        if (c==0) {
            cout << (x2 - x1 + 1) * (y2 - y1 + 1) << "\n";
        } else {
            cout << 0 << "\n";
        }
        return 0;
    }

    // Case a=0 => b*y = c
    if (a==0) {
        if (c % b != 0) {
            cout << 0 << "\n";
        } else {
            ll y = c / b;
            if (y1 <= y && y <= y2) {
                cout << (x2 - x1 + 1) << "\n";
            } else {
                cout << 0 << "\n";
            }
        }
        return 0;
    }
    // Case b=0 => a*x = c
    if (b==0) {
        if (c % a != 0) {
            cout << 0 << "\n";
        } else {
            ll x = c / a;
            if (x1 <= x && x <= x2) {
                cout << (y2 - y1 + 1) << "\n";
            } else {
                cout << 0 << "\n";
            }
        }
        return 0;
    }

    // General case
    // 1) compute gcd and one solution to a*x + b*y = g
    ll g, x0, y0;
    tie(g, x0, y0) = ext_gcd(llabs(a), llabs(b));
    if (a < 0) x0 = -x0;
    if (b < 0) y0 = -y0;

    // 2) check divisibility
    if (c % g != 0) {
        cout << 0 << "\n";
        return 0;
    }

    // 3) scale base solution to a*x + b*y = c
    ll factor = c / g;
    x0 *= factor;
    y0 *= factor;

    // 4) parameterize general solution
    ll dx =  b / g;  // x = x0 + dx * t
    ll dy = -a / g;  // y = y0 + dy * t

    // 5) compute t-range for x constraints
    ll tx_min = ceil_div(x1 - x0, dx);
    ll tx_max = floor_div(x2 - x0, dx);
    // and for y constraints
    ll ty_min = ceil_div(y1 - y0, dy);
    ll ty_max = floor_div(y2 - y0, dy);

    // 6) intersect the intervals [tx_min,tx_max] and [ty_min,ty_max]
    ll t_low  = max(tx_min, ty_min);
    ll t_high = min(tx_max, ty_max);

    ll ans = (t_low > t_high ? 0 : t_high - t_low + 1);
    cout << ans << "\n";

    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys
sys.setrecursionlimit(10**7)

# Extended GCD: returns (g, x, y) with a*x + b*y = g
def ext_gcd(a, b):
    if b == 0:
        return (a, 1, 0)
    g, x1, y1 = ext_gcd(b, a % b)
    x = y1
    y = x1 - (a // b) * y1
    return (g, x, y)

# floor division: floor(a/b)
def floor_div(a, b):
    q, r = divmod(a, b)
    if r != 0 and ((r > 0) != (b > 0)):
        q -= 1
    return q

# ceil division: ceil(a/b)
def ceil_div(a, b):
    q, r = divmod(a, b)
    if r != 0 and ((r > 0) == (b > 0)):
        q += 1
    return q

def main():
    data = list(map(int, sys.stdin.read().split()))
    a, b, c = data[0], data[1], data[2]
    x1, x2, y1, y2 = data[3], data[4], data[5], data[6]

    # Move constant term: a*x + b*y = -c
    c = -c

    # Case a=b=0
    if a == 0 and b == 0:
        if c == 0:
            print((x2 - x1 + 1) * (y2 - y1 + 1))
        else:
            print(0)
        return

    # Case a=0 => b*y = c
    if a == 0:
        if c % b != 0:
            print(0)
        else:
            y = c // b
            print(x2 - x1 + 1 if y1 <= y <= y2 else 0)
        return

    # Case b=0 => a*x = c
    if b == 0:
        if c % a != 0:
            print(0)
        else:
            x = c // a
            print(y2 - y1 + 1 if x1 <= x <= x2 else 0)
        return

    # General case
    # 1) Find g, and a solution to a*x + b*y = g
    g, x0, y0 = ext_gcd(abs(a), abs(b))
    if a < 0: x0 = -x0
    if b < 0: y0 = -y0

    # 2) Check if g divides c
    if c % g != 0:
        print(0)
        return

    # 3) Scale solution to match a*x + b*y = c
    factor = c // g
    x0 *= factor
    y0 *= factor

    # 4) Parameterize: x = x0 + (b/g)*t, y = y0 - (a/g)*t
    dx = b // g
    dy = -a // g

    # 5) Derive t-intervals from bounds
    tx_min = ceil_div(x1 - x0, dx)
    tx_max = floor_div(x2 - x0, dx)
    ty_min = ceil_div(y1 - y0, dy)
    ty_max = floor_div(y2 - y0, dy)

    # 6) Intersect intervals
    t_low = max(tx_min, ty_min)
    t_high = min(tx_max, ty_max)

    print(max(0, t_high - t_low + 1))

if __name__ == "__main__":
    main()
```