zoukankan      html  css  js  c++  java
  • SGU106 (扩展欧几里得)

    The equation

    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,当c%gcd(a,b)!=0的时候方程无解。
    当c%gcd(a,b)==0时,判断一下a,b为0的情况,
    当a和b都不为0时,设r=gcd(a,b),先用扩展欧几里得求出ax+by=r的一组特解(x0,y0),
    那么原方程的一组特解是(x0*c/r,y0*c/r),
    改写成ax0+by0=-c,
    设n为整数,则ax0+n*(a*b/r)+by0-n*(a*b/r)=-c,
    即a(x0+n*b/r)+b(y0-n*a/r)=-c,
    那么x=x0+n*b/r,y=y0-n*a/r
    由题目中x1<=x<=x2,y1<=y<=y2得,
    x1<=x0+n*b/r<=x2,
    y1<=y0-n*a/r<=y2,
    解这不等式组就可以了。
    扩展gcd的y=t-a/b*y;写成y=t-y*a/b
    WA了好多次=_=......

    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<iostream>
    using namespace std;
    long long exgcd(long long a,long long b,long long &x,long long &y)
    {
        if(b==0)
        {
            x=1;
            y=0;
            return a;
        }
        long long r=exgcd(b,a%b,x,y);
        long long t=x;
        x=y;
        y=t-a/b*y;//y=t-y*a/b是错的 
        return r;
    }
    int main()
    {
        long long a,b,c,x1,x2,y1,y2;
        cin>>a>>b>>c>>x1>>x2>>y1>>y2;
        long long x,y;
        long long r=exgcd(a,b,x,y);
        c=-c;
        if(a==0||b==0)
        {
            if(a==0&&b==0)
            {
                if(c==0)
                printf("%lld
    ",(x2-x1+1)*(y2-y1+1));
                else printf("0
    ");
            }
            else if(a==0)
            {
                if(c%b==0&&c/b>=y1&&c/b<=y2)
                printf("%lld
    ",x2-x1+1);
                else printf("0
    ");
            }
            else
            {
                if(c%a==0&&c/a>=x1&&c/a<=x2)
                printf("%lld
    ",y2-y1+1);
                else printf("0
    ");
            }
            return 0;
        }
        if(c%r)    printf("0
    "); 
        else
        {
            long long t=c/r;
            x*=t;y*=t;
            long long k=b/r;
            double L1,R1;
            if(k>0)
            {
                L1=(double)(x1-x)/(double)k;
                R1=(double)(x2-x)/(double)k;
            }
            else
            {
                L1=(double)(x2-x)/(double)k;
                R1=(double)(x1-x)/(double)k;
            }
            
            k=a/r;
            double L2,R2;
            if(k>0)
            {
                L2=(double)(y-y2)/(double)k;
                R2=(double)(y-y1)/(double)k;
            }
            else
            {
                L2=(double)(y-y1)/(double)k;
                R2=(double)(y-y2)/(double)k;
            }
            
            double L,R;
            L=max(L1,L2);
            R=min(R1,R2);
            
            long long ans=(long long)(floor(R+1e-6)-ceil(L-1e-6)+1);
            //printf("ans=%d
    ",ans);
            if(ans>0) printf("%lld
    ",ans);
            else printf("0
    ");
        }
        
        return 0;
    }
    View Code
















  • 相关阅读:
    HTML -- Note
    JavaScript使用childNodes和children
    Qt编写自定义控件69-代码行数统计
    Qt编写自定义控件68-IP地址输入框
    Qt编写自定义控件67-通用无边框
    Qt编写自定义控件66-光晕时钟
    Qt编写自定义控件65-光晕日历
    Qt编写自定义控件64-垂直时间轴
    Qt编写自定义控件63-水波效果
    Qt编写自定义控件62-探探雷达
  • 原文地址:https://www.cnblogs.com/ACRykl/p/8748213.html
Copyright © 2011-2022 走看看