Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. public class ChipRace {
  2.  
  3.     int[] chips;
  4.     double[] ans;
  5.     boolean[] v;
  6.     double p[][][];
  7.    
  8.     void solve(int player, int numCard, int total, double p){
  9.         if(numCard==0 || player == v.length) return;
  10.         solve(player+1, numCard, total, p);
  11.        
  12.         if(!v[player]){
  13.             v[player]=true;
  14.             ans[player] += p*chips[player]/total;
  15.             solve(0, numCard-1, total-chips[player], p*chips[player]/total);
  16.             v[player]=false;
  17.         }
  18.     }
  19.    
  20.     public double[] chances(int[] chips) {
  21.         int n = chips.length;
  22.         double[] ret = new double[n];
  23.         int total = 0;
  24.         for(int i=0; i<n; i++) total += chips[i];
  25.         int m = total/5;
  26.         if(total%5 >= 3) m++;
  27.        
  28.         if(m==0) return ret;
  29.  
  30.        
  31.         p = new double[m][n][total+1];
  32.         for(int i=0; i<n; i++) p[0][i][total] = chips[i]*1.0/total;
  33.            
  34.         for(int k=1; k<m; k++){
  35.  
  36.             for(int i=0; i<n; i++){
  37.                 if(chips[i]==0) continue;
  38.                 for(int j=0; j<n; j++){
  39.        
  40.                     if(i==j) continue;
  41.                     for(int t=chips[j]+chips[i]; t<=total; t++){
  42.                         p[k][i][t-chips[j]] += p[k-1][j][t] * chips[i]*1.0/(t-chips[j]);
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.        
  48.         for(int i=0; i<m; i++) for(int j=0; j<n; j++) for(int k=0; k<=total; k++) ret[j] += p[i][j][k];
  49.         return ret;
  50.        
  51. //      this.chips = chips;
  52. //      ans = new double[n];
  53. //      v = new boolean[n];    
  54. //      solve(0, m, total, 1);
  55. //      return ans;
  56.     }
  57.  
  58.     public static void main(String[] args) {
  59.         ChipRace c = new ChipRace();
  60. //      int[] input = new int[]{0, 1, 2, 3, 4};
  61.         int[] input = new int[]{4, 4, 4, 4, 4, 4, 4, 4, 4, 3};
  62. //      int[] input = new int[]{3, 4, 4, 4, 4, 4, 4, 4, 4, 4};
  63. //      int[] input = new int[]{2, 0, 1, 4, 3, 2, 3, 4, 1, 2};
  64. //      int[] input = new int[]{4, 4, 4, 4, 4};
  65.         double[] ret = c.chances(input);
  66.         for(int i=0; i<ret.length; i++) System.out.print(ret[i] + ", ");
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement