Description
Input
输入有多组测例,每组测例有一行,为4 个整数x1,y1, x2, y2,含义见题目描述。输入文件以EOF 结束。
Output
Sample Input
3 0 1 2
6 0 4 0
Sample Output
5
0
Data Constraint
解题思路
来自 欧几里得算法的应用 江苏省常州高级中学 金斌
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define ll long long
#define sqr(x) ((x)*(x))
using namespace std;
ll x,y,xx,yy;
void gcd(ll x,ll y,ll xx,ll yy,ll &n,ll &m)
{
ll dj,len1,len2;
dj=x*xx+y*yy;
len1=sqr(x)+sqr(y);
len2=sqr(xx)+sqr(yy);
//printf("%lld %lld %lld %lld %lld %lld %lld\n",x,y,xx,yy,len1,len2,dj);
if (len1>len2)
{
gcd(xx,yy,x,y,m,n);
return;
}
if (dj<0)
{
gcd(x,y,-xx,-yy,n,m);
m=-m;
return;
}
if (4*sqr(dj)<=len1*len2) {n=1; m=0; return;}
ll k;
k=dj/len1;
if (2*dj<(2*k+1)*len1)
{
gcd(x,y,xx-k*x,yy-k*y,n,m);
n-=k*m;
return;
}
else
{
gcd(x,y,xx-(k+1)*x,yy-(k+1)*y,n,m);
n-=(k+1)*m;
return;
}
}
ll n,m;
int main()
{
freopen("math.in","r",stdin);
freopen("math.out","w",stdout);
while (~scanf("%lld%lld%lld%lld",&x,&y,&xx,&yy))
{
gcd(x,y,xx,yy,n,m);
printf("%lld\n",sqr(n*x+m*xx)+sqr(n*y+m*yy));
}
return 0;
}