题目来源:51Nod - 1004
给出一个整数N,输出N^N(N的N次方)的十进制表示的末位数字。
Input
一个数N(1 <= N <= 10^9)
Output
输出N^N的末位数字
Sample Input
13
Sample Output
3
题解:
#include<cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int mod = 10;
typedef long long ll;
ll pow_mod(ll a)
{
ll res=1;
ll b=a;
while(b)
{
if(b&1) res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
int main()
{
ll n;
scanf("%lld",&n);
printf("%d\n",pow_mod(n));
return 0;
}
拓展:
a的k次幂前n位数
用该方法要注意精度
ak=10lgak=10k⋅lga=10[k⋅lga]+{k⋅lga}前n位:[10n−1⋅10{k⋅lga}]ak=10lgak=10k⋅lga=10[k⋅lga]+{k⋅lga}前n位:[10n−1⋅10{k⋅lga}]
例题 LOJ1282