快速幂求a^b结果的最后一位
来源:互联网 发布:制作字幕的软件 编辑:程序博客网 时间:2024/06/06 03:57
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 668 800sample output
96代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int k_mi(int a, int b) { //快速幂取模
int ans = 1,t = a % 10;
while(b) {
if(b&1) ans = (ans * t) % 10;
t = (t * t) % 10;
b >>= 1;
}
return ans % 10;
}
int main() {
int a, b;
while(scanf("%d %d", &a, &b) != EOF) {
printf("%d\n", k_mi(a, b));
}
return 0;
}
阅读全文
0 0
- 快速幂求a^b结果的最后一位
- 快速幂或找规律求a的b次方的最后一位-HDU1097
- 快速求幂的最后一位
- 求一个数的 a 的 b 次方的结果的最后一位数。
- 求A^B的最后三位
- HDU-1097-a^b最后一位
- 求A^B的最后三位数表示的整数
- 5,29 求a的b次方 的最后三位数
- 5,29 求 a的 b次方的最后三位数
- 求A的B次方的最后三位整数
- C++快速求幂a^b
- 求n!的最后一位非零数
- 求n的n次方最后一位
- 求a的b次幂的最后3位(难度:1颗星)
- 杭电1097--a^b周期最后一位
- 二分法快速求a^b
- 求A^B的最后三位数表示的整数。 说明:A^B的含义是“A的B次方”
- 幂方的最后一位
- css/网页布局方式
- java继承常见错误
- apache和tomcat区别
- Java 泛型详解
- 剑指offer: 二叉搜索树的后序遍历序列
- 快速幂求a^b结果的最后一位
- 浅谈SVM
- 旋转数组中的最小数字
- Bootstrap初识
- 关于python中值传递和引用传递的问题
- 习题 4.12 有4个圆塔,圆心分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),圆半径为1,这4个塔的高度为10m,塔以外无建筑物。今输入任一点的坐标,求该点的建筑高度(塔外的高度为零)
- jQuery UI 工作原理
- 携程4月11日编程题 输出最大乘积 java
- 九、swift3.0可选类型+类型修饰符及其它