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 ...
随机推荐
- 在使用angular和swiper插件中的一些问题
在使用angular获取swiper图片的时候swiper就不会轮播. 解决方法: 1.使用计时器的方法,不是最优 settimeOut(function(){ mySwiper = new Swip ...
- vue本地和线上环境(域名)配置
vue本身为运行脚手架项目自家搭载了一个nodejs后台环境,本地可通过proxyTable来处理跨域问题,但是上线(或生产环境)之后改域名真是一件麻烦的事情,所以进行一些配置. config/ind ...
- (WPF) ComboBox 之绑定
1. 在UI(Xaml) 里面直接绑定数据. <Window x:Class="WpfTutorialSamples.ComboBox_control.ComboBoxSample& ...
- ARM平台指令虚拟化初探
0x00:什么是代码虚拟化? 虚拟机保护是这几年比较流行的软件保护技术.这个词源于俄罗斯的著名软件保护软件“VmProtect”,以此为开端引起了软件保护壳领域的革命,各大软件保护壳都将虚拟机保护这一 ...
- Quick-Cocos2d-x Lua脚本加密打包器
准备开新项目了,在寻找合适的框架,后来就发现了Quick-Cocos2d-x这玩意. 别说,还挺好使.之后一步步研究,发现Lua不加密是不行的. 加密的方法在这里. 因为在做版本更新的时候,一般大家都 ...
- MVC 默认路由 Areas
1.使用重名controller 在asp.net mvc2以后的版本里面,有了area(区域的概念),这为我们开发中提供了不少方便的地方,但是很不凑巧,若是存在多个重名的controller就会发生 ...
- selenium代理
selenium.KeyDown("id=ctaskName", "d"); selenium.KeyPress("id=cta ...
- myVision云服务商业数据分析解决方案
类型: 定制服务 软件包: business intelligence internet retailing solution collateral 联系服务商 产品详情 解决方案 概要 2014年, ...
- 2017.11.1 微型计算机原理与接口技术-----第七章 中断系统与8237A DMA控制器
第七章 微型计算机原理与接口技术-----中断系统与8237A DMA控制器 (1)数据传送的两种方式:中断方式和直接存储器存取方式(DMA):中断是微处理器与外部设备交换信息的一种方式:DMA是存储 ...
- C语言文件操作类型速查
文件使用方式 含义 "r"(只读) 为输入打开一个文本文件,不存在则失败 "w"(只写) 为输出打开一个文本文件,不存在则新建,存在则删除后再新建 " ...