Giuseppe
Giuseppe

Reputation: 492

10341 - Solve It WA

I was working on this problem from the ACM Uva webpage, and I've tried every single input from the forums, and it works perfectly. It keeps giving me "Wrong answer", and as I'm not an english speaker, maybe I'm forgetting something. Maybe someone from here had submitted this solution. This is the 5th submit I've made from this problem and it's always wrong. Thanks a lot. EDIT:

Solve the equation:

p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0

where 0 <= x <= 1.

Input

Input consists of multiple test cases and terminated by an EOF. Each test case consists of 6 integers in a single line: p, q, r, s, t and u (where 0 <= p,r <= 20 and -20 <= q,s,t <= 0). There will be maximum 2100 lines in the input file.

Output

For each set of input, there should be a line containing the value of x, correct upto 4 decimal places, or the string "No solution", whichever is applicable.

#include <math.h>
#include <stdio.h>
#define eps 0.00001

int p, q, r,s,t,u;

long double fdex(long double x)
{
long double res=p*exp(-x)+ q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u;

return res;


}



int main()
{
freopen("input.txt","rt",stdin);
freopen("output.txt","wt",stdout);


long double x,inf,sup,fx;
while(scanf("%d %d %d %d %d %d\n",&p, &q, &r,&s,&t,&u)!=EOF)
{
    long double x1;
    inf=0.0;
    sup=1.0;
    if (p==0 && q==0 && r==0 && s==0 && t==0)
    printf("0.0000\n");
    else if((fdex(inf)*fdex(sup)>0.0))
    printf("No solution\n");

        else 
        {


                do
                {
                    x=(inf+sup)/2.0;
                    fx=fdex(x);
                        if(fx>eps)
                        {
                        inf=x;
                        }
                        else if(fx<-eps)
                        {
                        sup=x;

                        }

                }while( fx<-eps || fx>eps);
                printf("%.4llf\n",x);

        }
}


}

Problem

Upvotes: 0

Views: 1361

Answers (5)

Daniel Ford
Daniel Ford

Reputation: 59

You are confusing binary search on x (over the 10,000 possible values: 0.0000 through 1.0000) with binary search on f(x): just because f(x) is very close to 0 doesn't mean that f(x + 0.0001) is not closer to 0.

Upvotes: 0

Z Rayan
Z Rayan

Reputation: 16

The mistake is in :

else if((fdex(inf)*fdex(sup)>0.0))
    printf("No solution\n");

As I belive this fn is decreasing so the replacement will be:

x = sup;
else if((fdex(x)>0.0)&& (x==1))
    printf("No solution\n");

Upvotes: 0

dev13
dev13

Reputation: 23

One possibility is that you forgot the case where p=q=r=s=t=0, but u != 0.

Also, the printf modifier for long double is L, not ll.

Upvotes: 0

Peter de Rivaz
Peter de Rivaz

Reputation: 33509

Does it help to make eps smaller?

I worry that with the current 0.00001 it might not quite find the root accurately enough to be correct to 4 significant figures.

Upvotes: 0

IVlad
IVlad

Reputation: 43467

Some things to try / consider:

  1. printf("%.4llf\n",x); will round the answer to 4 decimal places, so 1.23456 will become 1.2346. The problem isn't clear if this is what you're expected to do - it might want the first 4 decimals without rounding;
  2. What jrok said in comments:

    If output needs to come in lines, something as silly as a redundant empty line at the end of input can make your submission WA on Uva. For example, for (int i = 0; i < ARRAYSIZE; ++i) cout << arr[i] << '\n'; may not work. You need additional check to not print '\n' after the last line. Stupid IMO, but that's how it is...

    Personally I never experienced this on UVA, but I wasn't very active there, so you might want to check it.

  3. Don't use scanf(..) != EOF. scanf returns the number of successfully read values, so you should do scanf(...) == 6.

Upvotes: 1

Related Questions