博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 10539 - Almost Prime Numbers 素数打表
阅读量:4611 次
发布时间:2019-06-09

本文共 1794 字,大约阅读时间需要 5 分钟。

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 
#include
#include
#include
#include
#include
using namespace std ;typedef long long ll;const int N=1000000;ll a, b, prime[N + 2], now[N+2];int H[N + 2], cnt, scc;void prime_table() { cnt = 0; H[1] = 1; for(int i = 2; i <= N ; i++) { if(!H[i]) { for(int j = i + i ; j <= N ; j += i) H[j] = 1; prime[++cnt] = i; } } scc = 0; for(int i = 1 ; 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-1); printf("%lld\n", ans); } return 0;}
代码

 

转载于:https://www.cnblogs.com/zxhl/p/5113413.html

你可能感兴趣的文章
N76E003---输入捕获
查看>>
poj 1094 Sorting It All Out(拓扑排序)
查看>>
acdream B - 郭式树 (水题 卡cin,cout, 卡LL)
查看>>
BMP图像格式
查看>>
python的匿名函数lambda解释及用法
查看>>
c#遍历Dictionary使用KeyValuePair
查看>>
defineProperties属性的运用==数据绑定
查看>>
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
Python中Selenium的使用方法
查看>>
三月23日测试Fiddler
查看>>
20171013_数据库新环境后期操作
查看>>
poj 1654 && poj 1675
查看>>
运维派 企业面试题1 监控MySQL主从同步是否异常
查看>>
Docker 版本
查看>>
poj 1753 Flip Game
查看>>
在深信服实习是怎样的体验(研发测试岗)
查看>>
Linux免密码登陆
查看>>