【codevs1213】解的个数

这个题可以很快的看出是扩展gcd,但是……细节贼多……

gcd推出式子的过程写在代码里了23333

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
long long a,b,c,p,q,r,s,n,x,y,g,ans,t;
void exgcd(long long a,long long b,long long &x,long long &y)
{
    if(b==0)
    {
        x=1,y=0,g=a;return;
    }
    exgcd(b,a%b,x,y);
    long long x1=x,y1=y;
    x=y;
    y=x1-a/b*y1;
}
/*
ax1+by1=c
bx2+(a%b)y2=c
bx2+ay2-a/b*b*y2=c
ay2+b(x2-a/b*y2)=c
x1=y2 y1=x2-a/b*y2 
*/ 
int main()
{
    scanf("%lld",&n);
    while(n--)
    {
        x=y=0;ans=0;
        g=1;
        scanf("%lld%lld%lld%lld%lld%lld%lld",&a,&b,&c,&p,&q,&r,&s);
        if(a==0&&b==0&&c==0)//特判 
        {
            if(q>=p&&s>=r)
                ans=(q-p+1)*(s-r+1);
            printf("%lld\n",ans);continue;
        }
        if(a==0&&b==0&&c!=0)
        {
            printf("0\n");continue;
        }
        c=-c; 
        if(c%g)
        {
            printf("0\n");continue;
        }
        exgcd(a,b,x,y);//先求ax+by=gcd(x,y)的解 
        x*=(c/g),y*=(c/g),a/=g,b/=g;
        ans=0;
        for(int i=-1000000;i<=1000000;i++)//枚举倍数的范围 
        {
            if(x+i*b<p||x+i*b>q||y-i*a<r||y-i*a>s)
                continue;
            if(a*g*(x+i*b)+b*g*(y-i*a)==c)
                ans++;
        }
        printf("%lld\n",ans);
    }
}

 

posted @ 2017-10-26 18:35  那一抹落日的橙  阅读(153)  评论(0编辑  收藏  举报