Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import java.math.RoundingMode;
  2. import java.text.DecimalFormat;
  3. import java.util.Scanner;
  4.  
  5. public class C {
  6. public static void main(String[] args) {
  7. int n = 0;
  8. int m = 0;
  9. int k = 0;
  10.  
  11. double[][] probDistribution = new double[2][9];
  12.  
  13. try (Scanner sc = new Scanner(System.in)) {
  14. n = sc.nextInt();
  15. m = sc.nextInt();
  16. k = sc.nextInt();
  17. }
  18.  
  19. probDistribution[1][0] = 1;
  20.  
  21. for (int i = 1; i <= k; i++) {
  22. int last = Math.min(m * n, i) - 1;
  23. probDistribution[1][last] = 1;
  24.  
  25. for (int j = 0; j < last; j++) {
  26. if (j == 0) {
  27. probDistribution[1][j] /= (m * n);
  28. } else {
  29. double one = probDistribution[0][j - 1] * (m * n - j) / (m * n);
  30. double two = probDistribution[0][j] * (j + 1) / (m * n);
  31.  
  32. probDistribution[1][j] = one + two;
  33.  
  34. }
  35. probDistribution[1][last] -= probDistribution[1][j];
  36. }
  37.  
  38. for (int j = 0; j < i; j++) {
  39. probDistribution[0][j] = probDistribution[1][j];
  40. }
  41. }
  42.  
  43. double expectedValue = 0;
  44.  
  45. int maxTarget = Math.min(k, m * n);
  46.  
  47. for (int i = 0; i < maxTarget; i++) {
  48. double v = (i + 1d) * probDistribution[0][i];
  49. expectedValue += v;
  50. }
  51.  
  52. System.out.println(printDoubleJava(expectedValue));
  53. }
  54.  
  55. public static String printDoubleJava(double d) {
  56. DecimalFormat df = new DecimalFormat("0.000000000000");
  57. df.setRoundingMode(RoundingMode.HALF_UP);
  58.  
  59. return df.format(d);
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement