SGU 106. The equation 解题报告(模线性方程)

96 篇文章 0 订阅
6 篇文章 0 订阅

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

    解题报告:简单的题意。方程ax+by+c=0在矩形((x1, y1), (x2, y2))中有多少个整点。

    首先,应该考虑特殊情况,a=0,或者b=0,或者同时等于0。剩下的情况必定是一条有斜率K的直线了。

    我的做法是在每个限制(x1, x2, y1, y2)中找最近的符合条件的点,按x排序,最大减最小,除以x的点距即可。

    做法不难,但是WA了很多次,都是一些细节问题。如果真的是比赛,是要吃大亏的……码力仍需加强。

    代码如下:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

typedef long long LL;

// 扩展欧几里得
void exgcd(LL a, LL b, LL &d, LL &x, LL &y)
{
    if(b==0)
        d=a, x=1, y=0;
    else
        exgcd(b, a%b, d, y, x), y-=x*(a/b);
}

// 解线性方程ax+by=c,返回最小的非负x解
LL linear_mod(LL a, LL b, LL c)
{
    LL d, x, y;
    exgcd(a, b, d, x, y);   // 解ax+by=d, d为a,b最大公约数

    if(c%d) return -1;      // 如果d不能整除c,方程无解

    a/=d, b/=d, c/=d;       // 都除以最大公约数

    if(b<0) b=-b;           // 取正b,如果使用y则y也要换号,此处忽略y

    return (x*c%b+b)%b; // 该方程最小非负x解
}

LL limit_linear_mod(LL a, LL b, LL c, LL limit)
{
    return linear_mod(a, b, c-a*limit)+limit;
}

LL flip_limit_linear_mod(LL a, LL b, LL c, LL limit)
{
    return -limit_linear_mod(a, b, -c, -limit);
}

inline bool check(int x, int sta, int end)
{
    return sta<=x && x<=end;
}

inline LL absLL(LL a)
{
    return a<0?-a:a;
}

struct Point
{
    LL x, y;

    bool operator<(const Point& cmp) const
    {
        return x<cmp.x;
    }
} point[10];
int pointNum;

LL xx1, xx2, yy1, yy2;

void addPoint(LL x, LL y)
{
    if(check(x, xx1, xx2) && check(y, yy1, yy2))
        point[pointNum].x=x, point[pointNum].y=y, pointNum++;
}

LL gcd(LL a, LL b)
{
    return b==0?a:gcd(b, a%b);
}

int main()
{
    LL a, b, c;

    while(~scanf("%lld%lld%lld", &a, &b, &c))
    {
        scanf("%lld%lld%lld%lld", &xx1, &xx2, &yy1, &yy2);

        c=-c;

        LL ans=0;

        if(a==0 && b==0)
        {
            if(c==0)
                ans=(xx2-xx1+1)*(yy2-yy1+1);
            printf("%lld\n", ans);
            continue;
        }

        if(b==0)
        {
            swap(a, b);
            swap(xx1, yy1);
            swap(xx2, yy2);
        }

        if(a==0)
        {
            if(c%b==0 && check(c/b, yy1, yy2))
                ans=(xx2-xx1+1);
        }
        else
        {
            LL tmpx, tmpy;
            pointNum=0;

            tmpx = limit_linear_mod(a, b, c, xx1);
            tmpy = (c-a*tmpx)/b;
            addPoint(tmpx, tmpy);

            tmpy = limit_linear_mod(b, a, c, yy1);
            tmpx = (c-b*tmpy)/a;
            addPoint(tmpx, tmpy);

            tmpx = flip_limit_linear_mod(a, b, c, xx2);
            tmpy = (c-a*tmpx)/b;
            addPoint(tmpx, tmpy);

            tmpy = flip_limit_linear_mod(b, a, c, yy2);
            tmpx = (c-b*tmpy)/a;
            addPoint(tmpx, tmpy);

            if(pointNum)
            {
                sort(point, point+pointNum);
                ans=(absLL(point[pointNum-1].x-point[0].x)/absLL(b/gcd(a, b))+1);
            }
        }

        printf("%lld\n", ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值