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;
}


相关文章
|
6月前
|
Java
HDU-2199-Can you solve this equation
HDU-2199-Can you solve this equation
30 0
|
6月前
|
Java
HDU-2199-Can you solve this equation?
HDU-2199-Can you solve this equation?
37 0
|
C++
poj 2182 Lost Cows(树状数组)
FJ有n头牛,现在它们排成一排,每一头牛都有一个不同的编号(1-n),现在知道从第二头牛开始每头牛左边比自己编号小的牛的数目,让你确定每头牛的编号。
40 0
|
网络架构
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
|
数据挖掘
HDU-1032,The 3n + 1 problem(水题)
HDU-1032,The 3n + 1 problem(水题)
|
测试技术
HDU-1026,Ignatius and the Princess I(BFS+打印路径)
HDU-1026,Ignatius and the Princess I(BFS+打印路径)
|
算法 Go
HDU-1548,A strange lift(Dijkstra)
HDU-1548,A strange lift(Dijkstra)
HDOJ/HDU 1372 Knight Moves(经典BFS)
HDOJ/HDU 1372 Knight Moves(经典BFS)
134 0