Aizu:0009- Prime Number
Prime Number
Time limit 1000 ms
Memory limit 131072 kB
Problem Description
Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n. A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.
Input
Input consists of several datasets. Each dataset has an integer n (1 ≤ n ≤ 999,999) in a line.
The number of datasets is less than or equal to 30.
Output
For each dataset, prints the number of prime numbers.
Sample Input
10
3
11
Output for the Sample Input
4
2
5
解题心得:
- 题意就是给你一个数n,问你在不大于n的数里有多少个素数。
- 就是考了一个素数筛选,可以将素数找出来,然后二分查找n的位置,也可以直接得出前缀和。
二分查找
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <vector>;
using namespace std;
const int maxn = 1e6+100;
bool prim[maxn];
vector <int> ve;
void get_prim() {
prim[0] = prim[1] = true;
for(int i=2;i<maxn;i++) {
if(prim[i])
continue;
ve.push_back(i);
for(int j=i*2;j<maxn;j+=i) {
prim[j] = true;
}
}
}
int main() {
int n;
get_prim();
while(scanf("%d",&n) != EOF) {
int pos = upper_bound(ve.begin(),ve.end(),n) - ve.begin();
if(ve[pos] > n)
pos--;
printf("%d\n",pos+1);
}
return 0;
}
前缀和的代码
#include <algorithm>
#include <cstring>
#include <stdio.h>
using namespace std;
const int maxn = 1e6+100;
int sum[maxn];
bool prim[maxn];
void get_prim() {
prim[1] = prim[0] = true;
for(int i=2;i<maxn;i++) {
if(prim[i])
continue;
for(int j=i*2;j<maxn;j+=i) {
prim[j] = true;
}
}
}
void get_sum() {
int cnt = 0;
for(int i=0;i<maxn;i++) {
if(!prim[i])
cnt++;
sum[i] = cnt;
}
}
int main() {
get_prim();
get_sum();
int n;
while(scanf("%d",&n) != EOF) {
printf("%d\n",sum[n]);
}
return 0;
}
Aizu:0009- Prime Number的更多相关文章
- AOJ - 0009 Prime Number (素数筛法) && AOJ - 0005 (求最大公约数和最小公倍数)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34870 求n内的素数个数. /* ********************* ...
- AOJ 0009 Prime Number
题意:给出n,求不大于n的素数有多少个. 算法:先用线性时间复杂度的筛法打素数表,对于每个输入统计不超过的素数个数. #include <cstdio> int p[100010]; bo ...
- 题目1040:Prime Number(第k个素数)
题目链接:http://ac.jobdu.com/problem.php?pid=1040 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- 每日一九度之 题目1040:Prime Number
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...
- 九度OJ 1040:Prime Number(质数) (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5278 解决:2180 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...
- 【九度OJ】题目1040:Prime Number 解题报告
[九度OJ]题目1040:Prime Number 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1040 题目描述: Ou ...
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 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 ...
- [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 ...
随机推荐
- JavaScript 原型链 OOP(二)
原型对象 `prototype` - 原型对象的所有属性和方法,都能被实例对象共享; JavaScript 通过构造函数生成新对象,因此构造函数可以视为对象的模板.实例对象的属性和方法,可以定义 ...
- 微信小程序电商实战-入门篇
小程序开发工具有新版本更新啦!开发体验更好了,详情可以查看微信公众平台-小程序https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.h ...
- oracle笔记2-多表查询和子查询
--查询出当前用户下的所有表 select table_name from user_tables; --执行顺序原则: from where group by having select ...
- java:图片压缩
java使用google开源工具实现图片压缩 :http://www.cnblogs.com/linkstar/p/7412012.html
- centos6.5_64bit_tomcat日志合并在一个.log下
问题 tomcat每次启动时,自动在logs目录下生产以下日志文件,且每天都会生成对应日期的一个文件,造成日志文件众多: 目的 Tomcat以上日志都输出到同一个文件中. 修 ...
- prop & attr
<input id="chk1" type="checkbox" />是否可见 <input id="chk2" type ...
- anaconda添加源(channels)
如果使用environment.yml下载conda环境时,系统很慢,那么可以增加其他的下载源: - https://conda.anaconda.org/menpo - https://mirror ...
- leetcode:查找
1. word ladder 题目: Given two words (start and end), and a dictionary, find the length of shortest t ...
- SAP成都研究院Sunshine: 我的C4C实习感受和保研之路
今天的文章来自SAP成都一位实习生,曾经和Jerry同在C4C成都开发团队一起工作过.在Sunshine最后一个工作日里,Jerry和Sunshine一起吃饭的时候,她曾经聊到接下来的保研打算和将来工 ...
- 在jupyter notebook 中同时使用安装不同版本的python内核-从而可以进行切换
在安装anaconda的时候,默认安装的是python3.6 但是cs231n课程作业是在py2.7环境下运行的.所以需要在jupyter notebook中安装并启用python2.7版本 方法: ...