HDU 1097 A hard puzzle(简单快速幂)
来源:互联网 发布:linux 删除文件 api 编辑:程序博客网 时间:2024/05/20 14:43
<span style="font-size:18px;">http://acm.hdu.edu.cn/showproblem.php?pid=1097</span>
A hard puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 34742 Accepted Submission(s): 12477
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.
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 668 800
Sample Output
96
题意:求a的b次方所得结果的最后一位数。
解题思路:这是一题较简单的快速幂。以下是我对快速幂的理解,具体看代码。
快速幂思路:
• 求a^b,是不是要把a乘b次呢?这样也行, 只是慢了点 • 例如求a^8,先把a乘a一次,得到a^2,然后把 a^2和a^2乘一次,得到a^4,然后把a^4和a^4乘一 下就得到a^8了 • 假如要求a^10呢?10=8+2,a^10=a^8*a^2 • 把10化为二进制, 10=1010(2),1010(2)=1000(2)+10(2)
<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:24px;">#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int q_pow(int a,int b){ int ans=1;//ans初始化为1 while(b>0) { if(b&1) ans =(ans*a)%10;//如果b为奇数,把ans乘于a a =(a*a)%10;//把a变成a^a,并且%10,防止溢出 b>>=1;//b右移一位,相当于除于2 } return ans;}int main(){ int a,b; while(cin>>a>>b) { printf("%d\n",q_pow(a%10,b));//a%10在这是为了提速,原因自己想想 } return 0;}</span></span></span>
0 0
- HDU 1097 A hard puzzle(简单快速幂)
- HDU 1097 A hard puzzle && 快速幂
- HDU 1097 A hard puzzle(快速幂)
- hdu 1097 A hard puzzle(快速幂入门)
- HDU 1097 A hard puzzle(快速幂)
- HDU 1097 A hard puzzle(找规律,快速幂)
- HDU 1097 A hard puzzle (规律&&快速幂)
- HDU-1097-A hard puzzle(快速幂)
- hdu 1097 A hard puzzle (快速幂)
- HDU--1097A hard puzzle(快速幂)
- HDU 1097 A hard puzzle【快速幂取模】
- hdu 1097 A hard puzzle (快速幂取模)
- HDU-1097-A hard puzzle( 快速幂取模 )
- hdu 1097 A hard puzzle 快速幂 找规律 H
- hdu 1097 A hard puzzle(快速幂取模)
- HDU - 1097 - A hard puzzle (快速幂取模)
- HDU 1097 A hard puzzle(快速幂取模)
- HDU 1097 A hard puzzle(快速幂取模)
- 关于子进程继承父进程属性的小问题总结
- eclipse构建maven的web项目
- 你不知道java问题if后面加不加括号的问题
- windows遍历文件夹
- Linux环境Nginx安装与调试以及PHP安装
- HDU 1097 A hard puzzle(简单快速幂)
- C# String与string的区别 以及 C# Swap string
- Linux写时拷贝技术(copy-on-write)
- 30多个iOS常用动画,带详细注释
- CocoStudio:ImageView分析
- 链表倒序输出三种算法
- bootstrap单击导航条下的li后,自动收回
- C语言 编译链接过程介绍
- 深入解析Oracle学习笔记(第七章)