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 ...
随机推荐
- ZOJ 4019 Schrödinger's Knapsack
Schrödinger's Knapsack Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid has a magical knap ...
- Vue加载json文件
一.在build/dev-server.js文件里 var app = express() 这句代码后面添加如下(旧版): var appData = require('../address.json ...
- C# Dictionary序列化/反序列化
[Serializable] public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue& ...
- GDAL中文件的驱动及对应的文件格式(转) https://blog.csdn.net/haojknm/article/details/5851968
GDALDriver * poDriver; char *pszFormat=GetOutimgInfo(m_strInput); poDriver = GetGDALDriverManager()- ...
- C++:error 1189(转)
在VS 2013中编译程序时出现错误: 错误提示1: error C1189: #error : Building MFC application with /MD[d] (CRT dll versi ...
- .net 读写xml
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- MySQL中查询行数最多的表并且排序
#切换到schema use information_schema; #查询数据量最大的30张表 并排序 select table_name,table_rows from tables order ...
- 北洋UAM-05LX(网口系列适用)ROS节点
参考创客智造ROS与激光雷达入门教程 说明: 介绍ROS如何接入Hokuyo网口的雷达及基本使用 测试雷达:UAM-05LX采用太网接口,如果型号是USB口的参考教程 ros wiki: http:/ ...
- ubuntu 14.04 安装 eclipse
在安装 eclipse 之前必须先安装 jdk 1. 卸载默认的 jdk,以防安装出错 sudo apt-get purge openjdk* 2. 安装 jdk1.8.0_111 下载jdk1.8. ...
- Linux压缩和解压缩类指令
⒈gzip ①gzip 文件 压缩文件,只能将文件压缩为*.gz文件 ②gunzip 文件.gz 用于解压文件 ⒉zip ①zip [选项] xxx.zip 需要压缩的内容 压缩文件和目录 常用选项: ...