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 <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#define ll long long
ll x1,y1,x2,y2;
ll sqr(ll x)
{
return x * x;
}
ll getDot(ll x1,ll y1,ll x2,ll y2)
{
return x1 * x2 + y1 * y2;
}
ll getDis(ll x,ll y)
{
return sqr(x) + sqr(y);
}
void slove(ll x1,ll y1,ll x2,ll y2,ll &a,ll&b)
{
if (getDot(x1,y1,x2,y2) < 0)
{
slove(x1,y1,-x2,-y2,a,b);
b = -b;
return;
}
if (getDis(x1,y1) > getDis(x2,y2))
{
slove(x2,y2,x1,y1,b,a);
return;
}
ll len1 = getDis(x1,y1);
ll len2 = getDis(x2,y2);
ll dot = getDot(x1,y1,x2,y2);
if (sqr(dot) * 4 <= len1 * len2)
{
a = 1;
b = 0;
return;
}
ll k = dot / len1;
if (dot * 2 > (k + k + 1) * len1)
{
k++;
}
slove(x1,y1,x2 - k * x1,y2 - k * y1,a,b);
a = a - k * b;
}
int main()
{
freopen("math.in","r",stdin);
freopen("math.out","w",stdout);
while (~scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2))
{
ll x,y;
slove(x1,y1,x2,y2,x,y);
printf("%lld\n",getDis(x1 * x + x2 * y , y1 * x + y * y2));
}
}