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

来源:互联网 发布:淘宝如何登录店铺 编辑:程序博客网 时间:2024/05/22 07:51

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 -30 40 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
原创粉丝点击