#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
using namespace std;
double a,b,c,d,e,f,ansx,ansy,ansz;
const double inf=1e18;
inline double dist(double x,double y,double z){
return sqrt(x*x+y*y+z*z);
}
inline double calc(double x,double y){
double A=c,B=e*x+d*y,C=a*x*x+b*y*y+f*x*y-1.0;
double delta=B*B-4.0*A*C;//公式啊
if(delta<0)return inf;//初中知识啊
double z1=(-B+sqrt(delta))/(2.0*A),z2=(-B-sqrt(delta))/(2.0*A);
if(dist(x,y,z1)<dist(x,y,z2))return z1;//两个解选最优的
return z2;
}
inline void mnth(){
double T=2333,eps=1e-15;
while(T>eps){
double nowx=ansx+(rand()*2-RAND_MAX)*T;
double nowy=ansy+(rand()*2-RAND_MAX)*T;
double nowz=calc(nowx,nowy);
if(nowz==inf){T*=0.997;continue;}
double delta=dist(nowx,nowy,nowz)-dist(ansx,ansy,ansz);
if(delta<0)ansx=nowx,ansy=nowy,ansz=nowz;
else if(exp(delta/T)*RAND_MAX<rand())ansx=nowx,ansy=nowy,ansz=nowz;
T*=0.997;
}
}
int main(){
//srand((int)time(NULL));//HDU应该是不支持这个
srand(19260817);//随便搞个数就行
while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f)!=EOF){
ansx=0;ansy=0;ansz=sqrt(1.0/c);
mnth();printf("%.10lf\n",dist(ansx,ansy,ansz));
}
return 0;
}
原文:https://www.cnblogs.com/PPXppx/p/11668603.html