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). 但是,我在统计数组中元素出 ...
随机推荐
- 爱,死亡和机器人 第十四集 齐马蓝 中文字幕(Python处理utf8文件获取想要的内容)
处理代码 file = "a.srt" fi = open(file, mode='r') a = fi.readline() i = 1 while len(str(a)) != ...
- Java单例模式 多种实现方式
一:通过静态私有成员实现单例模式 (1):私有化构造函数 (2):new静态实例属性对象,加锁. 单例类: package SinglePag; /* * 构造函数私有化,结合锁+静态的概念 实现单例 ...
- hdu2819 Swap 最大匹配(难题)
题目大意: 给定一个元素的值只有1或者0的矩阵,每次可以交换两行(列),问有没有方案使得对角线上的值都是1.题目没有限制需要交换多少次,也没限制行交换或者列交换,也没限制是主对角线还是副对角线.虽然没 ...
- mysql简单语句
创建名为user的数据库: create database user; 显示所有数据库: show databases; 选择名为user的数据库: use user; 显示所有表: show tab ...
- SciPy和Numpy处理能力
1.SciPy和Numpy的处理能力: numpy的处理能力包括: a powerful N-dimensional array object N维数组: advanced array slicing ...
- 初级模拟电路:1-2 PN结与二极管
回到目录 1. 掺杂半导体 上面我们分析了本征半导体的导电情况,但由于本征半导体的导电能力很低,没什么太大用处.所以,一般我们会对本征半导体材料进行掺杂,即使只添加了千分之一的杂质,也足以改变半导 ...
- 判断list数组里的json对象有无重复,有则去重留1个
查找有无重复的 var personLength = [{ certType: '2015-10-12', certCode:'Apple'}, { certType: '2015-10-12', c ...
- C语言提高 (4) 第四天 数组与数组作为参数时的数组指针
1昨日回顾 const int 和 int const是一样的 const char *p;值不变 char * const p; 指针不能变 编译器对参数的退化: 第三种模型: 三级指针 三级指针局 ...
- 最长上升子序列(LIS)与最长公共子序列(LCS)
1.LIS : 给定一个序列,求它的最长上升子序列(n<=2000) 第一种 O(n^2): dp[i] 为以i为开头的最长上升子序列长度 code1: #include<cstdio&g ...
- String,StringBuffer,StringBuild的区别
1.三者在执行速度方面的比较:StringBuilder > StringBuffer > String 2.String <(StringBuffer,StringBuild ...