AOJ 0009 Prime Number
题意:给出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的更多相关文章
- AOJ - 0009 Prime Number (素数筛法) && AOJ - 0005 (求最大公约数和最小公倍数)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34870 求n内的素数个数. /* ********************* ...
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 每日一九度之 题目1040:Prime Number
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...
- 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 ...
- 10 001st prime number
这真是一个耗CPU的运算,怪不得现在因式分解和素数查找现在都用于加密运算. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13 ...
- [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 ...
- [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 ...
- 10_ for 练习 _ is Prime Number ?
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 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 ...
随机推荐
- 正方体旋转demo
接着上一篇博客的内容顺藤摸瓜往下想,既然stage有景深这个概念,可以表达3D场景,那么这个stage就可以呈现立体几何咯,于是自己写了个Cubic Demo 一个正方体有6个面,我们把一个正方体平铺 ...
- utf8_bin跟utf8_general_ci的区别
ci是 case insensitive, 即 "大小写不敏感", a 和 A 会在字符判断中会被当做一样的; bin 是二进制, a 和 A 会别区别对待. 例如你运行: SEL ...
- python安装办法
先我们来安装python 1.首先进入网站下载:点击打开链接(或自己输入网址https://www.python.org/downloads/),进入之后,选择64位下载. 2.下载完成后如下图所示 ...
- QWidget窗口类
import sys from PyQt5.QtWidgets import QWidget, QApplication,QPushButton from PyQt5.QtGui import QIc ...
- include的作用
#include发生在预处理阶段,整个编译链接过程,#include是最简单的了,没有之一.就是在include的位置直接把文件原原本本完完整整一字不落的包含进来,下面举一个极端点的例子: //fil ...
- mongodb系列~mongodb的副本集(1)
一 简介: mongodb副本集 二 复制方式: 1 全量复制 2 增量复制三 同步检测过程: 一 正常情况下: 1 master执行语句,并将所有的修改数据库的操作以日志Oplog ...
- 让webstrom更好用的设置
一.让webstrom在编辑vue项目时更快 1.在webstrom的项目管理树中,找到node_modules文件夹,在文件夹上点右键,在出来的右键菜单中选择“Mark Directory as” ...
- Docker镜像原理
⒈是什么? 镜像是一种轻量级.可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容,包括代码.运行时.库.环境变量以及配置文件等. 引用 UnionFs( ...
- 梯度优化算法总结以及solver及train.prototxt中相关参数解释
参考链接:http://sebastianruder.com/optimizing-gradient-descent/ 如果熟悉英文的话,强烈推荐阅读原文,毕竟翻译过程中因为个人理解有限,可能会有谬误 ...
- 分布式系列 - dubbo服务telnet命令【转】
dubbo服务发布之后,我们可以利用telnet命令进行调试.管理.Dubbo2.0.5以上版本服务提供端口支持telnet命令,下面我以通过实例抛砖引玉一下: 1.连接服务 测试对应IP和端口下的d ...