The equation SGU - 106(扩展欧几里得+解不等式)

The equation SGU - 106

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

题意:

题意比较好懂,给定 a , b , c , x 1 , x 2 , y 1 , y 2 求满足方程 a x + b y + c = 0 并且x在 [ x 1 , x 2 ] 范围内,y在 [ y 1 , y 2 ] 范围内的根有多少个。

分析:

这道题关键部分就是解线性方程,但是解不等式部分需要注意细节

我们要解方程需要将原式变成

a x + b y = c

所以首先c=-c
但是题目中的a,b,c均有可能是负数,为了稍后解不等式的方便(不在分类讨论变号问题了)

正数不用管,下面我们只讨论都是负数的情况该怎么操作:

首先c是负数,c = -c,此时为了保证等号,a,b也要成相反符号a=-a,b=-b

其次判断a是负数,则a=-a此时因为

a x + b y = c a x = c b y x = c b y a

a的正负改变了x的正负,那么区间也要相应的改变,因为乘负号不等号方向改变,因此区间要变成 [ x 2 , x 1 ] .

再其次判断b是负数,处理方式和a相同

这样根据扩展欧几里得我们求得一个特解 x 0 , y 0

则通解为:

x = x 0 + k × b g c d
y = y 0 k × a g c d

这样根据

x 1 x x 2
y 1 y y 2

求解不等式得到

x 1 x b g c d k x 2 x b g c d

y y 2 a g c d k y y 1 a g c d

注意左边向上取整,即如果有小数部分,去掉小数整数部分+1
右边向下取整,即如果有小数部分,去掉小数保留整数部分

最后求交集即可

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
ll ex_gcd(ll a,ll b,ll &x,ll &y){
    if(!b){
        x = 1;
        y = 0;
        return a;
    }
    ll gcd = ex_gcd(b,a%b,y,x);
    y -= a / b * x;
    return gcd;
}
int main(){
    ll a,b,c,x1,x2,y1,y2;
    ll g,x,y;
    cin >> a >> b >> c >> x1 >> x2 >> y1 >> y2;
    c = -c;//原式ax+by+c=0把c移到等号右边
    if(c < 0){//把c变成正的,如果c变了,ab也要变号
        c = -c;
        a = -a;
        b = -b;
    }
    if(a < 0){
        a = -a;
        swap(x1,x2);
        x1 = -x1;
        x2 = -x2;
    }
    if(b < 0){
        b = -b;
        swap(y1,y2);
        y1 = -y1;
        y2 = -y2;
    }
    if(a == 0 || b == 0){
        if(a == 0 && b == 0){
            if(c == 0)
                cout << (x2 - x1 + 1) * (y2 - y1 + 1) << endl;
            else
                cout << "0" << endl;
        }
        else if(a == 0){
            if(c % b == 0 && c / b >= y1 && c / b <= y2)
                cout << x2 - x1 + 1 << endl;
            else
                cout << "0" << endl;
        }
        else{
            if(c % a == 0 && c / a >= x1 && c / a <= x2)
                cout << y2 - y1 + 1 << endl;
            else
                cout << "0" << endl;
        }
        return 0;
    }
    g = ex_gcd(a,b,x,y);
    if(c % g){
        cout << "0" << endl;
        return 0;
    }
    ll tmp = c / g;
    double adx = b / g,ady = a / g;
    x *= tmp;
    y *= tmp;
    ll l = max(ceil((x1-x)/adx),ceil((y-y2)/ady));
    ll r = min(floor((x2-x)/adx),floor((y-y1)/ady));
    if(r >= l)
        cout << r - l + 1 << endl;
    else
        cout << "0" << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/81123239
sgu