给定两个用字符串表示的数字sa和sb ,找到b %MOD,其中MOD为1e9 +7。数字a和b最多可以包含10个6位数字。
例子:
Input : sa = 2, sb = 3
Output : 8
Input : sa = 10000000000000000000000000000000000000000000
sb = 10000000000000000000000000000000000000000000
Output : 494234546
由于a和b非常大(每个最多可以包含10 ^ 6个数字)。因此,我们可以做的是,应用费马小定理和模的性质来简化a和b 。
减少:
据我们所知,
(ab) % MOD = ((a % MOD)b) % MOD
减少b:
如何减少b ,我们已经在Find(a ^ b)%m中进行了讨论,其中’b’非常大
现在最终我们使a和b都在1 <= a,b <= 10 ^ 9 + 7的范围内。因此,我们现在可以使用我们的模幂来计算所需的答案。
C++
// CPP program to find (a^b) % MOD where a and
// b may be very large and represented as strings.
#include
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
输出:
8