static public long Choose(int n, int k, bool isSorted, bool isUnique) { long result = 1; if (!isSorted && !isUnique) { for (int i = 0; i < k; i++) { result *= n; } } if (!isSorted && isUnique) { result = Combinatorial.C(n, k) * Combinatorial.Factorial(k); } if (isSorted && isUnique) { result = Combinatorial.C(n, k); } if (isSorted && !isUnique) { result = Combinatorial.C(n + k - 1, k); } return(result); }
public long count(string n) { long result = 0; int[] digits = new int[10]; for (int i = 0; i < n.Length; i++) { digits[n[i] - '0']++; } while (n != "") { for (int i = 0; i < n[0] - '0'; i++) { if (digits[i] > 0) { int k = n.Length - 1; long x = 1; digits[i]--; for (int j = 0; j < 10; j++) { x *= Combinatorial.C(k, digits[j]); k -= digits[j]; } digits[i]++; result += x; } } digits[n[0] - '0']--; n = n.Substring(1); } return(result); }