Ordinary exponentiation algorithm
Let's look at an example hdu 2035
Cute A ^ B
Title Description
A ^ B integer seek the last three digits of the representation.
Description: A ^ B is the meaning of "A, B power"
Input
Input data comprising a plurality of test cases, one row for each instance, two positive integers A and B (1 <= A, B <= 10000), if A = 0, B = 0, it means the end of the input data without processing.
Output
For each test case, output a final three A ^ B integer representation, each output per line.
Sample Iutput
2 3
12 6
6789 10000
0 0
Sample Output
8
984
1
Problem-solving ideas : the simplest ideas should be talking about data obtained and then take the remainder of 1000 obtained the last three digits, we've all heard the concept of exponential explosion of it, the result will increase as the index of explosive growth, and 6789 10000 power even long long type is also kept high, below us to introduce a set of mathematical formulas
- (a + b) % c = ((a%c) + (b%c)) % c
- a * b % c = ( (a%c) * (b%c) ) % c
- a / b% c = (( a% c) / (b% c))% c
we can modulo operation for each factor, thereby reducing the size of data
following the C language code
#include <stdio.h>
long long quit(long long x, long long y)
{
long long ans = 1;
for(int i = 1; i <= y; i++)
{
ans *= x;
ans = ans%1000;
}
return ans%1000;
}
int main(void)
{
long long a, b, ans;
while(~scanf("%lld%lld", &a, &b))
{
if(a == 0 && b == 0) break;
ans = quit(a, b);
printf("%d\n", ans);
}
return 0;
}
Ordinary exponentiation algorithm time complexity is O (n), the time when we want to find the last three digits of 2 1 billion needed will be very long
What is the Quick Power Algorithm
Speak index core idea of fast power algorithm decreases and increases in base will reduce the number of cycles
we look at a chestnut
2 ^ 2 = 9 . 8 * @ 2 base-case 2
= 4 4 * in the base-case 2 // 4
= 16 2 * @ 2 base-case 16
= 256 . 1 * @ 2 base-case 256
can be seen 2 . 9 results in base when the index is equal to all of the odd product
Here we use code shows!
long long fastPower(long long base, long long power)
{
long long ans = 1;
while(power > 0)
{
if(power % 2 == 0)//如果指数为偶数
{
power /= 2;//把指数缩小一半
base = base * base % 1000;//底数平方
}
else
{
power--;//指数减一
power /= 2;//把指数缩小一半
ans *= base % 1000;//指数为奇数时的积
base = base * base % 1000;//底数平方
}
}
return ans;
}
Fast power optimization algorithm
long long fastPower(long long base, long long power)
{
long long ans = 1;
while(power > 0)
{
if(power % 2 == 1)
{
ans *= base % 1000;
}
power /= 2;
base *= base % 1000;
}
return ans;
}
The final optimization
We can power% 2 == 1 & 1 be replaced with a faster power, this operation is called "bit operation", power & 1, in binary If the last bit is 1, then the number is odd, if the last one is 0, then the last one is an even number, and at this time 1 & calculation, if an odd number, the result is 1, if the result is an even number 0 '
and the code is
long long fastPower(long long base, long long power)
{
long long ans = 1;
while(power > 0)
{
if(power&1)
{
ans *= base % 1000;
}
power /= 2;
base *= base % 1000;
}
return ans;
}
This article Reference: https://blog.csdn.net/qq_19782019/article/details/85621386
(learning for personal use)