Uva-10006-carmichael Numbers (Fast Power + prime number judgment)

Source: Internet
Author: User


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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.