UVA10515
计算N的M次方的最后一位的数值
1 #include <stdio.h> 2 #include <string.h> 3 4 int t[10]; 5 int save[10][10]; 6 char n[105], m[105]; 7 8 int solve(int num) 9 { 10 int lenn = strlen(n); 11 int mod = 0; 12 for (int i = 0; i < lenn; i++) 13 { 14 mod = (mod * 10 + n[i] - '0') % num;///每一部分都取余就能使运算减量 15 } 16 mod--; 17 if (mod < 0) 18 mod = num - 1; 19 return mod; 20 } 21 22 int main() 23 { 24 for (int i = 0; i < 10; i++) 25 { 26 int tmp = i; 27 save[i][t[i]++] = i; 28 tmp = tmp * i % 10; 29 while (tmp != i) 30 { 31 save[i][t[i]++] = tmp; 32 tmp = tmp * i % 10; 33 } 34 } 35 while (~scanf("%s%s", m, n)) 36 { 37 if (strcmp(m, "0") == 0 && strcmp(n, "0") == 0) break; 38 int start = m[strlen(m) - 1] - '0'; 39 printf("%d\n", save[start][solve(t[start])]); 40 } 41 return 0; 42 }
人生就像心电图,想要一帆风顺,除非game-over