标签:pre 快速 mis bit names several ret 题解 超时
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 69614 Accepted Submission(s): 25945
题意:给出n,要求算出n^n的最右边一位。
题解:普通方法进行幂运算再进行取余会超时,需要用到快速幂
1 #include<bits/stdc++.h> 2 using namespace std; 3 int mod_exp(int a, int b, int c) //快速幂取余a^b%c 4 { 5 int res, t; 6 res = 1 % c; 7 t = a % c; 8 while (b) 9 { 10 if (b & 1) 11 { 12 res = res * t % c; 13 } 14 t = t * t % c; 15 b >>= 1; 16 } 17 return res; 18 } 19 int main() { 20 int t; 21 while(~scanf("%d",&t)) 22 { 23 while(t--) 24 { 25 int n; 26 scanf("%d",&n); 27 printf("%d\n",mod_exp(n,n,10)%10); 28 } 29 } 30 return 0; 31 }
标签:pre 快速 mis bit names several ret 题解 超时
原文地址:https://www.cnblogs.com/fqfzs/p/9858576.html