Given two numbers sa and sb represented as strings, find ab % MOD where MOD is 1e9 + 7. The numbers a and b can contain upto 106 digits.
Examples:
Input : sa = 2, sb = 3
Output : 8
Input : sa = 10000000000000000000000000000000000000000000
sb = 10000000000000000000000000000000000000000000
Output : 494234546
As a and b very large (may contain upto 10^6 digits each). So what we can do, we apply Fermat’s little theorem and property of modulo to reduce a and b.
Reduce a:
As we know,
(ab) % MOD = ((a % MOD)b) % MOD
Reduce b:
How to reduce b, We have already discuss in Find (a^b)%m where ‘b’ is very large
Now finally we have both a and b are in range of 1<=a, b<=10^9+7. Hence we can now use our modular exponentiation to calculate required answer.
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const ll MOD = 1e9 + 7;
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;
}
ll powerStrings(string sa, string sb)
{
ll a = 0, b = 0;
for (int i = 0; i < sa.length(); i++)
a = (a * 10 + (sa[i] - '0')) % MOD;
for (int i = 0; i < sb.length(); i++)
b = (b * 10 + (sb[i] - '0')) % (MOD - 1);
return powerLL(a, b);
}
int main()
{
string sa = "2", sb = "3";
cout << powerStrings(sa, sb) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static long MOD = (long) (1e9 + 7);
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;
}
static long powerStrings(String sa, String sb)
{
long a = 0, b = 0;
for (int i = 0; i < sa.length(); i++)
{
a = (a * 10 + (sa.charAt(i) - '0')) %
MOD;
}
for (int i = 0; i < sb.length(); i++)
{
b = (b * 10 + (sb.charAt(i) - '0')) %
(MOD - 1);
}
return powerLL(a, b);
}
public static void main(String[] args)
{
String sa = "2", sb = "3";
System.out.println(powerStrings(sa, sb));
}
}
|
Python3
MOD = 1000000007;
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;
def powerStrings(sa, sb):
a = 0;
b = 0;
for i in range(len(sa)):
a = (a * 10 + (ord(sa[i]) -
ord('0'))) % MOD;
for i in range(len(sb)):
b = (b * 10 + (ord(sb[i]) -
ord('0'))) % (MOD - 1);
return powerLL(a, b);
sa = "2";
sb = "3";
print(powerStrings(sa, sb));
|
C#
using System;
class GFG
{
static long MOD = (long) (1e9 + 7);
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;
}
static long powerStrings(String sa, String sb)
{
long a = 0, b = 0;
for (int i = 0; i < sa.Length; i++)
{
a = (a * 10 + (sa[i] - '0')) % MOD;
}
for (int i = 0; i < sb.Length; i++)
{
b = (b * 10 + (sb[i] - '0')) % (MOD - 1);
}
return powerLL(a, b);
}
public static void Main(String[] args)
{
String sa = "2", sb = "3";
Console.WriteLine(powerStrings(sa, sb));
}
}
|
PHP
<?php
$MOD = 1000000007;
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;
}
function powerStrings($sa, $sb)
{
global $MOD;
$a = 0;
$b = 0;
for ($i = 0; $i < strlen($sa); $i++)
$a = ($a * 10 + ($sa[$i] -
'0')) % $MOD;
for ($i = 0; $i < strlen($sb); $i++)
$b = ($b * 10 + ($sb[$i] - '0')) %
($MOD - 1);
return powerLL($a, $b);
}
$sa = "2";
$sb = "3";
echo powerStrings($sa, $sb);
?>
|
Javascript
<script>
let MOD = (1e9 + 7);
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;
}
function powerStrings(sa, sb)
{
let a = 0, b = 0;
for (let i = 0; i < sa.length; i++)
{
a = (a * 10 + (sa[i] - '0')) % MOD;
}
for (let i = 0; i < sb.length; i++)
{
b = (b * 10 + (sb[i] - '0')) % (MOD - 1);
}
return powerLL(a, b);
}
let sa = "2", sb = "3";
document.write(powerStrings(sa, sb));
</script>
|
Time Complexity: O(n+m) where n and m are lengths of strings sa and sb.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
01 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...