UVA10341 Solve It

题意

PDF

分析

\(0\le x\le 1\)时,\(f(x)=pe^{-x}+q\sin x+r\cos x+s\tan x+tx^2+u\)是减函数,所以当\(f(0)\ge 0 \wedge f(1)\le 0\)时,函数有唯一零点,否则没有。

那么二分答案即可。控制二分次数,时间复杂度\(O(100)\)

代码

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
    while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;

#define F(x) (p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*(x)*(x)+u)
co double eps=1e-14;
int main(){
//  freopen(".in","r",stdin),freopen(".out","w",stdout);
    int p,r,q,s,t,u;
    while(~scanf("%d%d%d%d%d%d",&p,&q,&r,&s,&t,&u)){
        if(F(1)>eps||F(0)<-eps) {puts("No solution");continue;}
        double x=0,y=1,m;
        for(int i=0;i<100;++i){
            m=(x+y)/2;
            F(m)<0?y=m:x=m;
        }
        printf("%.4lf\n",m);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/autoint/p/10662440.html