fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static double p[][];
  11. static double dp[];
  12. static int n;
  13.  
  14. public static double f(int mask) {
  15. if (dp[mask] > -0.5)
  16. return dp[mask];
  17.  
  18.  
  19.  
  20. return dp[mask];
  21. }
  22.  
  23. public static void main(String[] args) throws NumberFormatException,
  24. n = Integer.parseInt(bf.readLine());
  25. p = new double[n][n];
  26. for (int i = 0; i < n; i++) {
  27. StringTokenizer st = new StringTokenizer(bf.readLine());
  28. for (int j = 0; j < n; j++) {
  29. p[i][j] = Double.parseDouble(st.nextToken());
  30. }
  31. }
  32.  
  33. dp = new double[1 << n];
  34.  
  35. int limit = (1 << n);
  36. dp[limit - 1] = 1.;
  37.  
  38. int[] bitPairs = new int[n + 1];
  39. for (int bits = 0; bits <= n; ++bits) {
  40. bitPairs[bits] = bits * (bits + 1);
  41. bitPairs[bits] >>= 1;
  42. }
  43.  
  44. for (int mask = limit - 2; mask >= 0; --mask) {
  45. int bits = Integer.bitCount(mask);
  46. int pairs = bitPairs[bits];
  47.  
  48. for (int i = 0; i < n; i++) {
  49. if ((mask & (1 << i)) == 0) continue;
  50. for (int j = 0; j < n; j++) {
  51. if ((mask & (1 << j)) == 0)
  52. dp[mask] += dp[mask | (1 << j)] * p[i][j];
  53. }
  54. }
  55. dp[mask] /= pairs;
  56. }
  57.  
  58. for (int i = 0; i < n - 1; i++) {
  59. System.out.print(dp[1 << i] + " ");
  60. }
  61.  
  62. System.out.println(dp[1 << (n - 1)]);
  63.  
  64. }
  65. }
Success #stdin #stdout 0.1s 320256KB
stdin
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
stdout
1.0000000000000002 0.0 0.0 0.0 0.0