A Hard Puzzle
Problem Descriptionlcy gives a hard puzzle to feng5166,lwg,jgshining and ignatius:gave A and b,how to know the A^b.everyb Ody objects to this BT problem,so LCY makes the problem easier than begin.
This puzzle describes That:gave A and b,how to know the A^b ' s, the last digit number. But everybody was too lazy to slove the problem,so they remit to you, who was wise.
Inputthere is mutiple test cases. Each test cases consists of numbers a and B (0<a,b<=2^30)
Outputfor Each test case, you should output of the a^b ' s last digit number.
Sample Input
7 668 800
Sample Output
96
title: Ask for the last number of a^b, the problem into a^b%10, the code is as follows:
#include <cstdio>int mod (int a,int b,int k) { int ans=1; A=a%k; while (b>0) { if (b%2==1) ans= (ans*a)%k; b/=2; A= (a*a)%k; } return ans;} int main () { int a A, B; while (scanf ("%d%d", &a,&b) ==2) printf ("%d\n", mod (a,b,10)); return 0;}
This is a direct method of seeking. Look at the other people's puzzle, there is a law:
the mantissa is 0,1,5,6 regardless of how many times the mantissa remains unchanged, while the mantissa is 4 and 9 for every 2 cycles,
2,3,7,8 for every 4 cycles. The loop results are as follows:
0,1,5,6: The number of digits is always 0,1,5,6
2:6,2,4,8 Cycle
3:1,3,9,7 Cycle
4:6,4 Cycle
7:1,7,9,3 Cycle
8:6,8,4,2 Cycle
9:1,9 Cycle
Yes, I can get the water.the following:
#include <cstdio>int div[10]={1,1,4,4,2,1,1,4,4,2};int f[10][4]={{0},{1},{6,2,4,8},{1,3,9,7},{6,4},{5},{6}, {1,7,9,3},{6,8,4,2},{1,9}};int main () { int A, b; while (scanf ("%d%d", &a,&b) ==2) printf ("%d\n", f[a%10][b%div[a%10]]); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1097.A Hard puzzle "fast power or Law" "August 12"