关闭

SGU 106 The equation

标签: extcoo
567人阅读 评论(0) 收藏 举报

终于a了,这题还是比较麻烦的~wa的原因是没有考虑相除后是取它的上底还是下底。。。

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;

#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif

#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a)         for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a)        for( int i = (a)-1 ; i >= 0 ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define SZ(a)           ((int)a.size())
#define PP(n,m,a)       puts("---");FF(i,n){FF(j,m)cout << a[i][j] << ' ';puts("");}
#define pb              push_back
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-10;
const double pi = acos(-1.0);

i64 gcd(i64 _a, i64 _b)
{
    if (!_a || !_b)
    {
        return max(_a, _b);
    }
    i64 _t;
    while (_t = _a % _b)
    {
        _a = _b;
        _b = _t;
    }
    return _b;
};

i64 ext_gcd (i64 _a, i64 _b, i64 &_x, i64 &_y)
{
    if (!_b)
    {
        _x = 1;
        _y = 0;
        return _a;
    }
    i64 _d = ext_gcd (_b, _a % _b, _x, _y);
    i64 _t = _x;
    _x = _y;
    _y = _t - _a / _b * _y;
    return _d;
}

i64 exmod (i64 _a, i64 _p)
{
    i64 _ans, _y;
    ext_gcd (_a, _p, _ans, _y);
    _ans < 0 ? _ans += _p : 0;
    return _ans;
}

i64 a,b,c,x1,x2,y11,y22;
i64 gab;
i64 x,y;
i64 ans;


i64 findx(i64 q1,i64 q2,i64 w1,i64 w2)
{
    if(q1>q2)
    {
        swap(q1,q2);
    }
    if(w1>w2)
    {
        swap(w1,w2);
    }

    if(q1 > w2 || q2 < w1)
    {
        return 0;
    }
    else if(q1<=w1 && w2<=q2)
    {
        return w2-w1+1;
    }
    else if(q1>=w1 &&q1<=w2)
    {
        if(q2<=w2)
        {
            return q2-q1+1;
        }
        else
        {
            return w2 - q1+1;
        }
    }
    else if(q2>=w1 && q2 <= w2)
    {
        if(q1>=w1)
        {
            return q2 - q1 +1;
        }
        else
        {
            return q2 - w1 + 1;
        }
    }
}


int main()
{
    i64 temp;
    while(cin>>a>>b>>c)
    {
        cin>>x1>>x2;
        cin>>y11>>y22;
        if(a==0 && b==0)
        {
            if(c==0)
            {
                temp = (x2-x1+1)*(y22-y11+1);
                cout<<temp<<endl;
                continue;
            }
            else
            {
                cout<<"0"<<endl;
                continue;
            }
        }
        else if(a==0)
        {
            if(c%b==0)
            {
                temp = -c/b;
                if(temp<=y22 && temp >= y11)
                {
                    temp = x2-x1+1;
                    cout<<temp<<endl;
                    continue;
                }
            }
            else
            {
                cout<<"0"<<endl;
                continue;
            }
        }
        else if(b==0)
        {
            if(c%a==0)
            {
                temp = -c/a;
                if(temp <= x2 && temp >= x1)
                {
                    temp = y22 - y11 +1;
                    cout<<temp<<endl;
                    continue;
                }
            }
            else
            {
                cout<<"0"<<endl;
                continue;
            }
        }
        else
        {
            i64 t1,t2;
            if(c>0)
            {
                c =- c;
                a = -a;
                b = -b;
            }
            c = -c;
            if(a<0)
            {
                a = -a;
                x1=-x1;
                x2=-x2;
                if(x1>x2)
                swap(x1,x2);
            }
            if(b<0)
            {
                b=-b;
                y11=-y11;
                y22=-y22;
                if(y11>y22)
                swap(y11,y22);
            }
            gab = gcd(a,b);
            a /= gab;
            b /= gab;
            if(c%gab!=0)
            {
                cout<<"0"<<endl;
                continue;
            }

            x = exmod(a,b);
            x *= c;
            x %= b;
            y = (c-a*x)/b;
            i64 lix,rix;
            i64 liy,riy;
            lix = (x1-x)/b;
            if(x1-x > 0 && (x1-x)%b!=0 )
            {
                lix ++;
            }
            rix = (x2-x)/b;
            if(x2-x<0 && (x2-x)%b!=0)
            {
                rix--;
            }
            liy = (y-y22)/a;
            if( y - y22 >0 && (y-y22)%a!=0)
            {
                liy++;
            }
            riy = (y-y11)/a;
            if(y-y11<0 && (y-y11)%a != 0 )
            {
                riy--;
            }
            cout<<findx(lix,rix,liy,riy)<<endl;
        }
    }
    return 0;
}





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:331092次
    • 积分:6308
    • 等级:
    • 排名:第3878名
    • 原创:299篇
    • 转载:19篇
    • 译文:0篇
    • 评论:126条
    最新评论