Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ManagedWorkspace
- {
- using System;
- using System.Diagnostics;
- internal static class Program
- {
- private static void Main(string[] args)
- {
- int total_number_of_raccoons = 61;
- int total_number_of_tagged_raccoons = 8;
- Debug.Assert(total_number_of_raccoons >= total_number_of_tagged_raccoons);
- int total_number_of_untagged_raccoons = total_number_of_raccoons - total_number_of_tagged_raccoons;
- // p[n, k] = probability that k raccoons are tagged when chosen n raccoons.
- double[,] p = new double[total_number_of_raccoons + 1, total_number_of_tagged_raccoons + 1];
- // Simple base cases.
- p[1, 0] = ((double)total_number_of_untagged_raccoons) / total_number_of_raccoons;
- p[1, 1] = ((double)total_number_of_tagged_raccoons) / total_number_of_raccoons;
- for (int n = 2; n <= total_number_of_raccoons; n++)
- {
- for (int k = 0; k <= Math.Min(n, total_number_of_tagged_raccoons); k++)
- {
- if (k == 0)
- {
- if (p[n - 1, k] > 0)
- {
- int number_of_raccoons_chosen = n - 1;
- int number_of_tagged_raccoons_chosen = k;
- int number_of_raccoons_remaining = total_number_of_raccoons - number_of_raccoons_chosen;
- int number_of_tagged_raccoons_remaining = total_number_of_tagged_raccoons - number_of_tagged_raccoons_chosen;
- int number_of_untagged_raccoons_chosen = number_of_raccoons_chosen - number_of_tagged_raccoons_chosen;
- int number_of_untagged_raccoons_remaining = total_number_of_untagged_raccoons - number_of_untagged_raccoons_chosen;
- Debug.Assert(number_of_raccoons_remaining >= 0);
- Debug.Assert(number_of_tagged_raccoons_remaining >= 0);
- Debug.Assert(number_of_untagged_raccoons_chosen >= 0);
- Debug.Assert(number_of_untagged_raccoons_remaining >= 0);
- p[n, k] = p[n - 1, k] * ((double)number_of_untagged_raccoons_remaining) / number_of_raccoons_remaining;
- }
- }
- else if (k < n)
- {
- double A = 0;
- double B = 0;
- if (p[n - 1, k - 1]> 0)
- {
- int number_of_raccoons_chosen = n - 1;
- int number_of_tagged_raccoons_chosen = k - 1;
- int number_of_raccoons_remaining = total_number_of_raccoons - number_of_raccoons_chosen;
- int number_of_tagged_raccoons_remaining = total_number_of_tagged_raccoons - number_of_tagged_raccoons_chosen;
- int number_of_untagged_raccoons_chosen = number_of_raccoons_chosen - number_of_tagged_raccoons_chosen;
- int number_of_untagged_raccoons_remaining = total_number_of_untagged_raccoons - number_of_untagged_raccoons_chosen;
- Debug.Assert(number_of_raccoons_remaining >= 0);
- Debug.Assert(number_of_tagged_raccoons_remaining >= 0);
- Debug.Assert(number_of_untagged_raccoons_chosen >= 0);
- Debug.Assert(number_of_untagged_raccoons_remaining >= 0);
- A = ((double)number_of_tagged_raccoons_remaining) / number_of_raccoons_remaining;
- }
- if (p[n - 1, k] > 0)
- {
- int number_of_raccoons_chosen = n - 1;
- int number_of_tagged_raccoons_chosen = k;
- int number_of_raccoons_remaining = total_number_of_raccoons - number_of_raccoons_chosen;
- int number_of_tagged_raccoons_remaining = total_number_of_tagged_raccoons - number_of_tagged_raccoons_chosen;
- int number_of_untagged_raccoons_chosen = number_of_raccoons_chosen - number_of_tagged_raccoons_chosen;
- int number_of_untagged_raccoons_remaining = total_number_of_untagged_raccoons - number_of_untagged_raccoons_chosen;
- Debug.Assert(number_of_raccoons_remaining >= 0);
- Debug.Assert(number_of_tagged_raccoons_remaining >= 0);
- Debug.Assert(number_of_untagged_raccoons_chosen >= 0);
- Debug.Assert(number_of_untagged_raccoons_remaining >= 0);
- B = ((double)number_of_untagged_raccoons_remaining) / number_of_raccoons_remaining;
- }
- p[n, k] = p[n - 1, k - 1] * A + p[n - 1, k] * B;
- }
- else
- {
- double A = 0;
- if (p[n - 1, k - 1] > 0)
- {
- int number_of_raccoons_chosen = n - 1;
- int number_of_tagged_raccoons_chosen = k - 1;
- int number_of_raccoons_remaining = total_number_of_raccoons - number_of_raccoons_chosen;
- int number_of_tagged_raccoons_remaining = total_number_of_tagged_raccoons - number_of_tagged_raccoons_chosen;
- int number_of_untagged_raccoons_chosen = number_of_raccoons_chosen - number_of_tagged_raccoons_chosen;
- int number_of_untagged_raccoons_remaining = total_number_of_untagged_raccoons - number_of_untagged_raccoons_chosen;
- Debug.Assert(number_of_raccoons_remaining >= 0);
- Debug.Assert(number_of_tagged_raccoons_remaining >= 0);
- Debug.Assert(number_of_untagged_raccoons_chosen >= 0);
- Debug.Assert(number_of_untagged_raccoons_remaining >= 0);
- A = ((double)number_of_tagged_raccoons_remaining) / number_of_raccoons_remaining;
- }
- p[n, k] = p[n - 1, k - 1] * A;
- }
- }
- }
- Console.WriteLine(p[20, 4]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment