HDU 1097 A hard puzzle 【快速幂 Or 规律(瞎搞)】
来源:互联网 发布:云计算架构师 技术栈 编辑:程序博客网 时间:2024/05/09 13:19
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次方的个位数字
思路:利用快速幂
AC代码:
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int quickpow(int a, int b, int c){ int ans = 1; a=a%c; while (b) { if (b%2) //b为奇数 ans = (ans * a) % c; b = b / 2; a = (a*a) % c; //(a^2)%c; } return ans;}int main(){ int a, b; while(~scanf("%d%d",&a,&b)) cout << quickpow(a, b, 10) << endl;}
非正常解题思路:
(以下内容参照木块木的博客:http://blog.csdn.net/u014174811/article/details/40821591)
假设a=38,b=2,那么结果应为(a*a)%10=(38*38)%10=[(38%10)*(38%10)]%10=4;
假设a=27,b=2,那么结果应为(a*a)%10=(27*27)%10=[(27%10)*(27%10)]%10=9;
.......
可以看出,(a^b)%10=(a*a*a......*a)%10=[(a%10)*(a%10)*(a%10)*.....(a%10)]%10;
那么就可以使用(b-1)次循环,求出(a^b)%10的结果,但这样时间复杂度就是O(b-1),提交给OJ后,会提出超时的警告。
所以需要一个时间复杂度更低的程序。
正整数a不管多大,都是十进制数,所以a%10的结果必然是0-9中的一个数。
0^n=0,那么只要以0结尾的数,该数的n次方的个位数还是0。
1^n=1,那么只要以1结尾的数,该数的n次方的个位数还是1。
同样以5结尾的数也是如此。那其它的数字又不一样,最后得出下列这张表:
从红色的框中可以看出:0-9中任何一个数字,其n次方后的个位数是有规律的。每4次一个循环。所以只要先前列一个数组,将其
保存,就可以直接找到对应的数组元素就是其(a^b%10)的值,这样时间复杂度为0(1);
AC代码:
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int main(){ int ans[4][10] = { {0,1,6,1,6,5,6,1,6,1}, {0,1,2,3,4,5,6,7,8,9}, {0,1,4,9,6,5,6,9,4,1}, {0,1,8,7,4,5,6,3,2,9} }; int a, b; while(~scanf("%d%d",&a,&b)) { cout << ans[b%4][a%10] << endl; }}
0 0
- HDU 1097 A hard puzzle 【快速幂 Or 规律(瞎搞)】
- 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 1061 Rightmost Digit 【快速幂 Or 规律(瞎搞)】
- 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(快速幂)
- HDOJ 1097 A hard puzzle(规律)
- HDU 1097 A hard puzzle【快速幂取模】
- hdu 1097 A hard puzzle (快速幂取模)
- HDU-1097-A hard puzzle( 快速幂取模 )
- 不跟随手指的小球--OnGestureListener和SCROLLER的使用
- 1034. Head of a Gang (30)
- 文件和文件系统
- 全栈必备 网络编程基础
- DFS+奇偶剪枝
- HDU 1097 A hard puzzle 【快速幂 Or 规律(瞎搞)】
- 二分贪心专题D
- Kvm虚拟化的一种打包及部署方案(采用tar包,lvm方式)
- linux 基础知识大杂烩
- python2中unicode对象与字节流对象的编码问题
- 历届试题 小朋友排队
- 5-6 列出连通集 (25分)
- STM32高级开发(4)-ubuntu的简介和命令
- openjudge-拨钟问题