10341 - Solve It

All about problems in Volume 103. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

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

10341 - Solve It

Post by Chung Ha, Yun » Sat Aug 03, 2002 1:45 pm

Test set is perfect!!...^^;; :D

But, I recieve WA...again, again...

Please Help Me.. :cry:

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
System administrator & Problemsetter
Posts: 399
Joined: Sat Jan 12, 2002 2:00 am

Value of e

Post by shahriar_manzoor » Sat Aug 03, 2002 5:17 pm

Can't you write e=exp(1); :roll:

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

Thanks..^^

Post by Chung Ha, Yun » Sat Aug 03, 2002 6:18 pm

I modified something..

So, I recieve Accepted.. :D

Thanks~*

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » Tue Aug 20, 2002 2:33 pm

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.

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel » Tue Aug 20, 2002 3:49 pm

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

Post by Leo_Jeru » Wed Aug 28, 2002 5:30 pm

{ @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
Location: AIUB, BANGLADESH
Contact:

Post by Helal Md. Morshed Alam » Thu Aug 29, 2002 12:37 am

TRY THE CONCEPT OF BINARY SEARCH.

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

[10341] - WA ?

Post by amd-RS » Thu Sep 05, 2002 8:07 am

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

Post by Observer » Tue Jun 17, 2003 10:48 am

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

Post by hager » Tue Jun 17, 2003 11:05 am

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

Post by Observer » Tue Jun 17, 2003 11:47 am

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

Post by Observer » Tue Jun 17, 2003 11:51 am

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

Post by Alexander Kozlov » Wed Jul 02, 2003 4:24 pm

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
Location: Bangladesh

Post by Noim » Tue Jul 15, 2003 10:42 pm

i am not sure about the bugs of your problem but you can check your prgram for the inputs when the answer will be 0.0000 or 1.0000.
__nOi.m....

anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post by anupam » Wed Jul 16, 2003 2:59 pm

actually nr or bisection method need not know the fn is either inc or dec.
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. :oops: :oops:
"Everything should be made simple, but not always simpler"

Post Reply

Return to “Volume 103 (10300-10399)”