【Aizu - ALDS1_1_C】Prime Numbers(素数筛法)

Prime Numbers

 Descriptions:

A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.

Write a program which reads a list of N integers and prints the number of prime numbers in the list.

Input

The first line contains an integer N, the number of elements in the list.

N numbers are given in the following lines.

Output

Print the number of prime numbers in the given list.

Constraints

1 ≤ N ≤ 10000

2 ≤ an element of the list ≤ 108

Sample Input 1

5
2
3
4
5
6

Sample Output 1

3

Sample Input 2

11
7
8
9
10
11
12
13
14
15
16
17

Sample Output 2

4

题目大意:
输入n个数,判断里面有几个是素数,逐个枚举是最简单的但是会超时,这里用力一个简单的函数,以后可以套用

bool isprime(int x)
{
    if(x==2)
        return true;
    if(x<2||x%2==0)
        return false;
    int i=3;
    while(i<=sqrt(x))
    {
        if(x%i==0)
            return false;
        i+=2;
    }
    return true;
}

 AC代码:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
bool isprime(int x)
{
    if(x==2)
        return true;
    if(x<2||x%2==0)
        return false;
    int i=3;
    while(i<=sqrt(x))
    {
        if(x%i==0)
            return false;
        i+=2;
    }
    return true;
}
int n;
int main()
{
    int total=0;
    int m;
    cin>>n;
    while(n--)
    {
        cin>>m;
        if(isprime(m))
          total++;
    }
    cout<<total<<endl;
}

这题比较简单,我之所以要为他写一篇博客,是因为一下这个埃拉托色筛选法:

1.列举大于等于2的整数

2.留下最小的整数2,删除所有2的倍数

3.在剩下的整数中留下最小的3,删除所有3的倍数

4.在剩下的整数中留下最小的5,删除所有5的倍数

5.以下同理,留下仍未被删除的最小的整数,删除该整数的倍数,一直循环到结束

int isprime[100005];
void eratos(int x)
{
    for(int i=0; i<=x; ++i)
        isprime[i]=true;
    isprime[0]=isprime[1]=false;
    for(int i=2; i<=x; ++i)
    {
        if(isprime[i])
        {
            j=i+i;
            while(j<=x)
            {
                isprime[j]=false;
                j+=i;
            }
        }
    }
}

原文地址:https://www.cnblogs.com/sky-stars/p/11107904.html

时间: 2024-10-29 00:56:25

【Aizu - ALDS1_1_C】Prime Numbers(素数筛法)的相关文章

Codeforces 385C Bear and Prime Numbers [素数筛法]

Code: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<string> #include<queue> #include<deque> #include<stack> #include<map&g

POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】

Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Accepted: 10619 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations d

UVA 10539 Almost Prime Numbers( 素数因子)

Problem AAlmost Prime NumbersTime Limit: 1 second Almost prime numbers are the non-prime numbers which are divisible by only a single prime number. In this problem your job is to write a program which finds out the number of almost prime numbers with

CodeForces 385C Bear and Prime Numbers 素数打表

第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample has 3 queries. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9. The second query comes

Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)

Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeForces. Original ID: 385C 64-bit integer IO format: %I64d      Java class name: (Any) Prev Submit Status Statistics Discuss Next Type: None Recently, the bear started studyi

HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

Problem Description Give you a lot of positive integers, just to find out how many prime numbers there are. Input There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won't exceed 32-

poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697   Accepted: 10800 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

Codeforces 385C Bear and Prime Numbers(素数预处理)

Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出现个数时使用了map,以至于后面做前缀和的累加时,每次都要对map进行查询,以至于TLE.而自己一直没有发现,以为是欧拉筛对于这道题还不够优,于是上网搜题解,发现别人的做法几乎一样,但是却能跑过,挣扎了许久才想起是map的原因.map的内部实现是一颗红黑树,每次查询的复杂度为O(logN),在本来时

POJ 2739 Sum of Consecutive Prime Numbers(素数)

http://poj.org/problem?id=2739 题意: 给你一个10000以内的自然数X,然后问你这个数x有多少种方式能由连续的素数相加得来? 分析: 首先用素数筛选法把10000以内的素数都找出来按从小到大保存到prime数组中. 然后找到数X在prime中的上界, 如果存在连续的素数之和==X, 那么一定是从一个比X小的素数开始求和(不会超过X的上界),直到和sum的值>=X为止. 所以我们暴力枚举10000以内的所有可能的素数相加和的起始点i,然后求连续素数的和,看看当前以p

AOJ - 0009 Prime Number (素数筛法) &amp;&amp; AOJ - 0005 (求最大公约数和最小公倍数)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34870 求n内的素数个数. 1 /* *********************************************** 2 Author : zch 3 Created Time :2015/5/19 8:46:16 4 File Name :a.cpp 5 ************************************************ */