CSharp · Console
using System;
using System.Numerics;
public class Program
{
public static BigInteger Cnk(BigInteger n, BigInteger k)
{
BigInteger one = BigInteger.One;
BigInteger result = one;
BigInteger subtract = BigInteger.Subtract(n, k);
if (k.CompareTo(subtract) > 0)
{
k = subtract;
}
BigInteger i = one;
while (i.CompareTo(k) <= 0)
{
var nMinusI = BigInteger.Subtract(n, i);
var nMinusIPlustOne = BigInteger.Add(nMinusI, one);
var multiply = BigInteger.Multiply(result, nMinusIPlustOne);
result = BigInteger.Divide(multiply, i);
i = BigInteger.Add(i, one);
}
return result;
}
public static BigInteger GetCountOfLocks(int N, int M)
{
return Cnk(N, M - 1);
}
public static void Main()
{
Console.WriteLine("UniLecs");
Console.WriteLine(string.Format("Answer = {0}", GetCountOfLocks(3, 2))); // 3
Console.WriteLine(string.Format("Answer = {0}", GetCountOfLocks(5, 1))); // 1
Console.WriteLine(string.Format("Answer = {0}", GetCountOfLocks(10, 7))); // 210
Console.WriteLine(string.Format("Answer = {0}", GetCountOfLocks(5, 3))); // 10
}
}