Definition:
As the name implies, fast exponentiation is to quickly calculate the power of a certain number.
Its time complexity is O(log2N), and the efficiency is greatly improved compared with the naive O(N).
The following is an introduction to the b power of a
The weight of the i-th digit of the binary number is (2^(i-1)),
for example ,
a^11=a^(2^0+2^1+2^3)
11 In binary it is 1 0 1 1
11 = 2^3*1 + 2^2*0 + 2^1*1 + 2^0*1
As the name implies, fast exponentiation is to quickly calculate the power of a certain number.
Its time complexity is O(log2N), and the efficiency is greatly improved compared with the naive O(N).
The following is an introduction to the b power of a
principle:
Convert b to binary numberThe weight of the i-th digit of the binary number is (2^(i-1)),
for example ,
a^11=a^(2^0+2^1+2^3)
11 In binary it is 1 0 1 1
11 = 2^3*1 + 2^2*0 + 2^1*1 + 2^0*1
Therefore, we convert a^11 into a^(2^0)*a^(2^1)*a^(2^3);
Code:
where
t,y:int64;
begin
t:=1;y:=a;
while b<>0 do
begin
if (b and 1)=1 then t:=t*y mod n;
y:=y*y mod n; //A very powerful technique is used here, y*y finds a^(2^(i-1)) ←I don't know what this is, see the principle
b:=b shr 1;
end;
exit(t);
end;
begin
read(a,b,n); // n is modulo
write(f(a,b,n));
end.