GetNumberOfWays.cs

CSharp · Console

/Program.cs

using System;
using System.Numerics;
					
public class Program
{
	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;
	}
	
	static BigInteger GetNumberOfWays(int N, int T, int P)
	{
		T = T - N * P;
		BigInteger result = Cnk(T + N - 1, T);
		return result;
	}	
	
	public static void Main()
	{
    	Console.WriteLine("UniLecs");
		Console.WriteLine(string.Format("Answer = {0}", GetNumberOfWays(2, 5, 2)));   // 2
		Console.WriteLine(string.Format("Answer = {0}", GetNumberOfWays(3, 34, 10))); // 15
		
	}
}
;