LightOJ 1306 Solutions to an Equation(扩展欧几里德算法的应用)

1306 - Solutions to an Equation
    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

You have to find the number of solutions of the following equation:

Ax + By + C = 0

Where A, B, C, x, y are integers and x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case starts with a line containing seven integers A, B, C, x1, x2, y1, y2 (x1 ≤ x2, y1 ≤ y2). The value of each integer will lie in the range [-108, 108].

Output

For each case, print the case number and the total number of solutions.

Sample Input

Output for Sample Input

5

1 1 -5 -5 10 2 4

-10 -8 80 -100 100 -90 90

2 3 -4 1 7 0 8

-2 -3 6 -2 5 -10 5

1 8 -32 0 0 1 10

Case 1: 3

Case 2: 37

Case 3: 1

Case 4: 2

Case 5: 1

 


PROBLEM SETTER: JANE ALAM JAN

ps: 

扩展欧几里德算法:找出一对整数(x, y), 使得 ax + by = gcd(a, b)。

void ex_gcd(int a, int b, int &d, int &x, int &y)
{//d为a与b的最大公约数
    if(!b){
        d = a; x = 1; y = 0;
    }
    else{
        ex_gcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}

注意在递归调用时,x和y的顺序改变,递归出口: 当b = 0时,gcd(a,b) = a, 此时x = 1, y为任何数,

因此,gcd(a, 0) = 1 * a - 0 * 0 = a。需要记忆的是 y -= x * (a / b)。

扩展欧几里德算法证明过程:

LightOJ 1306 Solutions to an Equation(扩展欧几里德算法的应用)_第1张图片

如何求解其他解呢?

LightOJ 1306 Solutions to an Equation(扩展欧几里德算法的应用)_第2张图片

LightOJ 1306 Solutions to an Equation(扩展欧几里德算法的应用)_第3张图片

#include
#include
#define ll long long
using namespace std;
void ex_gcd(ll a, ll b, ll &d, ll &x, ll &y)
{//扩展欧几里得算法
    if (!b)
        {d = a; x = 1; y = 0;}

    else
    {
        ex_gcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
int sign(ll d, ll ab)
{//判断gcd(a,b)*a||b的符号
    if(d * ab < 0)return -1;
    else return 1;
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    for (int i = 1; i <= t; i++)
    {
        ll a, b, c, x1, x2, y1, y2;
        cin >> a >> b >> c >> x1 >> x2 >> y1 >> y2;
        ll x, y, d;
        ll cnt = 0;
        cout << "Case " << i << ": ";
        ex_gcd(a, b, d, x, y);
        //特殊点的处理
        if(a == 0 && b == 0)
        {
            if(c == 0) cnt = (x2 - x1 + 1) * (y2 - y1 + 1);
            cout<= y1 && temp <= y2){
                    cnt = x2 - x1 + 1;
                }
            }
            cout<= x1 && temp <= x2)cnt = y2 - y1 + 1;
            }
            cout< r){
                    cout<<0<

补充一点:

floor()是向负无穷大舍入,floor(-9.3) == -10;floor(x)返回的是小于或等于x的最大整数。

ceil()是向正无穷大舍入,ceil(-9.4) == -9;ceil(x)返回的是大于x的最小整数


你可能感兴趣的:(扩展欧几里德算法证明,扩展欧几里德算法应用,不等式,取整,数学分析,lrj-紫书,LightOJ)