A hard puzzle

来源:互联网 发布:淘宝店铺第三方 编辑:程序博客网 时间:2024/06/11 17:01

A hard puzzle

问题描述 :

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.

输入:

There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)

输出:

For each test case, you should output the a^b’s last digit number.

样例输入:

7 668 800

样例输出:

9

6

题解:题意是求a^b的最后一位数,这里是用快速幂来解决的,也就是边乘边模,这样可以避免溢出。

#include<stdio.h>#include<string.h>typedef long long LL;LL fun(LL x,LL n){LL res=1;while(n>0){if(n&1){res=(res*x)%10;}x=(x*x)%10;n>>=1;}return res%10;}int main(){LL a,b;while(scanf("%d%d",&a,&b)!=EOF){printf("%d\n",fun(a,b)%10);}return 0;}

0 0
原创粉丝点击