How do you calculate the modulo of a high-raised number?
Solution 1:
There are often tricks to this if the numbers are nice enough, but even if they're not, here's a way that's not entirely horrible.
You already know what 439 is mod 713. What is $439^2 \mod 713$? What about $439^4$? (Hint: take your answer for $439^2$ after reducing it mod 713, and then square it again.) In the same way, calculate $439^8, 439^{16}, \dots, 439^{128} \mod 713$. Now just note that 233 = 128 + 64 + 32 + 8 + 1. So multiply the appropriate powers of 439 together - again, one calculation at a time, reducing mod 713 each time.
Now you should only have to do 11 calculations, and now all your numbers are 6 digits or less. Rather than impossible, it's now simply tedious. :)
By the way, one thing to notice: 713 = 23 * 31. Perhaps your calculations will be easier if you do them mod 23 and 31, then apply the Chinese remainder theorem?
Solution 2:
$713=23\cdot 31$
$439 \pmod {23}=2$ and $\phi(23)=22$ and $233\equiv 13{\pmod {22}}$
So, $439^{223} {\pmod {23}} \equiv 2^{22\cdot 10 + 13}\equiv {(2^{22})}^{10}2^{13}\equiv 2^{13} {\pmod {23}}$ using Euler's Totient Theorem.
$2^6\equiv 18 {\pmod {23}}, 2^7\equiv 36 \equiv 13$
$\implies 2^{13}\equiv 18\cdot 13=234\equiv 4 {\pmod {23}}=4+23x$ for some integer $x$.
$439 \pmod {31}=5$ and $\phi(31)=30$ and $233\equiv 23{\pmod {30}}$
So, $439^{223} {\pmod {31}} \equiv 5^{23} {\pmod {31}}$
$5^3 \equiv 1 {\pmod {31}} \implies 5^{23}\equiv({5^3})^7 5^2\equiv 5^2{\pmod {31}}=25+31y$ for some integer $y$.
So, we need to find $z$ such that $z=25+31y=4+23x$
Expressing as continued fraction, $$\frac{31}{23}=1+\frac{8}{23}=1+\frac{1}{\frac{23}{8}}=1+\frac{1}{2+\frac{7}{8}}$$
$$=1+\frac{1}{2+\frac{1}{\frac{8}{7}}}=1+\frac{1}{2+\frac{1}{1+\frac{1}{7}}}$$
So, the last but one convergent is $$1+\frac{1}{2+\frac{1}{1}}=\frac{4}{3}$$
So, $23\cdot 4- 31\cdot 3=-1$
$25+31y=4+23x\implies 23x=31y+21(31\cdot 3-23\cdot 4)$ $\implies 23(x+84)=31(y+63)$ $$\implies x+84=\frac{31(y+63)}{23}$$
So, $23\mid (y+63)$ as $x+84$ is integer and $(31,23)=1$ i.e., $ 23\mid (y+69-6)\implies 23\mid (y-6) \implies y=6+23w$
So, $z=25+31y=25+31(6+31w)=713w+211 \equiv 211 {\pmod {713}}$
Solution 3:
Here`s the algorithm,basically it is Modular exponentiation.
function modular_pow(base, exponent, modulus)
result := 1
while exponent > 0
if (exponent mod 2 == 1):
result := (result * base) mod modulus
exponent := exponent >> 1
base = (base * base) mod modulus
return result
Also here is the working code in c++ which can work for upto 10^4 test cases,in 0.7 seconds Also the ideone link http://ideone.com/eyLiOP
#include <iostream>
using namespace std;
#define mod 1000000007
long long int modfun(long long int a,long long int b)
{
long long int result = 1;
while (b > 0)
{
if (b & 1)
{
a=a%mod;
result = (result * a)%mod;
result=result%mod;
}
b=b>>1;
a=a%mod;
a = (a*a)%mod;
a=a%mod;
}
return result;
}
int main()
{
int t;
cin>>t;
while(t--)
{
long long int a,b;
cin>>a>>b;
if(a==0)
cout<<0<<"\n";
else if(b==0)
cout<<1<<"\n";
else if(b==1)
cout<<a%mod<<"\n";
else
{
cout<<(modfun(a,b))%mod<<"\n";
}
}
return 0;
}