HDU-1097,A hard puzzle(快速幂)

简介: HDU-1097,A hard puzzle(快速幂)

Problem Description:


lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody 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 is too lazy to slove this problem,so they remit to you who is wise.


Input:  


There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)


Output:


For each test case, you should output the a^b's last digit number.


Sample Input:


7 66


8 800  


Sample Output:


9


6


解题思路:


这道题就是求a的b次方的最后一位数字,典型的快速幂,因为是要求最后一位,所以在进行快速幂之前,我们可以对原数进行取个位数字之后再运算,因为决定最后一位数字的始终就是个位数字,也就是%10。


程序代码:  


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int quickmul(int a,int b)
{
  int ans=1;
  while(b)
  {
    if(b&1)
      ans=(ans*a)%10;
    a=(a*a)%10;
    b>>=1;
  }
  return ans%10;
}
int main()
{
  int n,m;
  while(cin>>n>>m)
  {
    cout<<quickmul(n%10,m)<<endl;//关键n%10
  }
  return 0;
}


相关文章
UVa668 - Parliament(贪心)
UVa668 - Parliament(贪心)
64 0
hdu-1098 Ignatius's puzzle(费马小定理)
hdu-1098 Ignatius's puzzle(费马小定理)
161 0
hdu-1098 Ignatius's puzzle(费马小定理)
[HDU 4738] Caocao‘s Bridges | Tarjan 求割边
Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn’t give up. Caocao’s army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river,
144 0
|
Java
HDOJ 1097 A hard puzzle(循环问题)
HDOJ 1097 A hard puzzle(循环问题)
124 0
|
算法 C++
HDOJ(HDU) 2109 Fighting for HDU(简单排序比较)
HDOJ(HDU) 2109 Fighting for HDU(简单排序比较)
121 0
|
C语言
HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
102 0
|
存储 算法 测试技术

热门文章

最新文章