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 ...
随机推荐
- SpringBoot对比SpringMVC,SpringMVC 处理请求过程
(问较多:1.SpringBoot对比SpringMVC.2.SpringMVC 处理请求过程.问:springboot的理解 Spring,Spring MVC,Spring Boot 三者比较 S ...
- ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password:NO)
转自:http://blog.sina.com.cn/s/blog_586a1f3e01000b82.html 刚使用mysql, 就老是碰到这个问题,真是郁闷, 终于找到原因.. C:\Progra ...
- Linux - 文件ACL权限控制
getfacl 1.test # 查看文件ACL权限 setfacl -R -m u:xuesong:rw- 1.test # 对文件增加用户的读写权限 -R 递归
- Springboot使用FastJson后,接口返回中文乱码的问题解决。
哎,天下文章一大抄,到处都是一模一样的教你怎么替换掉jackson成fastjson的,可后续中文乱码网上居然没一篇文章.翻了一会源码还是写个文章共享下吧.免得后来人又浪费时间折腾. 在springb ...
- python - classs内置方法 solt
solt # __solt__ # 是一个类变量,变量值可以是列表.元组或者是可迭代对象,也可以是一个字符串 # (以为这所有实例只有一种数据属性) # # 作用:(为了节省内存空间,减少过多的实例属 ...
- Linq基于两个属性的分组
1.需求 我们看下面的定义 #region 学生类 /// <summary> /// 学生类 /// </summary> class Student { /// <s ...
- 【Python】【辅助程序】练手小程序:记录外网动态IP地址
练手小程序 程序作用:对IP实时记录: 1.定时获取外网IP,存储在本地文件中: 编写思路: 1)收集获取外网的API接口 http://bbs.125.la/thread-1383897 ...
- caffe中使用python定义新的层
转载链接:http://withwsf.github.io/2016/04/14/Caffe-with-Python-Layer/ Caffe通过Boost中的Boost.Python模块来支持使用P ...
- kafka系列五、kafka常用java API
引入maven包 <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka- ...
- linux软链接和硬链接的区别
硬链接:ln 3.txt 4 相当于把源文件复制了一份 软连接:ln -s 3.txt 5 相当于快捷方式 改动源文件4.5同时更新,删除3.txt ,5不存在,4存在的.