📌  相关文章
📜  求P的值和Q模998244353的模逆

📅  最后修改于: 2021-06-26 21:38:12             🧑  作者: Mango

给定两个整数PQ,任务是找到P的值和Q模998244353的Q的模逆。

P * Q^{-1} \% 998244353

注意: P和Q是互素整数
例子:

方法:问题中的关键发现是Q与998244353互质,那么Q -1始终存在,可以使用扩展欧几里德算法进行计算

例如:

下面是上述方法的实现:

C++
// C++ implementation to find the
// value of P.Q -1 mod 998244353
 
#include 
using namespace std;
 
// Function to find the value of
// P * Q^-1 mod 998244353
long long calculate(long long p,
                    long long q)
{
    long long mod = 998244353, expo;
    expo = mod - 2;
 
    // Loop to find the value
    // until the expo is not zero
    while (expo) {
 
        // Multiply p with q
        // if expo is odd
        if (expo & 1) {
            p = (p * q) % mod;
        }
        q = (q * q) % mod;
 
        // Reduce the value of
        // expo by 2
        expo >>= 1;
    }
    return p;
}
 
// Driver code
int main()
{
    int p = 1, q = 4;
 
    // Function Call
    cout << calculate(p, q) << '\n';
    return 0;
}


Java
// Java implementation to find the
// value of P.Q -1 mod 998244353
import java.util.*;
 
class GFG{
 
// Function to find the value of
// P * Q^-1 mod 998244353
static long calculate(long p, long q)
{
    long mod = 998244353, expo;
    expo = mod - 2;
 
    // Loop to find the value
    // until the expo is not zero
    while (expo != 0)
    {
 
        // Multiply p with q
        // if expo is odd
        if ((expo & 1) == 1)
        {
            p = (p * q) % mod;
        }
        q = (q * q) % mod;
 
        // Reduce the value of
        // expo by 2
        expo >>= 1;
    }
    return p;
}
 
// Driver code
public static void main(String[] args)
{
    long p = 1, q = 4;
     
    // Function call
    System.out.println(calculate(p, q));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 implementation to find the
# value of P.Q -1 mod 998244353
 
# Function to find the value of
# P * Q^-1 mod 998244353
def calculate(p, q):
     
    mod = 998244353
    expo = 0
    expo = mod - 2
 
    # Loop to find the value
    # until the expo is not zero
    while (expo):
 
        # Multiply p with q
        # if expo is odd
        if (expo & 1):
            p = (p * q) % mod
        q = (q * q) % mod
 
        # Reduce the value of
        # expo by 2
        expo >>= 1
 
    return p
 
# Driver code
if __name__ == '__main__':
     
    p = 1
    q = 4
 
    # Function call
    print(calculate(p, q))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation to find the
// value of P.Q -1 mod 998244353
using System;
class GFG{
  
// Function to find the value of
// P * Q^-1 mod 998244353
static long calculate(long p, long q)
{
    long mod = 998244353, expo;
    expo = mod - 2;
  
    // Loop to find the value
    // until the expo is not zero
    while (expo != 0)
    {
  
        // Multiply p with q
        // if expo is odd
        if ((expo & 1) == 1)
        {
            p = (p * q) % mod;
        }
        q = (q * q) % mod;
  
        // Reduce the value of
        // expo by 2
        expo >>= 1;
    }
    return p;
}
  
// Driver code
public static void Main(string[] args)
{
    long p = 1, q = 4;
      
    // Function call
    Console.WriteLine(calculate(p, q));
}
}
  
// This code is contributed by Ritik Bansal


输出
748683265