static long distinctCount(IEnumerable <long> source, long length, long remaining, long skippable) { if (skippable < 0L) { return(0L); } if (skippable == 0L) { return(Combinatoric.Multinomial(source)); } var count = source.First(); if (count >= length) { return(remaining.Power((int)length)); } var others = source.Skip(1); var result = distinctCount(others, length, remaining - 1L, skippable - count); for (var x = 1L; x <= count; x++) { var temp = distinctCount(others, length - x, remaining - 1L, skippable + x - count); result = checked (result + Combinatoric.Choose(length, x) * temp); } return(result); }
public static void Multinomial() { //this example shows how to get multinomial //use Combinatoric.Multinomial(collection_of_x) to get multinomial Console.WriteLine(Combinatoric.Multinomial(new[] { 1, 4, 4, 2 })); }