1.

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS11
* Problem: 10056 - What is the Probability ?
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=997
*
* @author Christian Posselt
* @author Christian Mitterreiter
* @version 1.0, 25.05.2011
*
* Status: Accepted
* Time: 0.600
*/

import java.io.IOException;
import java.util.Scanner;

public class Main
{

public static void main(String[] args) throws NumberFormatException, IOException
{
/**
* Scanner for the input
*/
Scanner sc = new Scanner(System.in);

/**
* amount of testcases
*/
int cases = sc.nextInt();

for(int i=1;i<=cases;i++)
{

//amount of players
int N = sc.nextInt();

//probability of a win
double p = sc.nextDouble();

//the k-th player should win
int k = sc.nextInt();

double result;

if(p>0)
//X(P=k) = p*q^(k-1) / ( 1 - q^N) with q = (1-p)
//
// 1. For k to win, k-1 players have to fail => p*q^(k-1),
// 2. But it could also happen that he wins in the second round => p*q^(k-1)*q^(n)
// 3. But it could also happen that he wins in the third round => p*q^(k-1)*q)^(n^2)
// 4. But it could also happen that he wins in the y-th round => p*q^(k-1)*q^(n^(y-1))
// The sum of the n-th rounds is a geometric series, therefore:
// p*(1-p)^(k-1) * (sum(i=0 to infinity, q^(i*n))) => p*q^(k-1) / (1-q^n)
result = Math.pow((1-p),k-1) * p / ( 1 - Math.pow((1-p),N) );
else
result = 0.;


//print result
System.out.printf("%.4f\n",result);
}
}

}

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



1.
import java.util.Scanner;

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10
* Problem: 10056 - What is the Probability ?
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=226&page=show_problem&problem=997
*
* @author Siegfried Ippisch
* @author Martin Lambeck
* @version 1.0
*
* Method : -
* Status : Accepted
* Runtime: 0.632
*/
public class WhatIsTheProbability {

static Scanner in = new Scanner(System.in);

public static void main (String...strings)
{
int cases = in.nextInt();

for (int i = 0; i < cases; i++)
{
testcase();
}

}

static void testcase()
{
int players = in.nextInt();
double p = in.nextDouble();
int winner = in.nextInt() -1;

double rem = 1.0;

double wp = 0.0;



for (int pl = 0; pl < players; pl++)
{
double w = rem * p;

if (pl == winner)
wp = w;

rem -= w;
}

wp = (1.0/(1.0-rem)) * wp;
//wp =
// while (rem >= 0.00001)
// {
// wp += rem * firstwp;
// rem = firstrem * rem;
// }

System.out.printf("%.4f\n", p == 0 ? 0.0 : wp);
}
}