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). 但是,我在统计数组中元素出 ...
随机推荐
- javascript设计模式-继承
javascript继承分为两种:类式继承(原型链.extend函数).原型式继承(对继承而来的成员的读和写的不对等性.clone函数). 类式继承-->prototype继承: functio ...
- Android应用优化之代码检测优化
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...
- 推荐10个超棒的jQuery工具 提示插件
脚本之家 http://www.jb51.net/article/28525.htm
- C++调用Matlab 注意事项
前言:脑残的使用了C++调用Matlab,没想到Matlab的使用者的智商还真TMD不一般, 竟然有这样的 plot(x_Abnorm_index,Vec2(Abnorm_index),'sb','l ...
- Linux 中文件名颜色所代表的属性
1. 白色:表示一般文件 2. 蓝色:表示目录 3. 绿色:表示可执行的文件或程序 4. 浅蓝色:表示链接文件 5. 黄色:表示设备文件 6. 灰色:表示其他类型文件 7. 红色:表示压缩文件或者包文 ...
- [NOI2005]瑰丽华尔兹_动态规划_单调队列
Code: #include<cstdio> #include<cstring> #include<deque> #include<algorithm> ...
- nginx视频服务缓存方案设置指导
本文描述了如何通过设置nginx缓存达到降低服务器后端压力的效果以及结合nginx第三方插件ngx_cache_purge实现nginx缓存后的自动清理功能.具体实施步骤如下所示:第一步:获取清除清除 ...
- js阻止点击事件的冒泡的实现
<html> <head> <script type="text/javascript"> function fnclick1(){ alert ...
- Bash 如何取得当前正在执行的脚本的绝对路径?
转自:http://blogread.cn/it/article/6549?f=wb Bash 如何取得当前正在执行的脚本的绝对路径? 如题,一般我们写Shell脚本的时候,都倾向使用绝对路径,这样无 ...
- Android开发人员必备的10 个开发工具
工欲善其事,必先利其器,Android SDK 本身包含很多帮助开发人员设计.开发.测试和发布 Android 应用的工具,在本文中,我们将讨论 10 个最常用的工具. 1.Eclipse ADT ...