题意:给出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. 致敬Python 2.7! 致敬unicode函数!

    致敬Python 2.7! 致敬unicode函数! 终于下定决心放弃python 2.7, 拥抱Python 3.x的阵营了. 因为老是被中文编码虐待, 受够了. 同时也把机器里的widows XP ...

  2. This page is about building Firefox Desktop

    This page is about building Firefox Desktop The Mozilla build system, like the rest of the Mozilla c ...

  3. 第三周结对项目--小学生四则运算CAI软件汇报及总结(UI/web)

    前言: 这周是和我队友苏卫喜一起结对开发,我主要是写项目文档需求分析,她是通过我的需求文档来进行做思维导图,之后我们通过思维导图一起讨论用户界面设计. 以下就是我的需求分析1.0版本 1.   软件名 ...

  4. Python中的部分特殊属性

    __name__ is the class name;   返回类名 __module__ is the module name in which the class was defined;   定 ...

  5. python(十四)新式类和旧式类

    这里有个重要概念呢在下面那个链接 http://blog.csdn.net/zimou5581/article/details/53053775 http://www.cnblogs.com/btch ...

  6. AutoML技术现状与未来展望

    以下内容是对AutoML技术现状与未来展望讲座的总结. 1.机器学习定义 <西瓜书>中的直观定义是:利用经验来改善系统的性能.(这里的经验一般是指数据) Mitchell在<Mach ...

  7. Maven入门-安装及配置(一)

    0.Maven简介 三种仓库:

  8. ditto复制增强

    1.下载 http://ditto-cp.sourceforge.net/ 2.用法   ctrl+` ctrl+数字 或者  ctrl +`  然后用鼠标选择 soeasy

  9. python3之协程

    1.协程的概念 协程,又称微线程,纤程.英文名Coroutine. 线程是系统级别的它们由操作系统调度,而协程则是程序级别的由程序根据需要自己调度.在一个线程中会有很多函数,我们把这些函数称为子程序, ...

  10. 默认以管理员身份运行VS2013/15/17

    方法如下: 1.右击VS的快捷方式,选择[属性],打开属性对话框,再点击[高级]按钮,如下图所示: 2.再勾选[用管理员身份运行],点击[确定]即可: 然后就可以双击VS快捷方式,直接以管理员身份运行 ...