📌  相关文章
📜  查找(a ^ b)%m,其中“ b”非常大

📅  最后修改于: 2021-04-26 07:13:02             🧑  作者: Mango

给定三个数字a,b和m,其中1 <= a,m <= 10 ^ 6。给定非常大的’b’,最多包含10 ^ 6个数字,并且m是质数,任务是找到(a ^ b)%m。
例子:

方法:根据费马小定理,

a^(p-1) mod p = 1, When p is prime.

由此,从问题上来说,M为质数,表示A ^ B mod M如下:

A^B mod M = ( A^(M-1) * A^(M-1) *.......* A^(M-1) * A^(x) ) mod M

其中x是B mod M-1并且A ^(M-1)连续B /(M-1)次
现在,根据费马小定理,

A ^ (M-1) mod M = 1.

因此,

A^B mod M = ( 1 * 1 * ....... * 1 * A^(x) ) mod M

因此,使用M-1的mod B将数量减少为一个较小的数字,然后使用power()方法计算(a ^ b)%m。
下面是上述方法的实现:

C++
// C++ program to find
// (a^b)%m for b very large.
#include 
#define ll long long int
using namespace std;
 
// Function to find power
ll power(ll x, ll y, ll p)
{
    ll res = 1; // Initialize result
 
    // Update x if it is more than or
    // equal to p
    x = x % p;
 
    while (y > 0) {
       
        // If y is odd, multiply x
        // with the result
        if (y & 1)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
// Driver Code
int main()
{
    ll a = 3;
 
    // String input as b is very large
    string b = "100000000000000000000000000";
 
    ll remainderB = 0;
    ll MOD = 1000000007;
 
    // Reduce the number B to a small number
    // using Fermat Little
    for (int i = 0; i < b.length(); i++)
        remainderB = (remainderB * 10 +
                       b[i] - '0') % (MOD - 1);
 
    cout << power(a, remainderB, MOD) << endl;
    return 0;
}


Java
// Java program to find
// (a^b)%m for b very large.
import java.io.*;
 
class GFG
{
     
// Function to find power
static long power(long x,
                  long y, long p)
{
    long res = 1; // Initialize result
 
    // Update x if it is more
    // than or equal to p
    x = x % p;
 
    while (y > 0)
    {
        // If y is odd, multiply
        // x with the result
        if ((y & 1) > 0)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Driver Code
public static void main (String[] args)
{
long a = 3;
 
// String input as
// b is very large
String b = "100000000000000000000000000";
 
long remainderB = 0;
long MOD = 1000000007;
 
// Reduce the number B to a small
// number using Fermat Little
for (int i = 0; i < b.length(); i++)
    remainderB = (remainderB * 10 +
                  b.charAt(i) - '0') %
                 (MOD - 1);
 
System.out.println(power(a, remainderB, MOD));
}
}
 
// This code is contributed by anuj_67.


Python3
# Python3 program to find
# (a^b)%m for b very large.
 
# Function to find power
def power(x, y, p):
    res = 1 # Initialize result
 
    # Update x if it is
    # more than or equal to p
    x = x % p
 
    while (y > 0):
         
        # If y is odd, multiply
        # x with the result
        if (y & 1):
            res = (res * x) % p
 
        # y must be even now
        y = y >> 1 # y = y/2
        x = (x * x) % p
         
    return res
 
# Driver Code
a = 3
 
# String input as b
# is very large
b = "100000000000000000000000000"
 
remainderB = 0
MOD = 1000000007
 
# Reduce the number B
# to a small number
# using Fermat Little
for i in range(len(b)):
    remainderB = ((remainderB * 10 +
                   ord(b[i]) - 48) %
                   (MOD - 1))
 
print(power(a, remainderB, MOD))
 
# This code is contributed by mits


C#
// C# program to find
// (a^b)%m for b very large.
using System;
 
class GFG
{
     
// Function to find power
static long power(long x,
                  long y, long p)
{
    // Initialize result
    long res = 1;
 
    // Update x if it is more
    // than or equal to p
    x = x % p;
 
    while (y > 0)
    {
        // If y is odd, multiply
        // x with the result
        if ((y & 1) > 0)
            res = (res * x) % p;
 
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
 
// Driver Code
public static void Main ()
{
    long a = 3;
     
    // String input as
    // b is very large
    string b = "100000000000000000000000000";
     
    long remainderB = 0;
    long MOD = 1000000007;
     
    // Reduce the number B to
    // a small number using
    // Fermat Little
    for (int i = 0; i < b.Length; i++)
        remainderB = (remainderB * 10 +
                          b[i] - '0') %
                             (MOD - 1);
     
    Console.WriteLine(power(a, remainderB, MOD));
}
}
 
// This code is contributed by anuj_67.


PHP
 0)
    {
        // If y is odd, multiply
        // x with the result
        if ($y & 1)
            $res = ($res * $x) % $p;
 
        // y must be even now
        $y = $y >> 1; // y = y/2
        $x = ($x * $x) % $p;
    }
    return $res;
}
 
// Driver Code
$a = 3;
 
// String input as b
// is very large
$b = "100000000000000000000000000";
 
$remainderB = 0;
$MOD = 1000000007;
 
// Reduce the number B
// to a small number
// using Fermat Little
for ($i = 0; $i < strlen($b); $i++)
    $remainderB = ($remainderB * 10 +
                   $b[$i] - '0') %
                  ($MOD - 1);
 
echo power($a, $remainderB, $MOD);
 
// This code is contributed by mits
?>


输出:
835987331