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.
Examples:
Input: A = 2, B = 6
Output: 333333336
Input: A = 4, B = 5
Output: 600000005
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:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define m 1000000007
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
ll modexp(ll x, ll n)
{
if (n == 0) {
return 1;
}
else if (n % 2 == 0) {
return modexp((x * x) % m, n / 2);
}
else {
return (x * modexp((x * x) % m, (n - 1) / 2) % m);
}
}
ll getFractionModulo(ll a, ll b)
{
ll c = gcd(a, b);
a = a / c;
b = b / c;
ll d = modexp(b, m - 2);
ll ans = ((a % m) * (d % m)) % m;
return ans;
}
int main()
{
ll a = 2, b = 6;
cout << getFractionModulo(a, b) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static long m = 1000000007 ;
static long gcd( long a, long b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static long modexp( long x, long n)
{
if (n == 0 ) {
return 1 ;
}
else if (n % 2 == 0 ) {
return modexp((x * x) % m, n / 2 );
}
else {
return (x * modexp((x * x) % m, (n - 1 ) / 2 ) % m);
}
}
static long getFractionModulo( long a, long b)
{
long c = gcd(a, b);
a = a / c;
b = b / c;
long d = modexp(b, m - 2 );
long ans = ((a % m) * (d % m)) % m;
return ans;
}
public static void main (String[] args) {
long a = 2 , b = 6 ;
System.out.println(getFractionModulo(a, b));
}
}
|
Python3
m = 1000000007
def gcd(a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def modexp(x, n):
if (n = = 0 ) :
return 1
elif (n % 2 = = 0 ) :
return modexp((x * x) % m, n / / 2 )
else :
return (x * modexp((x * x) % m,
(n - 1 ) / 2 ) % m)
def getFractionModulo(a, b):
c = gcd(a, b)
a = a / / c
b = b / / c
d = modexp(b, m - 2 )
ans = ((a % m) * (d % m)) % m
return ans
if __name__ = = "__main__" :
a = 2
b = 6
print ( getFractionModulo(a, b))
|
C#
using System;
public class GFG{
static long m = 1000000007;
static long gcd( long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long modexp( long x, long n)
{
if (n == 0) {
return 1;
}
else if (n % 2 == 0) {
return modexp((x * x) % m, n / 2);
}
else {
return (x * modexp((x * x) % m, (n - 1) / 2) % m);
}
}
static long getFractionModulo( long a, long b)
{
long c = gcd(a, b);
a = a / c;
b = b / c;
long d = modexp(b, m - 2);
long ans = ((a % m) * (d % m)) % m;
return ans;
}
static public void Main (){
long a = 2, b = 6;
Console.WriteLine(getFractionModulo(a, b));
}
}
|
PHP
<?php
function abc( $a , $b )
{
if ( $a == 0)
return $b ;
return abc( $b % $a , $a );
}
function modexp( $x , $n )
{
$m = 1000000007;
if ( $n == 0)
{
return 1;
}
else if ( $n % 2 == 0)
{
return modexp(( $x * $x ) % $m , $n / 2);
}
else
{
return ( $x * modexp(( $x * $x ) % $m ,
( $n - 1) / 2) % $m );
}
}
function getFractionModulo( $a , $b )
{
$m = 1000000007;
$c = abc( $a , $b );
$a = $a / $c ;
$b = $b / $c ;
$d = modexp( $b , $m - 2);
$ans = (( $a % $m ) * ( $d % $m )) % $m ;
return $ans ;
}
$a = 2;
$b = 6;
echo (getFractionModulo( $a , $b )) ;
?>
|
Javascript
<script>
var m = 100000007;
function gcd(a , b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
function modexp(x , n) {
if (n == 0) {
return 1;
} else if (n % 2 == 0) {
return modexp((x * x) % m, n / 2);
} else {
return (x * modexp((x * x) % m, (n - 1) / 2) % m);
}
}
function getFractionModulo(a , b) {
var c = gcd(a, b);
a = a / c;
b = b / c;
var d = modexp(b, m - 2);
var ans = ((a % m) * (d % m)) % m;
return ans;
}
var a = 2, b = 6;
document.write(getFractionModulo(a, b));
</script>
|
Time Complexity: O(log(min(a, b) + logm)
Auxiliary Space: O(log(min(a, b) + logm)
Approach#2: Using fermat’s little
This approach computes the value of (A/B) % m using Fermat’s Little Theorem to calculate the modular inverse of B.
Algorithm
1. Define a function inverse_modulo(a, m) to compute the inverse of a modulo m using Fermat’s Little Theorem.
2. Define a function fraction_to_natural_modulo(A, B, m) to compute the value of (A/B) % m, where A, B, and m are given integers.
3. Inside the fraction_to_natural_modulo function, calculate the inverse of B modulo m using the inverse_modulo function.
4. Multiply A and the inverse of B modulo m, and take modulo m to get the result.
5. Return the result
Python3
def inverse_modulo(a, m):
return pow (a, m - 2 , m)
def fraction_to_natural_modulo(A, B, m):
inverse_B = inverse_modulo(B, m)
result = (A * inverse_B) % m
return result
A = 2
B = 6
m = 1000000007
natural_number = fraction_to_natural_modulo(A, B, m)
print (natural_number)
|
Time complexity:
The time complexity of this code is O(log m) because it uses the pow() function to compute the inverse modulo.
Auxiliary Space:
The space complexity of this code is O(1) because it only stores a few integers in memory.