1. 程式人生 > >UVA10341 Solve It

UVA10341 Solve It

復雜 out nal pdf dig print fine isdigit 二分

題意

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;
}

UVA10341 Solve It