Topic Transfer: UVA-10006
Idea: is the rapid power of violence in the past on the line, and then to pay attention to the details, that is, the rapid power will explode int, and then is the first to determine whether the prime number is the direct output is normal, otherwise it will time out
AC Code:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set > #include <deque> #include <cctype> #define LL long long#define INF 0x7fffffffusing namespace Std;int n;int Is_prime (int x) {if (x = = 1) return 0;if (x = = 2 | | x = = 3) return 1;for (int i = 2; I <= sqrt (x); i + +) {if (x% i = = 0) r Eturn 0;} return 1;} int kmod (int x, int mod) {LL ret = 1;int tt = x;int t = mod;while (t) {if (T & 1) ret = (ret * x)% Mod;x = ((LL) x * x) % mod;//here will explode int T >>= 1;} if (ret = = TT) return 0;else return 1; }int judge (int x) {for (int i = 2; i < n; i++) {if (Kmod (i, N)) return 0;} return 1;} int main () {while (scanf ("%d", &n)! = EOF) {if (n = = 0) break;if (!is_prime (n) && judge (n)) {//judgment primes are placed in front, self-thought not much affected, knot The fruit was a little tle. printf ("The number%d is a Carmichael number.\n", n);} else {printf ("%d is normal.\n", n);}} return 0;}
Uva-10006-carmichael Numbers (Fast Power + prime number judgment)