fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. int N, M;
  9. cin >> N >> M;
  10.  
  11. // Read the favorite burgers of all people
  12. vector<int> fav(N);
  13. for (int i = 0; i < N; ++i) {
  14. cin >> fav[i];
  15. }
  16.  
  17. int joshFav = fav[N-1]; // Josh's favorite burger (the last person's favorite)
  18.  
  19. // Count how many people before Josh have the same favorite burger as Josh
  20. int countJoshFavBefore = 0;
  21. for (int i = 0; i < N - 1; ++i) {
  22. if (fav[i] == joshFav) {
  23. countJoshFavBefore++;
  24. }
  25. }
  26.  
  27. // Initial probability that Josh's burger survives the coach's pick
  28. double probability = 1.0;
  29. probability *= (double)(M - 1) / M; // Chance that the coach doesn't pick Josh's burger
  30.  
  31. // There are M-1 burgers left after the coach picks
  32. int remainingBurgers = M - 1;
  33.  
  34. // For every person who shares Josh's favorite burger, update the probability
  35. for (int i = 0; i < N - 1; ++i) {
  36. if (fav[i] == joshFav) {
  37. probability *= (double)(remainingBurgers - 1) / remainingBurgers;
  38. remainingBurgers--;
  39. } else {
  40. // If they don't care about Josh's burger, just reduce the number of burgers left
  41. remainingBurgers--;
  42. }
  43. }
  44.  
  45. // Output the final probability with six decimal precision
  46. cout << fixed << setprecision(6) << probability << endl;
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0.01s 5280KB
stdin
3
1 2 3
stdout
0.000000