Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "": {
- "prefix": "cd_ncrMod",
- "body": [
- "// -----------------------------------------------------------------------------------------------",
- "const int N = 2e5 + 10;",
- "vector<int> fact(N, 1);",
- "",
- "int binExp(int a, int b, int m){",
- " ",
- " int res = 1;",
- " ",
- " while(b){",
- " if(b & 1) res = (res * 1LL * a) % m;",
- " a = (a * 1LL * a) % m;",
- " b = b >> 1;",
- " }",
- " ",
- " return res;",
- "}",
- " ",
- "int modInverse(int a, int m){",
- " ",
- " return binExp(a, m - 2, m);",
- "}",
- "",
- "// call calculateFactorial() in main",
- "void calculateFactorial(int m){",
- " ",
- " for(int i = 1; i < N; i++){",
- " fact[i] = (fact[i - 1] * 1LL * i) % m;",
- " }",
- " ",
- "}",
- " ",
- "int nCrMod(int n, int r, int m){",
- " ",
- " if(r > n) return 0;",
- " ",
- " return (fact[n] * 1LL * modInverse((fact[n - r] * 1LL * fact[r]) % m, m)) % m;",
- "}",
- "// -----------------------------------------------------------------------------------------------"
- ],
- "description": ""
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment