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;
}
Forever+Young 发布了274 篇原创文章 · 获赞 282 · 访问量 2万+ 私信 关注
标签:10,HDU,1097,int,puzzle,hard,ans,problem,include 来源: https://blog.csdn.net/weixin_43823808/article/details/104075568