📜  将小数表示为模m的自然数

📅  最后修改于: 2021-05-31 20:31:09             🧑  作者: Mango

给定两个整数AB ,其中A不能被B整除,任务是将A / B表示为自然数模m ,其中m = 1000000007
注意:在需要表达事件的概率,曲线面积和多边形等的情况下,此表示很有用。

例子:

方法:我们知道, A / B可以写成A *(1 / B),A *(B ^ -1)

已知模(%)运算符满足以下关系:

(a * b) % m = ( (a % m) * (b % m) ) % m

因此,我们可以这样写:

(b ^ -1) % m = (b ^ m-2) % m (Fermat's little theorem)

因此结果将是:

( (A mod m) * ( power(B, m-2) % m) ) % m

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long int
#define m 1000000007
  
// Function to return the GCD of given numbers
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
  
// Recursive function to return (x ^ n) % m
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);
    }
}
  
// Function to return the fraction modulo mod
ll getFractionModulo(ll a, ll b)
{
    ll c = gcd(a, b);
  
    a = a / c;
    b = b / c;
  
    // (b ^ m-2) % m
    ll d = modexp(b, m - 2);
  
    // Final answer
    ll ans = ((a % m) * (d % m)) % m;
  
    return ans;
}
  
// Driver code
int main()
{
    ll a = 2, b = 6;
  
    cout << getFractionModulo(a, b) << endl;
  
    return 0;
}


Java
// Java implementation of the approach
  
import java.io.*;
  
class GFG {
      
  
  
static long m  = 1000000007;
  
// Function to return the GCD of given numbers
 static long gcd(long a, long b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
  
// Recursive function to return (x ^ n) % m
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);
    }
}
  
// Function to return the fraction modulo mod
 static long getFractionModulo(long a, long b)
{
    long c = gcd(a, b);
  
    a = a / c;
    b = b / c;
  
    // (b ^ m-2) % m
    long  d = modexp(b, m - 2);
  
    // Final answer
    long ans = ((a % m) * (d % m)) % m;
  
    return ans;
}
  
// Driver code
  
    public static void main (String[] args) {
        long a = 2, b = 6;
  
    System.out.println(getFractionModulo(a, b));
    }
}
// This code is contributed by inder_verma


Python3
# Python3 implementation of the approach
m = 1000000007
  
# Function to return the GCD 
# of given numbers
def gcd(a, b):
  
    if (a == 0):
        return b
    return gcd(b % a, a)
  
# Recursive function to return (x ^ n) % m
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)
  
  
# Function to return the fraction modulo mod
def getFractionModulo(a, b):
  
    c = gcd(a, b)
  
    a = a // c
    b = b // c
  
    # (b ^ m-2) % m
    d = modexp(b, m - 2)
  
    # Final answer
    ans = ((a % m) * (d % m)) % m
  
    return ans
  
# Driver code
if __name__ == "__main__":
  
    a = 2
    b = 6
  
    print ( getFractionModulo(a, b))
  
# This code is contributed by ita_c


C#
//C#  implementation of the approach
  
using System;
  
public class GFG{
      
  
static long m = 1000000007;
  
// Function to return the GCD of given numbers
static long gcd(long a, long b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
  
// Recursive function to return (x ^ n) % m
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);
    }
}
  
// Function to return the fraction modulo mod
static long getFractionModulo(long a, long b)
{
    long c = gcd(a, b);
  
    a = a / c;
    b = b / c;
  
    // (b ^ m-2) % m
    long d = modexp(b, m - 2);
  
    // Final answer
    long ans = ((a % m) * (d % m)) % m;
  
    return ans;
}
  
// Driver code
      
    static public void Main (){
          
        long a = 2, b = 6;
        Console.WriteLine(getFractionModulo(a, b));
    }
}


PHP


输出:
333333336

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。