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

1306 - Solutions to an Equation
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)。

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


如何求解其他解呢?



#include<iostream>
#include<cmath>
#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<<cnt<<endl;
            continue;
        }
        else if(a == 0 && b != 0)
        {
            if(c % b == 0){
                ll temp = -c / b;
                if(temp >= y1 && temp <= y2){
                    cnt = x2 - x1 + 1;
                }
            }
            cout<<cnt<<endl;
            continue;
        }
        else if(a != 0 && b == 0)
        {
            if(c % a == 0){
                ll temp = -c / a;
                if(temp >= x1 && temp <= x2)cnt = y2 - y1 + 1;
            }
            cout<<cnt<<endl;
            continue;
        }
        //求k的取值范围
        if(c % d == 0){
            x = x * (-c / d);
            y = y * (-c / d);
            ll t_x1, t_x2, t_y1, t_y2;
            if(sign(d, b) == -1){
                t_x1 = ceil((x2 - x) * 1.0 * d / b);
                t_x2 = floor((x1 - x) * 1.0 * d/ b);
            }
            else{
                t_x1 = ceil((x1 - x) * 1.0 * d / b);
                t_x2 = floor((x2 - x) * 1.0 * d / b);
            }
            if(sign(d,a) == -1){
                t_y1 = ceil((y - y1) * 1.0 * d / a);
                t_y2 = floor((y - y2) * 1.0 * d / a);
            }
            else{
                t_y1 = ceil((y - y2) * 1.0 * d / a);
                t_y2 = floor((y - y1) * 1.0 * d / a);
            }
            ll l = max(t_x1, t_y1);
            ll r = min(t_x2, t_y2);
            if(l > r){
                    cout<<0<<endl;
                    continue;
            }
            cnt = r - l + 1;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

补充一点:

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

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值