Ultraviolet A 10006-Carmichael Numbers number theory (Rapid power modulo + sieve method for prime number)

Source: Internet
Author: User

Carmichael Numbers

An important topic nowadays in computer science is cryptography. some people even think that cryptography is the only important field in computer science, and that life wocould not matter at all without cryptography. alvaro is one of such persons, and is a set of cryptographic procedures for cooking paella. some of the cryptographic algorithms he is implementing make use of big prime numbers. however, checking if a big number is prime is not so easy. an exhaustive approach can require the division of the number by all the prime numbers smaller or equal than its square root. for big numbers, the amount of time and storage needed for such operations wowould certainly ruin the paella.
However, some probabilistic tests exist that offer high confidence at low cost. One of them is the Fermat test.

Let a be a random number between 2 and n-1 (being n the number whose primality we are testing). Then, n is probably prime if the following equation holds:

 

 

 

If a number passes the Fermat test several times then it is prime with a high probability.
Unfortunately, there are bad news. Some numbers that are not prime still pass the Fermat test with every number smaller than themselves. These numbers are called Carmichael numbers.

In this problem you are asked to write a program to test if a given number is a Carmichael number. hopefully, the teams that fulfill the task will one day be able to taste a delicious portion of encrypted paella. as a side note, we need to mention that, according to Alvaro, the main advantage of encrypted paella over conventional paella is that nobody but you knows what you are eating.


Input
The input will consist of a series of lines, each containing a small positive number n (2 <n <65000 ). A number n = 0 will mark the end of the input, and must not be processed.

Output
For each number in the input, you have to print if it is a Carmichael number or not, as shown in the sample output.

Sample Input
1729
17
561
1109
431
0

Sample Output
The number 1729 is a Carmichael number.
17 is normal.
The number 561 is a Carmichael number.
1109 is normal.
431 is normal.

Judge whether a number is Carmichael.

If a number is not a prime number and any 2 <a <n satisfies the equation, n is the Carmichael number; otherwise, n is not the Carmichael number.

The key to this question is to calculate the power quickly.

 

# Include <stdio. h> # include <string. h> # include <math. h> # define LL long longint a [66000]; void judge_prime ()/* sieve method for Prime Number */{int I, j, m = sqrt (65010 + 0.5 ); memset (a, 0, sizeof (a); for (I = 2; I <= m; I ++) {if (! A [I])/* The prime number is 0 */{for (j = I * I; j <65010; j + = I) a [j] = 1; /* Non-prime number: 1 */} LL pow_mod (LL a, LL n, LL m)/* recursively evaluate the Fast Power */{if (n = 0) return 1; LL x = pow_mod (a, n/2, m); LL ans = x * x % m; if (n % 2 = 1) ans = ans * a % m; return ans;} int main () {judge_prime (); LL I, n; bool flag; while (~ Scanf ("% lld", & n) {if (! A [n]) {printf ("% lld is normal. \ n ", n); continue;} flag = true; for (I = 2; I <n; I ++) {if (pow_mod (I, n, n )! = I) {flag = false; break;} if (flag) printf ("The number % lld is a Carmichael number. \ n ", n); else printf (" % lld is normal. \ n ", n);} return 0 ;}

 

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.