Describe:
An integer representing the last three digits of the a^b. Description: The meaning of A^b is "A's B-square".
Input:
The input data contains multiple test instances, one row per instance, consisting of two positive integers a and B (1<=a,b<=10000), and if a=0, b=0, indicates the end of the input data and does not process.
Output:
For each test instance, output an integer representing the last three bits of a^b, one row for each output.
Sample input:
2 3
12 6
6789 10000
0 0
Sample output:
8
984
1
The Code of Fools is as follows (not fast power):
1#include <cstdio>2 intMain ()3 {4 intb;5 intk=1;6 while(SCANF ("%d%d", &a,&b)!=eof&& (a!=0&&b!=0)){7 for(intI=1; i<=b;i++){8k*=A;9k%= +; Ten } Oneprintf"%d\n", k); Ak=1; - } - the return 0; -}
Fast Power Code:
1#include <cstdio>2 intFastpow (intAintBintKKK) {3 intans=1;4 while(B >0){5 if(B &1){6Ans = ans*a%KKK;7 }8b >>=1;9A= a*a%KKK;Ten } One returnans; A } - intMain () - { the intb; - intsum; - while(SCANF ("%d%d", &a,&b)!=eof&& (a!=0&&b!=0)){ -sum = Fastpow (A, B, +); +printf"%d\n", sum); - } + A return 0; at}
Problem solving ideas (fast power):
(11 of the binary is 1011. That is, 11 = 2³x1 + 2²x0 + 2¹x1 + 2ºx1.
Memo bit arithmetic: Move right one equivalent to except 2. Shift left one equivalent to 2)
formal thinking: While loop is to control when B is 0 when the loop ends. The IF statement uses bitwise AND "&", when both sides are 1 and the expression is 1, this is used to determine whether the last digit of the binary is 1. If it is 1,ans, multiply the x^i,i to the position in the binary number of the bit. >> is a bitwise operator, moving one bit to the right, which removes the already calculated part. The last a= a*a%kkk; Used to mark the record X^2^i, the loop i is removed the I bit, when the i+1 bit is 1 o'clock, sum will multiply x^2^i. ~
HDU2035 people love a^b (fast power)