10056 - What is the Probability ?

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

Moderator: Board moderators

StatujaLeha
Learning poster
Posts: 91
Joined: Tue May 31, 2005 2:01 pm
Location: Russia

Post by StatujaLeha » Sun Oct 16, 2005 9:11 pm

It is my program. I use the same algorithm, but i get WA. ReiVaX18, do you use any tricks for printing the result?

Code: Select all

       if((p != 0)&&(p != 1))
      {
         q = 1.0 - p;
         printf("%.4lf\n",(pow(q,I - 1)*p)/(1.0 - pow(q,N)));
      }
      else
      {
         if(p == 0)
            printf("%.4lf\n",0.0000);
         else//p == 1
         {
            if(N == 1)
               printf("%.4lf\n",1.0000);
            else
               printf("%.4lf\n",0.0000);
         }
      }

ReiVaX18
New poster
Posts: 11
Joined: Mon Mar 29, 2004 11:53 am

Post by ReiVaX18 » Sun Oct 16, 2005 9:22 pm

I can't see any error, the only difference is that I use long double, but I think this should not be a problem

Code: Select all

#include <iostream>
#include <iomanip>
using namespace std;

long double pawah( long double b, int e ) {
	long double r = 1.0;
	for( int i = 0; i < e; i++ ) r *= b;
	return r;
}

int main() {
	cout.setf( ios::fixed );
	cout.precision( 4 );
	int s, n, k;
	long double p;
	cin >> s;
	while( s-- ) {
		cin >> n >> p >> k;
		if( p > 0.0 ) cout << p*pawah(1-p,k-1)/(1-pawah(1-p,n)) << endl;
		else cout << 0.0 << endl;
	}
}


StatujaLeha
Learning poster
Posts: 91
Joined: Tue May 31, 2005 2:01 pm
Location: Russia

Post by StatujaLeha » Sun Oct 16, 2005 10:11 pm

I find the mistake :)

Code: Select all

       if((p != 0)&&(p != 1))
      {
         q = 1.0 - p;
         printf("%.4lf\n",(pow(q,I - 1)*p)/(1.0 - pow(q,N)));
      }
      else
      {
         if(p == 0)
            printf("%.4lf\n",0.0000);
         else//p == 1
         {
            if([b]I[/b] == 1)
               printf("%.4lf\n",1.0000);
            else
               printf("%.4lf\n",0.0000);
         }
      }

mido
Learning poster
Posts: 78
Joined: Sun Jun 16, 2002 9:48 pm
Location: Cairo,Egypt

Post by mido » Mon Oct 17, 2005 3:37 pm

I think you meant I==1 instead of N==1.... :roll:

StatujaLeha
Learning poster
Posts: 91
Joined: Tue May 31, 2005 2:01 pm
Location: Russia

Post by StatujaLeha » Mon Oct 17, 2005 8:52 pm

I think you meant I==1 instead of N==1....
yes :) There I have fixed it. http://online-judge.uva.es/board/viewtopic.php?t=5530

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post by Darko » Thu Nov 17, 2005 9:42 am

Ok, I have no idea what I'm doing wrong, except using Java :)

Can someone please check these and/or post some input data?

Input:
19
2 0.166666 1
2 0.166666 2
100 0.000001 1
100 0.000001 99
999 0.00023 591
100 0.0 4
915 0.166666 1
23 0.1111 13
1 1.0 2
1 1.0 1
2 1.0 1
1000 0.0001 500
7 0.33333333 4
13 0.11111111 5
123 0.27362732 29
12 0.123456789 10
12 0.00000001 10
12 0.123456789 2
12 0.00000001 2
Output:
0.5455
0.4545
0.0100
0.0100
0.0010
0.0000
0.1667
0.0290
0.0000
1.0000
1.0000
0.0010
0.1049
0.0885
0.0000
0.0475
0.0833
0.1362
0.0833
Thanks,

Darko

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post by Darko » Wed Jul 12, 2006 1:13 am

I/O above is correct - I think adding EPS to the output did it for me, but I am not sure.

greynolds
New poster
Posts: 2
Joined: Tue Jun 23, 2009 10:07 pm

Re: 10056 - What is the Probability?

Post by greynolds » Tue Jun 23, 2009 10:11 pm

I am getting Wrong Answer with my java code, and I seem to get the same output as the other forum posters. I am not sure what "adding EPS" means above...

Code: Select all

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


public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BufferedReader stdin = 
			new BufferedReader(new InputStreamReader(System.in));
		
		try {
			while (stdin.ready())
			{
				String sLine = stdin.readLine();
				String outValue = winProbability(sLine);
				if (outValue != null)
				{
					System.out.println(outValue);
				}
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	static String winProbability(String input)
	{
		String [] fields = input.split(" +");
		if (fields.length != 3)
		{
			return null;
		}
		long N = Long.parseLong(fields[0]);
		double p = Double.parseDouble(fields[1]);
		long i = Long.parseLong(fields[2]);
		
		NumberFormat form = new DecimalFormat("0.0000");
		double prob = winProbability(N,p,i);
		return form.format(prob);
	}

	private static double winProbability(long n, double p, long i) {
		if (p == 0)
		{
			return 0;
		}

		return Math.pow(1.0-p,i-1.0)
		/((1/p)*(1.0-Math.pow(1.0-p,n)));
	}
}
Using the input

Code: Select all

19
2 0.166666 1
2 0.166666 2
100 0.000001 1
100 0.000001 99
999 0.00023 591
100 0.0 4
915 0.166666 1
23 0.1111 13
1 1.0 2
1 1.0 1
2 1.0 1
1000 0.0001 500
7 0.33333333 4
13 0.11111111 5
123 0.27362732 29
12 0.123456789 10
12 0.00000001 10
12 0.123456789 2
12 0.00000001 2
Gives

Code: Select all

0.5455
0.4545
0.0100
0.0100
0.0010
0.0000
0.1667
0.0290
0.0000
1.0000
1.0000
0.0010
0.1049
0.0885
0.0000
0.0475
0.0833
0.1362
0.0833

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Re: 10056 - What is the Probability?

Post by Darko » Tue Jun 23, 2009 10:14 pm

Means add small value to the result before formatting (usually referred to as epsilon or EPS in code).

In your case, you can try adding 1e-10 (or some suitable value) to prob before printing it.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: 10056 - What is the Probability?

Post by mf » Wed Jun 24, 2009 1:28 am

Adding an epsilon is needed because if your answer is very close to zero, small rounding errors could make it negative and make it print as -0.0000. Alternatively, you can print Math.max(0, your answer).

Also, Math.pow(1.0-p,i-1.0) will be NaN for p=1, i=1, and NaN's are never a good thing.

And here:

Code: Select all

      try {
         while (stdin.ready())
         {
            String sLine = stdin.readLine();
            ...
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
you shouldn't be catching exceptions or printing any stack traces. Let it be thrown out of main() - then the judge has a chance to say you got "runtime error" instead of potentially confusing "wrong answer".

I'd use this to read the input:

Code: Select all

int T = Integer.parseInt(stdin.readLine().trim());   // <- don't just ignore it!
for (int cs = 1; cs <= T; cs++) {
    String sLine = stdin.readLine();
     ...
}
And here:

Code: Select all

      String [] fields = input.split(" +");
      if (fields.length != 3) {
I think it's not going to work for leading or trailing whitespace on lines.

greynolds
New poster
Posts: 2
Joined: Tue Jun 23, 2009 10:07 pm

Re: 10056 - What is the Probability?

Post by greynolds » Wed Jun 24, 2009 10:38 am

Thanks for the replies. It's still not working though...

The code now is

Code: Select all

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


public class Main {

  /**
   * @param args
   * @throws IOException 
   * @throws NumberFormatException 
   */
  public static void main(String[] args) throws NumberFormatException, IOException {
    BufferedReader stdin = 
      new BufferedReader(new InputStreamReader(System.in));
    int T = Integer.parseInt(stdin.readLine().trim());
    for (int cs = 1; cs <= T; cs++) {      
      String sLine = stdin.readLine();
      String outValue = winProbability(sLine);
      if (outValue != null)
      {
        System.out.println(outValue);
      }            
    }
  }

  static String winProbability(String input)
  {
    String [] fields = input.trim().split(" +");

    long N = Long.parseLong(fields[0]);
    double p = Double.parseDouble(fields[1]);
    long i = Long.parseLong(fields[2]);

    NumberFormat form = new DecimalFormat("0.0000");
    double prob = winProbability(N,p,i);
    return form.format(prob);
  }

  private static double winProbability(long n, double p, long i) {
    if (p == 0)
    {
      return 0;
    }
    if (p >= 1)
    {
      return 1;
    }

    double q = 1 - p;

    double a = Math.pow(q,i-1) * p;
    double b = Math.pow(q, n);

    return Math.max(a / (1 - b) + 1e-10,0);
  }
}

Shafaet_du
Experienced poster
Posts: 147
Joined: Mon Jun 07, 2010 11:43 am
Location: University Of Dhaka,Bangladesh
Contact:

Re: 10056 - What is the Probability?

Post by Shafaet_du » Mon Mar 28, 2011 5:18 pm

Spoiler

Ans=(P(i-P)^i-1)/(1-(1-P)^n);

i've used 1e-8 as eps. Special case: if P==0 then ans=0;

Spoiler

Post Reply

Return to “Volume 100 (10000-10099)”