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). 但是,我在统计数组中元素出 ...
随机推荐
- [codeforces contest 1119 F] Niyaz and Small Degrees 解题报告 (树形DP+堆)
interlinkage: http://codeforces.com/contest/1119/problem/F description: 有一颗$n$个节点的树,每条边有一个边权 对于一个$x$ ...
- ORA-03137 - ORA-12592 TNS:BAD PACKET OR ORA-3137 故障处理
环境 操作系统:CentOS release 6.8 数据库:oracle 11.2.0.4.190115 说明:数据库psu 为19年1月份的补丁,可不间断运行,但是开发提示在执行一些批处理的时候, ...
- FileCopy文件复制
package cn.com.filecopy; import java.io.FileInputStream; import java.io.FileNotFoundException; impor ...
- MongoDB: The Definitive Guide
第一章 简介 MongoDB是面向文档的数据库,不是关系型数据库.内置对MapReduce的支持,以及对地理空间索引的支持. 丰富的数据模型 容易扩展,它所采用的面向文档的数据模型可以使其在多台服务器 ...
- unity3d 鼠标事件
using UnityEngine; using System.Collections; public class mouse : MonoBehaviour { //private Vector3 ...
- SqlServer数据库表导入SqlLite数据库表保持日期时间类型字段的格式
在写查询功能的过程中遇到一个这样的问题:按日期范围查询,sql语句是:where dt>=用户选择起始日期&&dt<=用户选择结束日期.数据库中的数据如图1,我选择的测试数 ...
- 关于编译PCL1.71
最近在编译PCL1.71时总会出现错误, 编译的时候就出现无法生成pcl_io_debug.lib 由于无法生成pcl_io_debug.lib,. 借鉴PCL中国的经验: (1):把io\inclu ...
- eclipse的小技巧
Eclipse 保存文件时自动格式化代码 很多同学不知道Eclipse有个很有用的功能,就是自动格式源代码的功能,一般大家都是直接Ctrl+Shift+F手动格式化,多浪费时间. 其实Eclipse里 ...
- day06-08初识面向对象
一.面向过程 VS 面向对象 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西.优点是:极大的降低了写程序的 ...
- javaee 文件的复制
package Shurushucu; import java.io.FileInputStream; import java.io.FileNotFoundException; import jav ...