题意:给出n,求不大于n的素数有多少个。

算法:先用线性时间复杂度的筛法打素数表,对于每个输入统计不超过的素数个数。

#include <cstdio>

int p[100010];
bool np[1000010];
int cntp; void SievePrime(int n) {
for (int i = 0; i <= n; ++i) np[i] = true;
np[0] = false, np[1] = false;
for (int i = 2; i <= n; ++i) {
if (np[i]) {
p[cntp++] = i;
for (int j = 2 * i; j <= n; j += i) {
np[j] = false;
}
}
}
} int main() {
SievePrime(1000000);
int n;
while (scanf("%d", &n) != EOF) {
int ans = 0;
for (int i = 2; i <= n; ++i) {
if (np[i]) ++ans;
}
printf("%d\n", ans);
}
return 0;
}

AOJ 0009 Prime Number的更多相关文章

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

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34870 求n内的素数个数. /* ********************* ...

  2. FZU 1649 Prime number or not米勒拉宾大素数判定方法。

    C - Prime number or not Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  3. 每日一九度之 题目1040:Prime Number

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...

  4. LintCode-Kth Prime Number.

    Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. The eli ...

  5. 10 001st prime number

    这真是一个耗CPU的运算,怪不得现在因式分解和素数查找现在都用于加密运算. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13 ...

  6. [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  7. [Swift]LeetCode762. 二进制表示中质数个计算置位 | Prime Number of Set Bits in Binary Representation

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  8. 10_ for 练习 _ is Prime Number ?

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量

    [抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...

随机推荐

  1. <table>居中的一种方法

    <div title="主页" data-options="iconCls:'icon-house'"> <center> <ta ...

  2. configure编译选项

    1.rpath与rpath-link的区别 参考链接:http://blog.csdn.net/xph23/article/details/38157491 rpath 是 运行时候链接的库, rpa ...

  3. B. ZgukistringZ

    题目链接:http://codeforces.com/contest/551/problem/B 题目大意:给你三个字符串,s1,s2,s3.  s1任意两个字符串之间可以互相交换. 问,在s1中s2 ...

  4. JavaScript学习 - 基础(四) - 控制语句/异常处理

    控制语句 if-else 语句 <script> //控制语句 //if-else格式: var x = 1 if(x==1){ console.log("this is if& ...

  5. Java泛型方法与泛型类的使用------------(五)

    泛型的本质就是将数据类型也参数化, 普通方法的输入参数的值是可以变的,但是类型(比如: String)是不能变的,它使得了在面对不同类型的输入参数的时候我们要重载方法才行. 泛型就是将这个数据类型也搞 ...

  6. spring源码学习1

    1.下载源码: https://github.com/spring-projects/spring-framework中找到链接: git clone https://github.com/sprin ...

  7. 【比赛游记】NOIP2018游记

    往期回顾:[比赛游记]NOIP2017游记 转眼间又过去了一年,当年还是初中生的我已经摇身一变成为了AHSOFNU的高一学生. 回顾这一年我好像也没学什么新东西,要说有用的可能就无旋Treap吧,不知 ...

  8. 【黑客免杀攻防】读书笔记7 - 软件逆向工程基础1(函数调用约定、Main函数查找)

    0x1 准备工作 1.1.准备工具 IDA:交互式反汇编工具 OllyDbg:用户层调试工具 Visual Studio:微软开发工具 1.2.基础知识 C++开发 汇编语言 0x2 查找真正的mai ...

  9. windows安装anaconda 报错failed to create anacoda menu ?

    windows安装anaconda 报错failed to create anacoda menu ? 装了无数次,每次都是 failed to create anacoda menu然后无选择忽略, ...

  10. AT91RM9200---定时器简介

    1.前言 系统定时器模块集成了3个不同的定时器 一个周期性间隔的定时器,用来为操作系统设置时基 一个看门狗定时器,可用于软件死锁时进行系统复位 一个实时时钟计数器用来记录流逝的时间 系统定时器时钟 这 ...