Note: n = p1 * p2 * p3...... Pm
Solution: use the formula a ^ (P-1/2) = (a/p) mod p to solve the problem.
# Include <stdio. h> # include <math. h> int flag [1005], p [500], a; int d [100]; int init (int s) {int len = 0, tmp, h = sqrt (s + 0.5); for (int I = 0; p [I] <= h; I ++) if (s % p [I] = 0) {if (a % p [I] = 0) return-1; while (s % p [I] = 0) {d [len ++] = p [I]; s/= p [I];} if (s = 1) return len;} if (s> 1) {if (a % s = 0) return-1; // you forgot to judge this part. The error is returned for a long time. TTTd [len ++] = s ;} return len;} int getans (int x, int s) {int tmp = s/2 ;__ int64 ans = 1, B = x; while (tmp> 0) {if (tmp & 1) ans = ans * B % s; B = B * B % s; tmp/= 2;} if (ans! = 1) return-1; return 1;} int main () {int I, j, k = 0, n; for (I = 2; I <1000; I ++) {if (! Flag [I]) {p [k ++] = I; for (j = I * I; j <1000; j + = I) flag [j] = 1 ;}}while (scanf ("% d", & a, & n )! =-1) {int len = init (n); if (len =-1) {printf ("0 \ n"); continue;} int ans = 1; for (I = 0; I <len; I ++) ans * = getans (a, d [I]); printf ("% d \ n", ans );} return 0 ;}