UVA-10539 Almost Prime Numbers
题目大意:这道题中给了一种数的定义,让求在某个区间内的这种数的个数。这种数的定义是:有且只有一个素因子的合数。
题目分析:这种数的实质是素数的至少两次幂。由此打表——打出最大区间里的所有这种数构成的表即可。
代码如下:
# include<iostream>
# include<cstdio>
# include<cmath>
# include<set>
# include<cstring>
# include<algorithm>
using namespace std;
const long long N=;
# define ll long long
int pri[],mark[];
set<ll>s;
ll mypow(int a,int b)
{
if(b==)
return a;
ll u=mypow(a,b/);
u*=u;
if(b&)
u*=a;
return u;
}
void init()
{
pri[]=;
memset(mark,,sizeof(mark));
for(int i=;i<=;++i){
if(!mark[i])
pri[++pri[]]=i;
for(int j=;j<=pri[]&&i*pri[j]<=;++j){
mark[i*pri[j]]=;
if(i%pri[j]==)
break;
}
}
for(int i=;i<=pri[];++i){
ll u=(pri[i]+0LL)*(pri[i]+0LL);///在这要对pri[i]进行强制类型转换,否则溢出。
while(u<N){
s.insert(u);
u=u*pri[i];
}
}
}
void work(ll a,ll b)
{
int ans=;
set<ll>::iterator it1,it2;
it1=lower_bound(s.begin(),s.end(),a);
it2=lower_bound(s.begin(),s.end(),b);
while(it1!=it2){
++it1;
++ans;
}
printf("%d\n",ans);
}
int main()
{
init();
int T;
ll l,r;
scanf("%d\n",&T);
while(T--)
{
scanf("%lld%lld",&l,&r);
work(l,r);
}
return ;
}
对于这一类问题:一定要理解透彻题中的新定义,否则费了半天劲写出来的东西是错的,既浪费时间,又浪费精力。
UVA-10539 Almost Prime Numbers的更多相关文章
- UVA 10539 - Almost Prime Numbers(数论)
UVA 10539 - Almost Prime Numbers 题目链接 题意:给定一个区间,求这个区间中的Almost prime number,Almost prime number的定义为:仅 ...
- UVA 10539 - Almost Prime Numbers 素数打表
Almost prime numbers are the non-prime numbers which are divisible by only a single prime number.In ...
- UVA - 10539 Almost Prime Numbers (几乎是素数)
题意:输入两个正整数L.U(L<=U<1012),统计区间[L,U]的整数中有多少个数满足:它本身不是素数,但只有一个素因子. 分析: 1.满足条件的数是素数的倍数. 2.枚举所有的素数, ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )
Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is ...
- 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 ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- Alexandra and Prime Numbers(思维)
Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
随机推荐
- 访问Hsql .data数据库文件
一.Hsql简介: hsql数据库是一款纯Java编写的免费数据库,许可是BSD-style的协议. 仅一个hsqldb.jar文件就包括了数据库引擎,数据库驱动,还有其他用户界面操作等内容.下载地址 ...
- Linux中Postfix虚拟用户及虚拟域(六)
Postfix基于虚拟用户虚拟域的邮件架构 上图是一个几乎完整的邮件系统架构图,这里基于Mysql数据库进行用户认证,不管是Postfix.Dovecot.webmail都需要去Mysql数据库中进行 ...
- ELK之kibana6.5
ELK之安装kibana6.5,这里采用rpm安装: https://www.elastic.co/guide/en/kibana/current/rpm.html # wget https://ar ...
- Java位运算实现加减乘除
一.加法 a+b 举例实现:13+9=22 13+9不考虑进位结果为12 只考虑进位结果为10 和刚好是22. 13二进制为1101,9二进制为1001. 不考虑进位结果为0100.算式为a^b 只考 ...
- Hadoop错误日志
1.错误日志:Directory /tmp/hadoop-root/dfs/name is in an inconsistent state: storage directory does not e ...
- 高通平台启动log概述(PBL log、sbl1 log、kernel log)【转】
本文转自:https://blog.csdn.net/RadianceBlau/article/details/78416776?utm_source=blogxgwz9 高通平台启动log概述(PB ...
- MVC5 一套Action的登录控制流程
流程: 用拦截器控制每一个页面请求和ajax请求,根据请求体的cookie里面是否有token判断是否登录,还必须判断该token在redis里面的缓存是否存在 组成部分: 拦截器: using Sy ...
- Linux rhel7 下MySQL5.7.18详细安装文档
Linux rhel7 下MySQL5.7.18详细安装文档 本文安装MySQL5.7使用的是vm虚拟机rhel7操作系统 ,ftp文件传输是FileZilla3.27,远程连接xssh5.0 1 登 ...
- VS中sln和suo的区别
1.调试程序出现这个错误an error occurred while validating 解决方案:http://stackoverflow.com/questions/8648428/an-er ...
- [SpringBoot] - 一份笔记
一. Spring Boot 入门 1. Spring Boot 简介 简化Spring应用开发的一个框架; 整个Spring技术栈的一个大整合; J2EE开发的一站式解决方案; 2. 微服务 201 ...