1. 程式人生 > >EOJ3134. 簡訊啟用碼(大數冪取模)

EOJ3134. 簡訊啟用碼(大數冪取模)

題面

輸入只有5位,所以轉化為long long型別用快速冪取模

前面補0的寫法printf("%05lld\n",ans);如果ans不足5位會在前面補0

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 long long mod_exp(long long a, long long b, long long c)        //快速冪取餘a^b%c
 4 {
 5     long long 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 { 21 int t; 22 while(~scanf("%d",&t)) 23 { 24 int
cases=-1; 25 while(t--) 26 { 27 cases++; 28 char s[10],s2[10]; 29 scanf("%s",s); 30 s2[0]=s[0]; 31 s2[1]=s[2]; 32 s2[2]=s[4]; 33 s2[3]=s[3]; 34 s2[4]=s[1]; 35 long long temp=(s2[0
]-'1'+1)*10000+(s2[1]-'1'+1)*1000+(s2[2]-'1'+1)*100+(s2[3]-'1'+1)*10+(s2[4]-'1'+1)*1; 36 printf("case #%d:\n%05lld\n",cases,mod_exp(temp,5,100000)); 37 } 38 } 39 return 0; 40 }
View Code