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 ...
随机推荐
- (生产)vuex - 状态管理
参考:https://vuex.vuejs.org/zh-cn/ 安装 直接下载 / CDN 引用 https://unpkg.com/vuex在 Vue 之后引入 vuex 会进行自动安装:< ...
- 什么叫erp系统
一般来说,erp系统是一个以会计(此处的会计是指管理会计)为核心的信息系统,用来识别和规划企业资源, 从而获取客户订单, 完成加工和交付,最后得到客户付款,最终获得收入和利润. 实际上, erp 系统 ...
- (12)JavaScript之[事件][事件监听]
事件 /** * 事件: * onload 和 onunload 事件在用户进入或离开页面时被触发 * * onchange事件常结合对输入字段的验证来使用 * onmouseover 和 onmou ...
- JQuery中Checkbox选择
判断是否选中 $(this).is(":checked") 取消选中 $(this).prop("checked", false) 选中 $(this).pro ...
- 使用uwsgi启动django项目
在 manage.py 同级目录 创建 uwsgi.ini 文件 ,内容如下: [uwsgi] # 对外提供 http 服务的端口 http = :18123 #the local unix sock ...
- 怎样解决putty终端乱码的方法
原文地址:https://jingyan.baidu.com/article/3aed632e5f00ae701080913a.html?qq-pf-to=pcqq.c2c 终端输入:echo $LA ...
- windows server 安装之后需要做的操作
一.运行windows update安装更新 提示: 若一直停留在“正在检查更新”,请参考https://answers.microsoft.com/zh-hans/windows/forum/win ...
- CRUD全栈式编程概述
业务场景 CRUD,从数据驱动的角度几乎所有的的业务都是在做这样的事情. 几乎所有的操作都是在做对表的增删改查. 假设我们将数据库数据规个类: 分为基础/配置数据和业务/增长数据,或者说静态数据 ...
- Codeforces 760A Petr and a calendar
题目链接:http://codeforces.com/problemset/problem/760/A 题意:日历需要多少列. #include <bits/stdc++.h> using ...
- G711格式语音采集/编码/转码/解码/播放
2019-05-01 语音g711格式和AMR格式类似,应用很简单,很多人已经整理过了,收录于此,以备不时之需,用别人现成的足矣,我们的时间应该用来干更有意义的事. 1.PCM to G711 Fas ...