## 10341 - Solve It

Moderator: Board moderators

Chung Ha, Yun
New poster
Posts: 19
Joined: Tue Jul 16, 2002 5:56 pm
Location: Seoul
Contact:

### 10341 - Solve It

Test set is perfect!!...^^;; But, I recieve WA...again, again...

Please Help Me.. Where is wrong?

My Algorithm is The Bisection.

[c]
#include<stdio.h>
#include<math.h>

#define Napier 2.71828

int main()
{
int p, q, r, s, t, u;
double start, middle, end;
double start_result, middle_result, end_result;
while(scanf("%d %d %d %d %d %d", &p, &q, &r, &s, &t, &u) > 0)
{
start = 0, middle = 0.5, end = 1;
if(p == 0 && q == 0 && r == 0 && s == 0 && t == 0 && u == 0)
printf("No solution\n");
else
{
while(1)
{
start_result = p*pow(Napier, -1*start) + q*sin(start) + r*cos(start) + s*tan(start) + t*pow(start, 2) + u;
middle_result = p*pow(Napier, -1*middle) + q*sin(middle) + r*cos(middle) + s*tan(middle) + t*pow(middle, 2) + u;
end_result = p*pow(Napier, -1*end) + q*sin(end) + r*cos(end) + s*tan(end) + t*pow(end, 2) + u;

if((middle_result < 0 ? middle_result*-1 : middle_result) <= 0.000000001)
{
printf("%.4lf\n", middle);
break;
}
else if((start_result < 0 && middle_result > 0) || (start_result > 0 && middle_result < 0))
{
end = middle;
middle = (start + end) / 2;
}
else if((end_result < 0 && middle_result > 0) || (end_result > 0 && middle_result < 0))
{
start = middle;
middle = (start + end) / 2;
}
else
{
printf("No solution\n");
break;
}
}
}
}
return 0;
}
[/c]

shahriar_manzoor
Posts: 399
Joined: Sat Jan 12, 2002 2:00 am

### Value of e

Can't you write e=exp(1); Chung Ha, Yun
New poster
Posts: 19
Joined: Tue Jul 16, 2002 5:56 pm
Location: Seoul
Contact:

### Thanks..^^

I modified something..

So, I recieve Accepted.. Thanks~*

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:
Could anyone tell me, what could be wrong with this code ? I got WA ....

Code: Select all

``cutted off spoiler ...``
Last edited by Dominik Michniewski on Thu Jul 17, 2003 8:52 am, edited 1 time in total.

Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany
Ok, your value is not precise enough, finally I found it. Try with step*=0.9 and min = f(1), max = f(0) because the function is monotonic decreasing.

Leo_Jeru
New poster
Posts: 1
Joined: Wed Aug 28, 2002 5:24 pm

### 10341 I have send it for many times. But it's still "W

{ @BEGIN_OF_SOURCE_CODE }
{ @JUDGE_ID: 18459CN 10341 PASCAL "EA" }
program ProbF(input, output);
const
ep = 1e-14;

var
p, q, r, s, t, u: integer;

function f(x: extended): extended;
begin
f := p * exp(-x) + q * sin(x) + r * cos(x) + s * sin(x) / cos(x) + t * sqr(x) + u;
end;

function f_(x: extended): extended;
begin
f_ := - p * exp(-x) + q * cos(x) - r * sin(x) + s / sqr(cos(x)) + 2 * t * x;
end;

procedure solve;
var
x0, x1: extended;
i: integer;
begin
if(p = 0)and(q = 0)and(r = 0)and
(s = 0)and(t = 0)and(u <> 0)then
begin
writeln(output, 'No solution');
exit;
end;
x1 := 0;
while(abs(f(x1)) < ep)or(abs(f_(x1)) < ep)do x1 := x1 + 1;
for i := 1 to 5000 do
begin
x0 := x1;
{if abs(f_(x0)) < ep then
begin
writeln(output, 'No solution');
exit;
end;}
x1 := x0 - f(x0) / f_(x0);
if abs(x1 - x0) < ep then break;
end;
if abs(f(x1)) < ep then
writeln(output, x1:0:4)
else
writeln(output, 'No solution');
end;

begin
while not eof(input) do
begin
readln(input, p, q, r, s, t, u);
solve;
end;
end.
{ @END_OF_SOURCE_CODE }

Helal Md. Morshed Alam
New poster
Posts: 4
Joined: Fri Oct 19, 2001 2:00 am
Contact:
TRY THE CONCEPT OF BINARY SEARCH.

amd-RS
New poster
Posts: 27
Joined: Thu Sep 05, 2002 7:37 am

###  - WA ?

Why I get WA ??? For the test case it works ...

My code here:

[c]/* @JUDGE_ID: 13213RX 10341 C "<a href="http://cpu.ucpel.tche.br/">Clube de Programa

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong
Excuse me.

Is the given function strictly increasing/decreasing? Note: "the given function" refers to
f(x) = p*e^(-x) + q*sin(x) + r*cos(x) + s*tan(x) + t*x^2 + u
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

hager
New poster
Posts: 10
Joined: Wed Jan 01, 2003 4:26 am
Location: Ume
Is the given function strictly increasing/decreasing?
I have never plotted it, but considering all the trigonometric functions I would say no, it is definitely not strictly increasing / decreasing. However, you can be sure that the function has at most one root in between 0 and 1 inclusive.

Best regards

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong
Well...

I think that Bisection Method can be used iff the function is strictly increasing/decreasing within the limits...
Or am I wrong?? 7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong
Observer wrote:Well...

I think that Bisection Method can be used iff the function is strictly increasing/decreasing within the limits...
Or am I wrong?? OK. I see that I'm wrong. It should be:
Bisection Method can be used iff f(upper) and f(lower) are of opposite signs...

Anyway, I've just got an ACC!!!!!!! Thx everyone!!!
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

Alexander Kozlov
New poster
Posts: 23
Joined: Sun Jun 15, 2003 4:45 pm
Location: Ukraine
Please, help! What wrong with this code?

Code: Select all

``````#include <iostream.h>
#include <math.h>
#include <stdio.h>

int p,q,r,s,t,u;
double a, b, c;

double f(double x)
{
return p/exp(x) + q*sin(x) + r*cos(x) + s*tan(x) + t*x*x + u;
}

int main() {

while ((cin >> p >> q >> r >> s >> t >> u) != NULL) {

a = 0.0; b = 1.0;

if (f(b) > 0 || f(a) < 0) {
printf("No solution\n");
continue;
}

while (b - a >= 0.00001) {

if (f(a) == 0) {
printf("%.4lf\n", a);
break;
}

if (f(b) == 0) {
printf("%.4lf\n", b);
break;
}

c = (a + b) / 2;

if (f(a) * f(c) > 0) a = c; else b = c;

}

printf("%.4lf\n", c);

}

return 0;
}``````

Noim
Learning poster
Posts: 88
Joined: Sun Oct 13, 2002 6:11 am
if you check two limits for which the funtion gives values of opposite sign, you can be sure that there is a sol between the limits.  