2 3 4
7 6HintIn the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
题意:
求n^n模10
代码:
#include<stdio.h> int exp_mod(int a,unsigned int n,int b) { int t; if(n==0||n==1) return n==0?1:a; t=exp_mod(a,n/2,b); t=t*t; if(n%2==1)t=t*a; return t%b; } int main() { int t; scanf("%d",&t); while(t--) { long long n; scanf("%lld",&n); printf("%d\n",exp_mod(n%10,n,10)); } return 0; }
分析:
代码是在网上找的模版,第一次做是想找规律的可是实在是太繁琐勒(其实是我懒= =! ),就上网看看有没有更简单的方法,才知道这样的题有模版,就是快速幂取模.果断背下来....
下面是我经过在网上找的解析: