title: Compute Ncr P Set 1 Introduction And Dynamic Programming Solution
signature: class Solution {
  
      public int ComputeNcrPSet1IntroductionAndDynamicProgrammingSolution(int n, int r, int p) {
test_code: using System;
  using System.Numerics;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.Web.UI;
  using System.Web.Script.Serialization;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  namespace EvaluatePrediction {
      class TestRunner {
          public bool ValidateSolution(int actual, int expected){
              return new JavaScriptSerializer().Serialize(actual) == new JavaScriptSerializer().Serialize(expected);
          }
  
          public string driver(int n, int r, int p, int expected){
              try{
                PLACEHOLDER_CLS_NAME solution_cls = new PLACEHOLDER_CLS_NAME();
                int pred_result = solution_cls.PLACEHOLDER_FN_NAME(n, r, p); 
                if (ValidateSolution(
                      pred_result,
                      expected)){
                  return "PASSED";
                }
                return "FAILED";
              } 
              catch (Exception exception_obj)
              {
                return exception_obj.GetType().FullName;
              }
          }
          static void Main(string[] args) {
              TestRunner runner = new TestRunner();
              String result;
              result = runner.driver(
                  82, 5, 94, 
                  50
              );
              Console.WriteLine(string.Format(
                  "TEST-0...{0}",
                  result)
              );result = runner.driver(
                  45, 24, 95, 
                  80
              );
              Console.WriteLine(string.Format(
                  "TEST-1...{0}",
                  result)
              );result = runner.driver(
                  44, 68, 61, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-2...{0}",
                  result)
              );result = runner.driver(
                  88, 24, 43, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-3...{0}",
                  result)
              );result = runner.driver(
                  90, 75, 57, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-4...{0}",
                  result)
              );result = runner.driver(
                  98, 55, 43, 
                  2
              );
              Console.WriteLine(string.Format(
                  "TEST-5...{0}",
                  result)
              );result = runner.driver(
                  80, 54, 88, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-6...{0}",
                  result)
              );result = runner.driver(
                  60, 75, 65, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-7...{0}",
                  result)
              );result = runner.driver(
                  52, 73, 86, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-8...{0}",
                  result)
              );result = runner.driver(
                  71, 26, 45, 
                  12
              );
              Console.WriteLine(string.Format(
                  "TEST-9...{0}",
                  result)
              );
          }
      }
  }
  
source_py: def compute_ncr_p_set_1_introduction_and_dynamic_programming_solution(n, r, p):
      C = [0 for i in range((r + 1))]
      C[0] = 1
      for i in range(1, (n + 1)):
          for j in range(min(i, r), 0, (- 1)):
              C[j] = ((C[j] + C[(j - 1)]) % p)
      return C[r]
source_cpp: int compute_ncr_p_set_1_introduction_and_dynamic_programming_solution(int n, int r, int p) {
    int C [ r + 1 ];
    memset ( C, 0, sizeof ( C ) );
    C [ 0 ] = 1;
    for ( int i = 1;
    i <= n;
    i ++ ) {
      for ( int j = min ( i, r );
      j > 0;
      j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p;
    }
    return C [ r ];
  }