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,x1,x2,y1,y2 a , b , c , x 1 , x 2 , y 1 , y 2 求满足方程 ax+by+c=0 a x + b y + c = 0 并且x在 [x1,x2] [ x 1 , x 2 ] 范围内,y在 [y1,y2] [ y 1 , y 2 ] 范围内的根有多少个。

分析:

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

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

ax+by=c a x + b y = − c

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

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

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

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

ax+by=cax=cbyx=cbya a x + b y = c ⟶ a x = c − b y ⟶ x = c − b y a

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

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

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

则通解为:

x=x0+k×bgcd x = x 0 + k × b g c d
y=y0k×agcd y = y 0 − k × a g c d

这样根据

x1xx2 x 1 ≤ x ≤ x 2
y1yy2 y 1 ≤ y ≤ y 2

求解不等式得到

x1xbgcdkx2xbgcd x 1 − x b g c d ≤ k ≤ x 2 − x b g c d

yy2agcdkyy1agcd 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值