Given three integers N, R and P where P is prime, the task is to find whether NCR is divisible by P or not.
Input: N = 6, R = 2, P = 7
6C2 = 15 which is not divisible by 7.
Input: N = 7, R = 2, P = 3
7C2 = 21 which is divisible by 3.
Approach: We know that NCR = N! / (R! * (N – R)!). Now using Legendre Formula, find the largest power of P which divides any N!, R! and (N -R)! say x1, x2 and x3 respectively.
In order for NCR to be divisible by P, the condition x1 > x2 + x3 must be satisfied.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find a pair from the given array with maximum nCr value
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
- Compute nCr % p | Set 2 (Lucas Theorem)
- Program to calculate value of nCr
- Sum of product of r and rth Binomial Coefficient (r * nCr)
- Queries of nCr%p in O(1) time complexity
- Calculate nCr using Pascal's Triangle
- Compute nCr % p | Set 3 (Using Fermat Little Theorem)
- Program to calculate the value of nCr Efficiently
- Find permutation of n which is divisible by 3 but not divisible by 6
- Find prime factors of Array elements whose sum of exponents is divisible by K
- Count of numbers upto M divisible by given Prime Numbers
- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Quick ways to check for Prime and find next Prime in Java
- Find coordinates of a prime number in a Prime Spiral
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Count all prime numbers in a given range whose sum of digits is also prime
- Count numbers in a given range whose count of prime factors is a Prime Number
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.