COJ0700 Mathematics (I.)

Source: Internet
Author: User

Question Description
Now there are a lot of numbers, please test these numbers.
Input
First line: CAs, which represents the number of data groups (not greater than 500000), the following CAS lines, one number per line, guaranteed to be within the 64-bit long shaping range, and no negative numbers. You need to test for each number to see if it is prime.
Output
A total of one row, the number of output primes, guaranteed to be positive in the 64-bit long shaping range.
Input example
4 2 3) 5 4
Output example
3
Other Notes
Data range: Guaranteed cas<=100000 to ensure that all numbers are within the 64-bit long range. Please try to optimize your program, including Oi optimization. Welcome to the violence law. →→

First there is such a theorem:

If P is a prime number, X is an integer, and x^2 mod p = 1, then x≡±1 (mod p).

Proof: x^2 mod p = 1 p | X^2-1-P | (x-1) (x + 1), and because P is a prime number, there is a multiple of p in X-1 and x+1.

But the converse of this theorem is a false proposition, but it rarely has a counter-example. We can use Converse to judge the primes.

Set the number to be measured as n, whichever is smaller than n a positive integer A, set n-1 = R * 2^s, if n is prime, the following conditions at least one is established:
1.a^s mod n = 1

2. There is an integer I satisfies: 0<=i<s and a^ (d* (2^i)) mod n = 1

Repeat steps 3 to 4 to stabilize the solution.

#include <cstdio>#include<cctype>#include<cstring>#include<algorithm>#defineRep (s,t) for (int i=s;i<=t;i++)using namespaceStd;inlineintRead () {intx=0, f=1;CharC=GetChar ();  for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1;  for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;} typedefLong LongLl;ll Pow (ll n,ll m,ll p) {ll ans=1, t=N;  while(m) {if(m&1) (ans*=t)%=p; (T*=T)%=p;m>>=1; }    returnans;}intCheck (ll a,ll n,ll R,ll s) {ll ans=pow (A,r,n);if(ans==1)return 0; Rep (1, s) {        if(ans==n-1)return 0; (Ans*=ans)%=N; }    return 1;}intIsPrime (ll N) {if(n==2)return 1; if(n<=1||! (n&1))return 0; intr=n-1, s=0;  while(! (r&1)) r>>=1, s++; Rep (1,3)if(Check (rand ()% (n1)+1, n,r,s))return 0; return 1;}intMain () {intT=read (), ans=0;  while(t--) ans+=IsPrime (read ()); printf ("%d\n", ans); return 0;}
View Code

COJ0700 Mathematics (I.)

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.