1.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;

/**
* Angewandte Mathematik, SS11
* Problem: 10341 - Solve It
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=15&page=show_problem&problem=1282
*
* @author Benedikt Z¨nnchen
* @author Erik Wenzel
* @version 1.0, 29/04/2011
*
* Method : Newtonverfahren (x1 = x - f(x)/df(x))
* Status : Accepted
* Runtime: 0.552
*/
public class Main
{

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line;
int a,b,c,d,e,f;
double x,xdif;
double f1,f2;
DecimalFormat df = new DecimalFormat();
df.applyPattern("0.0000");

while((line=reader.readLine()) != null)
{
String[] split = line.split(" ");
a = Integer.parseInt(split[0]);
b = Integer.parseInt(split[1]);
c = Integer.parseInt(split[2]);
d = Integer.parseInt(split[3]);
e = Integer.parseInt(split[4]);
f = Integer.parseInt(split[5]);
x = 0.5;
xdif = 1;
double e1 = f(0, a, b, c, d, e, f);
double e2 = f(1, a, b, c, d, e, f);
if (e1==0)
{
x = 0;
}
else if(e2==0)
{
x = 1;
}
else if(e1 < 0 && e2 > 0 || e1 > 0 && e2 <0)
{
int counter = 10;
while(/*counter>=0*/Math.abs(xdif) >= 1e-8)
{
counter--;
f1 = f(x, a, b, c, d, e, f);
f2 = df(x, a, b, c, d, e);
xdif = f1/f2;
x = x - xdif;
}
}
else
{
x = 2;
}

if(x <= 1 && x >= 0)
{
System.out.println(df.format(x).replace(',', '.'));
}
else
{
System.out.println("No solution");
}

}

}

public static double f(double x, int a, int b, int c, int d, int e, int f)
{
return a*Math.exp(-x) + b*Math.sin(x) + c*Math.cos(x) + d*Math.tan(x) + e*x*x + f;
}

public static double df(double x, int a, int b, int c, int d, int e)
{
return -a*Math.exp(-x) + b*Math.cos(x) - c*Math.sin(x) + d/(Math.cos(x)*Math.cos(x)) + 2*e*x;
}

}

2.

/**
* Angewandte Mathematik, SS11
* Problem: 10341 Solve It
* Link:
*
* @author Schramm, Felix
* @author Vogel, Johannes
* @version 1.0, 05/25/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.708
*/

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
StringBuffer output = new StringBuffer();
String result = new String();
int p,q,r,s,t,u = 0;
double x,fx,fsx,sinx,cosx,eXneg = 0.0;

while(input.hasNextInt()){
p = input.nextInt();
q = input.nextInt();
r = input.nextInt();
s = input.nextInt();
t = input.nextInt();
u = input.nextInt();

x=0.0;
sinx=Math.sin(x);
cosx=Math.cos(x);
eXneg=Math.pow(Math.E,-x);
fx=p*eXneg+q*sinx+r*cosx+s*(sinx/cosx)+t*x*x+u;
if(fx==0.0)
output.append("0.0000\n");

else {
x=1.0;
sinx=Math.sin(x);
cosx=Math.cos(x);
eXneg=Math.pow(Math.E,-x);
fx=p*eXneg+q*sinx+r*cosx+s*(sinx/cosx)+t*x*x+u;
if(fx==0.0){
output.append("1.0000\n");
}
else{
x = 0.5;
fx=1.0;

while(Math.abs(fx)>0.00001){
sinx=Math.sin(x);
cosx=Math.cos(x);
eXneg=Math.pow(Math.E,-x);
fx=p*eXneg+q*sinx+r*cosx+s*(sinx/cosx)+t*x*x+u;
fsx=-p*eXneg+q*cosx-r*sinx+s/(cosx*cosx)+2*t*x;
x=x-fx/fsx;
}
if(x>0.0&&x<1.0){
result=String.format("%.4f\n",x);
output.append(result);
}
else
output.append("No solution\n");

}
}
}
System.out.print(output);

}

}




-------------------------------------------------------------------------------------------------------


1.
package acm_10341_solve_it;

/**
* FWP, Ausgew¦hlte Probleme aus dem ACM Programming Contest, SS10
* Problem: 10341 Solve it
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=15&page=show_problem&problem=1282
*
* @author Martin Lambeck
* @version 1.0, 01/08/2010
*
* Method : newtons method, bisection (both AC)
* Status : Accepted
* Runtime: 0.620
*/




public class Main
{
static BufferedLongReader blr = new BufferedLongReader();
static long p, q, r, s, t, u;

public static void main (String...args)
{
while (testcase());
}

public static boolean testcase()
{
try
{
p = blr.nextLong();
q = blr.nextLong();
r = blr.nextLong();
s = blr.nextLong();
t = blr.nextLong();
u = blr.nextLong();

} catch (IllegalStateException e)
{
//input is terminated by eof
if (e.getCause() instanceof java.io.EOFException)
return false;
else
throw e;
}


//bisection and newton are both AC

double result;

result = bisection();
//result = newton();


if (result >= 0.0 && result <= 1.0)
System.out.printf("%.4f%n", result, result);
else
System.out.println("No solution");

return true;
}

//f(x) is monotone decreasing in [0,1]
// if f(x) is not collinear with the x-axis in [0,1] then
// sign( f(a) ) != sign( f(b) ), a < b <=> A c exists, such that ( f(c) == 0 ), with ( a <= c <= b )
public static double bisection()
{
if (f(0.0) == 0.0)
return 0.0;
if (f(1.0) == 0.0)
return 1.0;
if (f(0.0) * f(1.0) > 0) //if same sign
return -1;

double l = 0.0;
double r = 1.0;
double mid = 0.5;

while (f(l)-f(r) > 0.000000001)
{
mid = (r - l) / 2.0 + l;

if (f(mid) == 0)
return mid;

if (f(mid) * f(r) > 0) //check for same sign
r = mid;
else
l = mid;
}

return mid;
}

//the well-known newton's method. see wikipedia
public static double newton()
{
double bestx = -1;
double bestfx = 999;
double x = 0.0;
double gx = -1;
double fx = -1;

//use different startvalues for x
for (double x0 = 0.0; x0 <= 1.01; x0 += 0.1)
{
x = x0;

//do 30 iterations ( way more than actually required ;) )
for (int i = 0; i < 30; i++)
{
gx = g(x);
fx = f(x);

if (fx == 0 && x <= 1 && x >= 0)
return x;

// if slope is too flat we probably wont get a useful value for x
// especially handle gx==0
if (Math.abs(gx) < 0.00001)
{
x = -1;
break;
}

x = x - fx/gx; // x <- x - f(x)/f'(x)
}

if (x <= 1 && x >= 0 && Math.abs(fx) < bestfx)
{
bestfx = Math.abs(fx);
bestx = x;
}
}

if (Math.abs(f(bestx)) <= 0.00001)
return bestx;
else
return -1;
}

public static double f(double x)
{
double result;
double cosx;
double sinx;

cosx = Math.cos(x);
sinx = Math.sin(x);

result = (p * Math.exp(-x)) + (q * sinx) + (r * cosx) + (s * (sinx/cosx)) + (t * x * x) + u;

return result;
}

//g(x) = f'(x) = df(x)/dx
public static double g(double x)
{
double result;
double cosx;

cosx = Math.cos(x);
result = (-p * Math.exp(-x)) + (q * cosx) - (r * Math.sin(x)) + (s / (cosx*cosx)) + (2 * t * x);

return result;
}
}

class BufferedLongReader
{
byte[] buf = new byte[1024*32];
int pos = 0;
int len = 0;

public long nextLong()
{
long num = 0;
int chr;
boolean found = false;
boolean negative = false;
boolean stop = false;

while (!stop)
{
if (pos == len)
{
pos = 0;

try
{
len = System.in.read(buf);

} catch (java.io.IOException e)
{
throw new IllegalStateException(e); //wrap into runtime exception and throw
}
}

if (len == -1)
{
if (!found)
throw new IllegalStateException(new java.io.EOFException());

stop = true;
chr = ' ';

} else
{
chr = buf[pos];
pos++;
}

if (chr >= '0' && chr <= '9')
{
num = num*10 + (chr - '0');
found = true;

} else if (chr == '-')
{
if (found)
{
// - after a number
// stop here, and push back - into the buffer, maybe sign of next number
stop = true;
pos--;
buf[pos] = '-';

} else
{
negative = true;
}

} else
{
if (found)
stop = true;
else
negative = false;
}
}

return (negative ? -num : num);
}
}

2.

1.


import java.util.Scanner;

/**

* @group Lermer Florian, Sayli Hidir, Taskin Umut

* @contact lermer@gmail.de, hidirsayli@hotmail.de taskin_umut@yahoo.de

* @Problem_ID=U10341

* @Link http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=15&page=show_problem&problem=1282

* @external_help_link http://online-judge.uva.es/board/viewtopic.php?f=20&t=1058&st=0&sk=t&sd=a&sid=ca99548db7a11d738230331897e5504c

*

* @status Accepted

* @Version V1.09

*

* @problematical_issues - Only mathematical Problems *

* @param args

*/

/*

* 1. calculate x=f(0) and y=f(1)

2. if (x > 0 && y > 0) print 'No solution'.

3. if (x < 0 && y < 0) print 'No solution'.

4. if ( x == 0 ) print '0.0000'.

5. if ( y == 0 ) print '1.0000'.

6. You are sure that u have a solution, and calculate it (by your process).

*/

public class Main {



/**

* @param args

*/

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

public static void main(String[] args)

{

Scanner sc=new Scanner(System.in);



double x,y;

while(sc.hasNext())

{

p=sc.nextInt();

q=sc.nextInt();

r=sc.nextInt();

s=sc.nextInt();

t=sc.nextInt();

u=sc.nextInt();

x=function(0);

y=function(1);

if (x > 0 && y > 0){ System.out.println("No solution");continue;}

if (x < 0 && y < 0){ System.out.println("No solution");continue;}

if ( x == 0 ){ System.out.println("0.0000");continue;}

if ( y == 0 ){ System.out.println("1.0000");continue;}

else {

x=0.5;

for (int i=0;i<10;i++){

x=x-function(x)/dfunction(x);

}

System.out.printf("%.4f%n",x);



}

}



}



private static double function(double x) {

// return p/pow(E,x) + q*sin(x) + r*cos(x) + s*tan(x) + t*x*x + u;

double se=p/Math.pow(Math.E, x)+q* Math.sin(x)+r*Math.cos(x)+ s*Math.tan(x)+t*x*x+u;

return se;

}

private static double dfunction(double x)

{

// double se=1.0/cos(x);

// se*=se;

// return -p/pow(E,x) + q*cos(x) - r*sin(x) + s*se + t*2*x;

double se=1.0/Math.cos(x);

se*=se;

se=-p/Math.pow(Math.E, x)+q* Math.cos(x)-r*Math.sin(x)+ s*se+t*2*x;

return se;



}



}

3.

/**

 * ACM Training 2009

 * ACM Problem #10341 -
Solve It

 * Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=15&page=show_problem&problem=1282

 *

 * @author Felix Dietrich

 * @version 1.0, 09/23/2009

 *

 * Methode: Newton Method

 * Status : Accepted

 * Runtime: 0.672

 */



import java.util.*;



public class Main

{

    static
double p,q,r,s,t,u;

    

    public
static void main(String... strs)

    {

      
 Scanner sc = new Scanner(System.in);

      
 

      
 

      
 double solution;

      
 

      
 while(sc.hasNext())

      
 {

      
     p = sc.nextInt();

      
     q = sc.nextInt();

      
     r = sc.nextInt();

      
     s = sc.nextInt();

      
     t = sc.nextInt();

      
     u = sc.nextInt();

      
     

      
     if(Math.abs(f(0.0)) < 1e-6 )

      
       
 System.out.println(String.format("%.04f", 0.0000));

      
     else if(Math.abs(f(1.0))<1e-6)

      
       
 System.out.println(String.format("%.04f", 1.0000));

      
     else if(f(0.0) * f(1.0) > 0)

      
         System.out.println("No
solution");

      
     else

      
     {

      
         solution = 0.5;

      
         

      
         for(int i=0; i<20; i++)

      
         {

      
             solution
= solution - f(solution)/fDiff(solution);

      
         }

      
         

      
       
 System.out.println(String.format("%.04f", solution));

      
     }

      
 }

    }



    private
static double f(double x)

    {

      
 return
p*Math.exp(-x)+q*Math.sin(x)+r*Math.cos(x)+s*Math.tan(x)+t*x*x+u;

    }

    

    private
static double fDiff(double x)

    {

      
 return
-p*Math.exp(-x)+q*Math.cos(x)-r*Math.sin(x)+s*1.0/(Math.cos(x)*Math.cos(x))+2.0*t*x;

    }

}