UVA 10539 - Almost Prime Numbers 素数打表
Almost prime numbers are the non-prime numbers which are divisible by only a single prime number.
In this problem your job is to write a program which finds out the number of almost prime numbers
within a certain range.
Input
First line of the input file contains an integer N (N ≤ 600) which indicates how many sets of inputs
are there. Each of the next N lines make a single set of input. Each set contains two integer numbers
low and high (0 < low ≤ high < 1012).
Output
For each line of input except the first line you should produce one line of output. This line contains
a single integer, which indicates how many almost prime numbers are within the range (inclusive)
low . . . high.
Sample Input
3
1 10
1 20
1 5
Sample Output
3
4
1
题意:给你 一个范围 a,b,找出这个范围中 满足 x = p^k (p为素数,k > 1) 的数的个数
题解: ab,范围是10的12次方 我们找出1e6内的素数 ,打表出所有可能形成 的数去重排序 ,每次二分找下标就好了
ans(b) - ans(a-1)就是答案
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<vector>
using namespace std ;
typedef long long ll; const int N=;
ll a, b, prime[N + ], now[N+];
int H[N + ], cnt, scc;
void prime_table() {
cnt = ;
H[] = ;
for(int i = ; i <= N ; i++) {
if(!H[i]) {
for(int j = i + i ; j <= N ; j += i) H[j] = ;
prime[++cnt] = i;
}
}
scc = ;
for(int i = ; i <= cnt ; i++) {
for(ll j = prime[i] * prime[i] ; j <= 1e12 ; j *= prime[i]) {
now[scc++] = j;
}
}
sort(now, now + scc);
scc = unique(now,now + scc) - now ;
}
ll solve(ll x) {
ll ans = upper_bound(now,now + scc,x) - now;
return ans;
}
int main() {
prime_table();
int T;
scanf("%d",&T);
while(T--) {
scanf("%lld%lld",&a,&b);
ll ans = solve(b) - solve(a-);
printf("%lld\n", ans);
}
return ;
}
代码
UVA 10539 - Almost Prime Numbers 素数打表的更多相关文章
- UVA 10539 - Almost Prime Numbers(数论)
UVA 10539 - Almost Prime Numbers 题目链接 题意:给定一个区间,求这个区间中的Almost prime number,Almost prime number的定义为:仅 ...
- CodeForces 385C Bear and Prime Numbers 素数打表
第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...
- UVA - 10539 Almost Prime Numbers (几乎是素数)
题意:输入两个正整数L.U(L<=U<1012),统计区间[L,U]的整数中有多少个数满足:它本身不是素数,但只有一个素因子. 分析: 1.满足条件的数是素数的倍数. 2.枚举所有的素数, ...
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
- HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)
Problem Description Give you a lot of positive integers, just to find out how many prime numbers the ...
- uva 1415 - Gauss Prime(高斯素数)
题目链接:uva 1415 - Gauss Prime 题目大意:给出一个a,b,表示高斯数a+bi(i=−2‾‾‾√,推断该数是否为高斯素数. 解题思路: a = 0 时.肯定不是高斯素数 a != ...
- poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19697 ...
- POJ-3126 Prime Path---BFS+素数打表
题目链接: https://vjudge.net/problem/POJ-3126 题目大意: 给两个四位数a,b 每次改变a中的一位而且改动之后的必须是素数,问最少改动几次可以到b?(永远达不到b就 ...
- Codeforces 385C Bear and Prime Numbers(素数预处理)
Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...
随机推荐
- maven使用杂记
maven test使用记录 运行指定的测试类: >mvn test -Dtest=[ClassName] 运行测试类中指定的方法:(这个需要maven-surefire-plugin: ...
- lua环境变量
function foo() print(g or "'g' is not defined!") end foo() env = { g = 100, print = print ...
- 前端-js进阶和JQ源码思维导图笔记
看不清的朋友右键保存或者新窗口打开哦!喜欢我可以关注我,还有更多前端思维导图笔记
- 错误:Camera录制视频(6.0错误),5.1正常,7.1正常 (java.lang.RuntimeException: start failed.at android.media.MediaRecorder.native_start(Native Method))
Process: com.example.mycamera2, PID: 24086 java.lang.RuntimeException: start failed. at android.medi ...
- vue-cli 安装
1 node 下载 http://nodejs.cn/download/ 安装 2 npm install vue-cli -g 3 vue init <template-n ...
- linux+nginx+python+django环境配置
Django是一个开放源代码的Web应用框架,由Python写成,它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的.python+django也是web开发者最受欢迎的框架.今天 ...
- DDoS攻击与防范策略
DDoS(Distributed Denial of Service,分布式拒绝服务)攻击的主要目的是让指定目标无法提供正常服务,甚至从互联网上消失,是目前最强大.最难防御的攻击之一. 按照发起的方式 ...
- winserver2012安装.net 3.5
运行 dism.exe /online /enable-feature /featurename:NetFX3 /Source:I:\sources\sxs
- mysql 各项操作流程
启动mysql:进入命令行输入:net start mysql 如果失败则显示:服务名无效,需跳转到指定Bin目录下进行启动mysql, 成功则进行下一步:登陆 :mysql -uroot -proo ...
- vc++图像保存,重绘
新建mfc应用程序,单文档 增加绘图 分别增加命令响应 添加成员变量UINIT 图形可以运行,如何保存呢?(一个集合类,CPtArt) 用一个类的对象来保存一个图形的三个要素 所以插入一个新的类(通常 ...