Given two integers A and B where A is not divisible by B, the task is to express A / B as a natural number modulo m where m = 1000000007.
Note: This representation is useful where we need to express Probability of an event, Area of Curves and polygons etc.
Input: A = 2, B = 6
Input: A = 4, B = 5
Approach: We know that, A / B can be written as A * (1 / B) i.e. A * (B ^ -1).
It is known that the modulo(%) operator satisfies the relation:
(a * b) % m = ( (a % m) * (b % m) ) % m
So, we can write:
(b ^ -1) % m = (b ^ m-2) % m (Fermat's little theorem)
Therefore the result will be:
( (A mod m) * ( power(B, m-2) % m) ) % m
Below is the implementation of the above approach:
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Expressing a number as sum of consecutive | Set 2 (Using odd factors)
- Compute n! under modulo p
- Euler's criterion (Check if square root under modulo p exists)
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Multiply large integers under large modulo
- Equalizing array using increment under modulo 3
- Find Square Root under Modulo p | (When p is product of two primes in the form 4*i + 3)
- Expressing factorial n as sum of consecutive numbers
- Find sum of modulo K of first N natural number
- Sum of the natural numbers (up to N) whose modulo with K yield R
- Largest proper fraction with sum of numerator and denominator equal to a given number
- Maximum rational number (or fraction) from an array
- Convert given Decimal number into an irreducible Fraction
- Number of factors of very large number N modulo M where M is any prime number
- Find smallest number with given number of digits and sum of digits under given constraints
- Greedy Algorithm for Egyptian Fraction
- Find Recurring Sequence in a Fraction
- Convert Binary fraction to Decimal
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.