ruhan008

ncrMod

Sep 29th, 2025
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. {
  2. "": {
  3. "prefix": "cd_ncrMod",
  4. "body": [
  5. "// -----------------------------------------------------------------------------------------------",
  6. "const int N = 2e5 + 10;",
  7. "vector<int> fact(N, 1);",
  8. "",
  9. "int binExp(int a, int b, int m){",
  10. " ",
  11. " int res = 1;",
  12. " ",
  13. " while(b){",
  14. " if(b & 1) res = (res * 1LL * a) % m;",
  15. " a = (a * 1LL * a) % m;",
  16. " b = b >> 1;",
  17. " }",
  18. " ",
  19. " return res;",
  20. "}",
  21. " ",
  22. "int modInverse(int a, int m){",
  23. " ",
  24. " return binExp(a, m - 2, m);",
  25. "}",
  26. "",
  27. "// call calculateFactorial() in main",
  28. "void calculateFactorial(int m){",
  29. " ",
  30. " for(int i = 1; i < N; i++){",
  31. " fact[i] = (fact[i - 1] * 1LL * i) % m;",
  32. " }",
  33. " ",
  34. "}",
  35. " ",
  36. "int nCrMod(int n, int r, int m){",
  37. " ",
  38. " if(r > n) return 0;",
  39. " ",
  40. " return (fact[n] * 1LL * modInverse((fact[n - r] * 1LL * fact[r]) % m, m)) % m;",
  41. "}",
  42. "// -----------------------------------------------------------------------------------------------"
  43. ],
  44. "description": ""
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment