Módulo de potencia para grandes números representados como strings

Dados dos números sa y sb representados como strings, encuentre a b % MOD donde MOD es 1e9 + 7. Los números a y b pueden contener hasta 10 6 dígitos.
Ejemplos: 
 

Entrada: sa = 2, sb = 3 
Salida: 8
Entrada: sa = 10000000000000000000000000000000000000000000 
sb = 10000000000000000000000000000000000000000000 
Salida: 4 546233

Como a y b muy grandes (pueden contener hasta 10^6 dígitos cada uno). Entonces, lo que podemos hacer, aplicamos el pequeño teorema de Fermat y la propiedad del módulo para reducir a y b
Reducir a: 
Como sabemos, 

(ab) % MOD = ((a % MOD)b) % MOD

Reducir b: 
cómo reducir b , ya lo hemos discutido en Find (a^b)%m donde ‘b’ es muy grande
. Ahora finalmente tenemos que tanto a como b están en el rango de 1<=a, b<=10^ 9+7. Por lo tanto, ahora podemos usar nuestra exponenciación modular para calcular la respuesta requerida. 
 

C++

// CPP program to find (a^b) % MOD where a and
// b may be very large and represented as strings.
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long int
const ll MOD = 1e9 + 7;
 
// Returns modulo exponentiation for two numbers
// represented as long long int. It is used by
// powerStrings(). Its complexity is log(n)
ll powerLL(ll x, ll n)
{
    ll result = 1;
    while (n) {
        if (n & 1)
            result = result * x % MOD;
        n = n / 2;
        x = x * x % MOD;
    }
    return result;
}
 
// Returns modulo exponentiation for two numbers
// represented as strings. It is used by
// powerStrings()
ll powerStrings(string sa, string sb)
{
    // We convert strings to number
 
    ll a = 0, b = 0;
 
    // calculating  a % MOD
    for (int i = 0; i < sa.length(); i++)
        a = (a * 10 + (sa[i] - '0')) % MOD;
 
    // calculating  b % (MOD - 1)
    for (int i = 0; i < sb.length(); i++)
        b = (b * 10 + (sb[i] - '0')) % (MOD - 1);
 
    // Now a and b are long long int. We
    // calculate a^b using modulo exponentiation
    return powerLL(a, b);
}
 
int main()
{
    // As numbers are very large
    // that is it may contains upto
    // 10^6 digits. So, we use string.
    string sa = "2", sb = "3";
 
    cout << powerStrings(sa, sb) << endl;
    return 0;
}

Java

// Java program to find (a^b) % MOD
// where a and b may be very large
// and represented as strings.
import java.util.*;
 
class GFG
{
 
    static long MOD = (long) (1e9 + 7);
 
    // Returns modulo exponentiation for two numbers
    // represented as long long int. It is used by
    // powerStrings(). Its complexity is log(n)
    static long powerLL(long x, long n)
    {
        long result = 1;
        while (n > 0)
        {
            if (n % 2 == 1)
            {
                result = result * x % MOD;
            }
            n = n / 2;
            x = x * x % MOD;
        }
        return result;
    }
 
    // Returns modulo exponentiation for
    // two numbers  represented as strings. 
    // It is used by powerStrings()
    static long powerStrings(String sa, String sb)
    {
        // We convert strings to number
        long a = 0, b = 0;
 
        // calculating a % MOD
        for (int i = 0; i < sa.length(); i++)
        {
            a = (a * 10 + (sa.charAt(i) - '0')) %
                                               MOD;
        }
 
        // calculating b % (MOD - 1)
        for (int i = 0; i < sb.length(); i++)
        {
            b = (b * 10 + (sb.charAt(i) - '0')) %
                                        (MOD - 1);
        }
 
        // Now a and b are long long int. We
        // calculate a^b using modulo exponentiation
        return powerLL(a, b);
    }
 
    // Driver code
    public static void main(String[] args)
    {
         
        // As numbers are very large
        // that is it may contains upto
        // 10^6 digits. So, we use string.
        String sa = "2", sb = "3";
        System.out.println(powerStrings(sa, sb));
    }
}
 
// This code is contributed by Rajput-JI

Python3

# Python3 program to find (a^b) % MOD
# where a and b may be very large
# and represented as strings.
MOD = 1000000007;
 
# Returns modulo exponentiation
# for two numbers represented as
# long long int. It is used by
# powerStrings(). Its complexity
# is log(n)
def powerLL(x, n):
 
    result = 1;
    while (n):
        if (n & 1):
            result = result * x % MOD;
        n = int(n / 2);
        x = x * x % MOD;
    return result;
 
# Returns modulo exponentiation
# for two numbers represented as
# strings. It is used by powerStrings()
def powerStrings(sa, sb):
     
    # We convert strings to number
    a = 0;
    b = 0;
 
    # calculating a % MOD
    for i in range(len(sa)):
        a = (a * 10 + (ord(sa[i]) -
                       ord('0'))) % MOD;
 
    # calculating b % (MOD - 1)
    for i in range(len(sb)):
        b = (b * 10 + (ord(sb[i]) -
                       ord('0'))) % (MOD - 1);
 
    # Now a and b are long long int.
    # We calculate a^b using modulo
    # exponentiation
    return powerLL(a, b);
 
# Driver code
 
# As numbers are very large
# that is it may contains upto
# 10^6 digits. So, we use string.
sa = "2";
sb = "3";
 
print(powerStrings(sa, sb));
     
# This code is contributed by mits

C#

// C# program to find (a^b) % MOD where a and b 
// may be very large and represented as strings.
using System;
 
class GFG
{
    static long MOD = (long) (1e9 + 7);
     
    // Returns modulo exponentiation for two numbers
    // represented as long long int. It is used by
    // powerStrings(). Its complexity is log(n)
    static long powerLL(long x, long n)
    {
        long result = 1;
        while (n > 0)
        {
            if (n % 2 == 1)
            {
                result = result * x % MOD;
            }
            n = n / 2;
            x = x * x % MOD;
        }
        return result;
    }
     
    // Returns modulo exponentiation for
    // two numbers represented as strings.
    // It is used by powerStrings()
    static long powerStrings(String sa, String sb)
    {
        // We convert strings to number
        long a = 0, b = 0;
     
        // calculating a % MOD
        for (int i = 0; i < sa.Length; i++)
        {
            a = (a * 10 + (sa[i] - '0')) % MOD;
        }
     
        // calculating b % (MOD - 1)
        for (int i = 0; i < sb.Length; i++)
        {
            b = (b * 10 + (sb[i] - '0')) % (MOD - 1);
        }
     
        // Now a and b are long long int. We
        // calculate a^b using modulo exponentiation
        return powerLL(a, b);
    }
     
    // Driver code
    public static void Main(String[] args)
    {
         
        // As numbers are very large
        // that is it may contains upto
        // 10^6 digits. So, we use string.
        String sa = "2", sb = "3";
        Console.WriteLine(powerStrings(sa, sb));
    }
}
     
// This code is contributed by 29AjayKumar

PHP

<?php
// PHP program to find (a^b) % MOD
// where a and b may be very large
// and represented as strings.
$MOD = 1000000007;
 
// Returns modulo exponentiation
// for two numbers represented as
// long long int. It is used by
// powerStrings(). Its complexity
// is log(n)
function powerLL($x, $n)
{
    global $MOD;
    $result = 1;
    while ($n)
    {
        if ($n & 1)
            $result = $result * $x % $MOD;
        $n = (int)$n / 2;
        $x = $x * $x % $MOD;
    }
    return $result;
}
 
// Returns modulo exponentiation
// for two numbers represented as
// strings. It is used by powerStrings()
function powerStrings($sa, $sb)
{
    global $MOD;
     
    // We convert strings to number
    $a = 0;
    $b = 0;
 
    // calculating a % MOD
    for ($i = 0; $i < strlen($sa); $i++)
        $a = ($a * 10 + ($sa[$i] -
                     '0')) % $MOD;
 
    // calculating b % (MOD - 1)
    for ($i = 0; $i < strlen($sb); $i++)
        $b = ($b * 10 + ($sb[$i] - '0')) %
                        ($MOD - 1);
 
    // Now a and b are long long int.
    // We calculate a^b using modulo
    // exponentiation
    return powerLL($a, $b);
}
 
// Driver code
 
// As numbers are very large
// that is it may contains upto
// 10^6 digits. So, we use string.
$sa = "2";
$sb = "3";
 
echo powerStrings($sa, $sb);
     
// This code is contributed by mits
?>

Javascript

<script>
 
// Javascript program to find (a^b) % MOD where a and b 
// may be very large and represented as strings. 
 
let MOD = (1e9 + 7);
     
    // Returns modulo exponentiation for two numbers
    // represented as long long let. It is used by
    // powerStrings(). Its complexity is log(n)
    function powerLL(x, n)
    {
        let result = 1;
        while (n > 0)
        {
            if (n % 2 == 1)
            {
                result = result * x % MOD;
            }
            n = Math.floor(n / 2);
            x = x * x % MOD;
        }
        return result;
    }
     
    // Returns modulo exponentiation for
    // two numbers represented as strings.
    // It is used by powerStrings()
    function powerStrings(sa, sb)
    {
        // We convert strings to number
        let a = 0, b = 0;
     
        // calculating a % MOD
        for (let i = 0; i < sa.length; i++)
        {
            a = (a * 10 + (sa[i] - '0')) % MOD;
        }
     
        // calculating b % (MOD - 1)
        for (let i = 0; i < sb.length; i++)
        {
            b = (b * 10 + (sb[i] - '0')) % (MOD - 1);
        }
     
        // Now a and b are long long let. We
        // calculate a^b using modulo exponentiation
        return powerLL(a, b);
    }
 
// Driver Code
 
         // As numbers are very large
        // that is it may contains upto
        // 10^6 digits. So, we use string.
        let sa = "2", sb = "3";
        document.write(powerStrings(sa, sb));
            
</script>
Producción: 

8

 

Publicación traducida automáticamente

Artículo escrito por sahilshelangia y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *