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
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
扩展欧几里德,给出解的范围。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<map> #include<vector> #include<queue> #include<stack> #include<set> #include<cmath> #define ll long long #define mem(a,b) memset(a,b,sizeof(a)) #define inf 0x3f3f3f3f3f3f #define maxn 17000 #define root 1,n,1 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define mod 10007 using namespace std; ll gcd(ll a,ll b){ return b==0?a : gcd(b,a%b); } ll exgcd(ll a,ll b,ll &x,ll &y){ if(b==0){ x=1;y=0;return a; } ll r=exgcd(b,a%b,x,y); ll t=x;x=y; y=t-a/b*y; return r; } bool linear(ll a,ll b,ll c,ll &x,ll &y){ ll d=exgcd(a,b,x,y); if(c%d)return false; ll k=c/d; x*=k;y*=k; return true; } int main(){ ll a,b,c; while(~scanf("%lld%lld%lld",&a,&b,&c)){ ll x1,x2,y1,y2; ll ans=0; ll x=gcd(a,b); scanf("%lld%lld%lld%lld",&x1,&x2,&y1,&y2); if(!a&&!b){ if(!c){printf("%lld\n",(x2-x1+1)*(y2-y1+1));continue;} } else if(!a&&b){ if((-c%b)==0){ ll tmp=(-c)/b; if(tmp<=y2&&tmp>=y1){printf("1\n");continue;} } } else if(!b&&a){ if((-c%a)==0){ ll tmp=(-c)/a; if(tmp>=x1&&tmp<=x2){printf("1\n");continue;} } } else{ ll x0,y0; bool k=linear(a,b,-c,x0,y0); if(!k){printf("0\n");continue;} double t1,t2,t3,t4; t1=(x1-x0)*x*1.0/b; t2=(x2-x0)*x*1.0/b; t3=(y0-y1)*x*1.0/a; t4=(y0-y2)*x*1.0/a; if(t2>t1)swap(t1,t2); if(t4>t3)swap(t3,t4); ll l=t2,r=t1; if(l<t2)l++;if(r>t1)r--; ll l1=t4,r1=t3; if(l1<t4)l1++;if(r1>t3)r1--; l=max(l,l1); r=min(r,r1); if(r>=l) printf("%lld\n",r-l+1); else printf("0\n"); continue; } puts("0"); } }