SGU 106 扩展欧几里得
题意:求有多少对(x,y)满足x在x1~x2,y在y1~y2里使方程ax+by+c=0
方法:c取相反数,然后扩欧解得x,y,再x*=c/d,y*=c/d求得一组解,计算x、y的改变量,x=b/d,y=a/d*-1。根据 x1<=x+k*dx<=x2,解得x1-x<=kdx<=x2-x,判断dx的正负再两边分别除以dx,左边向上取整,右边向下取整,对y进行一样的操作,最后取x和y最大的左边界和最小的右边界。
注意:
1️⃣若d=0,即c=0。如果a=b=0,则输出(x2-x1+1)*(y2-y1+1),否则输出0
2️⃣若c%d!=0,则输出0
3️⃣若lx>ly||rx>ry||lp>rp,则输出0
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define fo freopen("in.txt","r",stdin)
#define fc fclose(stdin)
#define fu0(i,n) for(i=0;i<n;i++)
#define fu1(i,n) for(i=1;i<=n;i++)
#define fd0(i,n) for(i=n-1;i>=0;i--)
#define fd1(i,n) for(i=n;i>0;i--)
#define mst(a,b) memset(a,b,sizeof(a))
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d %d",&n,&m)
#define ss(s) scanf("%s",s)
#define sddd(n,m,k) scanf("%d %d %d",&n,&m,&k)
#define pans() printf("%d\n",ans)
#define all(a) a.begin(),a.end()
#define sc(c) scanf("%c",&c)
#define we(a) while(scanf("%d",&a)!=EOF)
const int maxn=200005;
const double eps=1e-8;
void gcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(!b)
{
d=a;x=1;y=0;
}
else
{
gcd(b,a%b,d,y,x);y-=(a/b)*x;
}
}
int main()
{
ll a,b,c,x1,x2,y1,y2;
cin>>a>>b>>c;
cin>>x1>>x2>>y1>>y2;
c*=-1;
ll d,x,y;
gcd(a,b,d,x,y);
if(d==0)
{
if(c==0)
{
cout<<(x2-x1+1)*(y2-y1+1)<<endl;
}
else
{
cout<<0<<endl;
}
return 0;
}
if(c%d)
{
cout<<0<<endl;
return 0;
}
x=x*c/d;
y=y*c/d;
ll dx=b/d,dy=a/d*(-1),lx,ly,rx,ry;
x1-=x,x2-=x,y1-=y,y2-=y;
if(dx>0)
{
lx=ceil(x1*1.0/dx);
rx=floor(x2*1.0/dx);
}
else
{
lx=ceil(x2*1.0/dx);
rx=floor(x1*1.0/dx);
}
if(dy>0)
{
ly=ceil(y1*1.0/dy);
ry=floor(y2*1.0/dy);
}
else
{
ry=floor(y1*1.0/dy);
ly=ceil(y2*1.0/dy);
}
ll lp=max(lx,ly),rp=min(rx,ry);
if(rx<lx||ry<ly||lp>rp)
{
cout<<0<<endl;
}
else
{
cout<<rp-lp+1<<endl;
}
return 0;
}