Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n. 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, 7.
Input
Input consists of several datasets. Each dataset has an integer n (n ≤ 999999) in a line.
The number of datasets ≤ 30.
Output
For each dataset, prints the number of prime numbers.
Sample Input
10 3 11
Output for the Sample Input
4 2 5
给定n,求1~n中素数的个数
非常简单的素数筛选,套模板直接1A。
最近做的这几题都是在AC找手感~~明天开始做多校联合
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN = 10000007;
int prime[MAXN+1];
void getPrime()
{
memset(prime,0,sizeof(prime));
for(int i = 2; i<=MAXN; i++)
{
if (!prime[i]) prime[++prime[0]] = i;
for(int j = 1;j<=prime[0] &&prime[j] < MAXN/i;j++ )
{
prime[prime[j]*i] = 1;
if(i%prime[j]==0) break;
}
}
}
int main()
{
getPrime();
int n,i;
while(cin>>n)
{
for(i = 1; ;i++)
if (prime[i] > n) break;
cout<<i-1<<endl;
}
}