HDU 2138 How many prime numbers (Miller_rabin method of judging prime numbers "* template" used fast power algorithm)

Source: Internet
Author: User

How many prime numbers

Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 12955 Accepted Submission (s): 4490


Problem Description Give you a lot of positive integers, just to find out how many prime numbers there is.

Input There is a lot of cases. In each case, there is a integer N representing the number of integers to find. Each of the integer won ' t exceed 32-bit signed integer, and each of the them won ' t is less than 2.

Output for each case, print the number of prime numbers you has found out.

Sample Input32 3 4

Sample Output2algorithm: Each input a number directly judge the violence Sqrt judge whether the number is prime, and then accumulate the number of primes, can also water over. but can also take this water problem that practice Miller_rabin judge Prime Method! Time complexity is low!
Code:
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm>using namespace Std;long long Pow_mod (long long A, long long I, long long N) {if (i==0) return 1%n;long long Temp=pow_mod (A, i>>1, N); temp = temp*temp%n;if (i&1) temp = (long long) Temp*a%n;return temp;} BOOL Test (int n, int a, int dd) {if (n==2) return true;if (n==a) return true;if ((n&1) ==0) return False;while (! ( dd&1)) Dd=dd>>1;int T=pow_mod (A, DD, n);    Call Fast power function while ((dd!=n-1) && (t!=1) && (t!=n-1)) {T = (long Long) t*t%n;dd=dd<<1;} Return (T==n-1 | | (dd&1) ==1);} BOOL Miller_rabin_isprime (int n)//o (Logn) {if (n<2) return False;int a[]={2, 3, ","//for (int i=0; i<3; i++) if (!tes T (n, A[i], n-1)) return False;return true;}    int main () {int n;        while (scanf ("%d", &n)!=eof) {int dd;        int cnt=0;            while (n--) {scanf ("%d", &AMP;DD);        if (Miller_rabin_isprime (DD)) cnt++;} printf ("%d\n", CNT); } return 0;}

HDU 2138 How many prime numbers (Miller_rabin method of judging prime numbers "* template" used fast power algorithm)

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.