UVa 10539 (筛素数、二分查找) Almost Prime Numbers
题意:
求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1
分析:
首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序。
对于L和U,二分查找出小于U和L的最大数的下标,作差即可得到答案。
#include <cstdio>
#include <cmath>
#include <algorithm> typedef long long LL; const int maxn = ;
const int maxp = ; bool vis[maxn + ];
int prime[maxp], cntp = ;
LL a[maxn], cnt = ; void Init()
{
int m = ;
for(int i = ; i <= m; ++i) if(!vis[i])
for(int j = i * i; j <= maxn; j += i) vis[j] = true;
for(int i = ; i <= maxn; ++i) if(!vis[i]) prime[cntp++] = i; for(int i = ; i < cntp; ++i)
{
LL temp = (LL)prime[i] * prime[i];
while(temp <= 1000000000000LL)
{
a[cnt++] = temp;
temp *= prime[i];
}
}
std::sort(a, a + cnt);
} int binary_search(LL n)
{
LL L = , R = cnt;
while(L < R)
{
LL mid = ((L + R + ) >> );
if(a[mid] <= n) L = mid;
else R = mid - ;
}
return L;
} int main()
{
Init();
int T;
scanf("%d", &T);
while(T--)
{
LL hehe, haha;
scanf("%lld%lld", &hehe, &haha);
int t = binary_search(hehe), k = binary_search(haha);
int ans = k - t;
if(a[t] == hehe) ans++;
printf("%d\n", ans);
} return ;
}
代码君
UVa 10539 (筛素数、二分查找) Almost Prime Numbers的更多相关文章
- UVa 1644 (筛素数 + 二分) Prime Gap
题意: 给出一个整数n,如果n是素数输出0,否则输出它后一个素数与前一个素数的差值. 分析: 首先用筛法把前十万个素数都筛出来,然后放到数组里.用二分找到不大于n的最大的素数的下标,如果这个素数等于n ...
- 【noi 2.7_413】Calling Extraterrestrial Intelligence Again(算法效率--线性筛素数+二分+测时)
题意:给3个数M,A,B,求两个质数P,Q.使其满足P*Q<=M且A/B<=P/Q<=1,并使P*Q最大.输入若干行以0,0,0结尾. 解法:先线性筛出素数表,再枚举出P,二分出对应 ...
- ACM-ICPC 2018 南京赛区网络预赛 J题Sum(线性筛素数)
题目链接:https://nanti.jisuanke.com/t/30999 参考自博客:https://kuangbin.github.io/2018/09/01/2018-ACM-ICPC-Na ...
- Leecode之双指针及二分查找
题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的 ...
- 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(数论)
UVA 10539 - Almost Prime Numbers 题目链接 题意:给定一个区间,求这个区间中的Almost prime number,Almost prime number的定义为:仅 ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 237C,素数打表,二分查找
C. Primes on Interval time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix
题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...
随机推荐
- php连接ftp
PHP连接ftp,发现一个很好用的类库phpseclib.英文原文 Connecting to SFTP with PHP If you need to connect to SFTP using P ...
- web页面开发笔记(不断更新)
布局: 1.左右分列2端:使用float:left+float:right,如果一边有多列的话,另一列会对齐最下面那列.所以一般把左右各设一列,一列中再细分多行. 2.div不跟随:使用clear:b ...
- flex直接访问服务器
1.读取指定文件里的xml: <?xml version="1.0" encoding="utf-8"?><s:WindowedApplica ...
- 1046 Shortest Distance (20)
#include<stdio.h> int main() { int n,m,a,b,tem,pre,p; int i,j; ]; while(scanf("%d",& ...
- Mysql 备份恢复之 Mysqldump 工具
目前正在学习中,看到mysqldump工具导出的数据都是文本形式的,如果是blob或text大对象类型导出的是什么格式的?这个需要后续研究.下面只先总结下简单的. 一.备份1.备份Mysql一个数据库 ...
- shutdown彻底关闭tomcat,以及多线程关闭
最近做的一个Web项目,发现shutdown.sh后,无法关掉tomcat进程. ps -ef | grep tomcat 返回tomcat进程仍然存在.经过调查发现是因为在Web应用中启动了线程池, ...
- 【BZOJ 1563】 [NOI2009]诗人小G
Description Input Output 对于每组数据,若最小的不协调度不超过1018,则第一行一个数表示不协调度若最小的不协调度超过1018,则输出"Too hard to arr ...
- 【BZOJ3262】 陌上花开
Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...
- DB天气app冲刺第十天
好了 这是第十天了,按照白板任务上的来说的话,今天没有完成,所以等一下还要继续看看今天能不能把他做完,今天出的问题在于我又自己调整了一下UI设计,因为发现以前的设计发面有重复,浪费了屏幕.所以还不如省 ...
- bnuoj 1053 EASY Problem (计算几何)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=1053 [题意]:基本上就是求直线与圆的交点坐标 [题解]:这种题我都比较喜欢用二分,三分做,果然可以 ...