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 < ...
随机推荐
- 学习W3SCHOOL 表单验证
//表单学习笔记 //建立一张表单的验证 <!DOCTYPE html> <html> <head> <meta http-equiv="Conte ...
- 题目1444:More is better
时间限制:3 秒 内存限制:100 兆 特殊判题:否 提交:1362 解决:640 题目描述: Mr Wang wants some boys to help him with a project. ...
- Ubuntu Linux启用root用户登录
Ubuntu Linux有一个与众不同的特点,那就是初次使用时,你无法作为root来登录系统,为什么会这样?这就要从系统的安装说起.对于其他Linux系统来 说,一般在安装过程就设定root密码,这样 ...
- 用火狐打开PDF文件
可以直接使用官方的Adobe Reader插件来实现在火狐中浏览PDF文件的功能.在你浏览一个PDF文件的时候,火狐将会尝试下载安装这个插件. 如果这个插件出现问题,那么就无计可施啦. 检查火狐的设置 ...
- vs中的主题配置
比较讨厌vs的默认字体颜色及背景,代码看久了就受不了.不过自己手动配置的话还是比较麻烦的,这里有一个网站(http://studiostyl.es/)提供了很多主题,一般都是背景比较深色的,对眼睛比较 ...
- unity3d引擎程序员养成
标准流程:1. c++ Primer 英文版(第四或第五版)全部看完习题做完是必须的.渲染程序设计比较复杂,后期会用到c++的全部特性.c++学的越好后面越轻松.要看英文版,计算机翻来覆去就那么几个单 ...
- First Groovy
class Sample { def names = ["anna", "annie", "tommy", "bobby" ...
- 1198: [HNOI2006]军机调度 - BZOJ
Description 凯萨拥有一支由n个人组成的雇佣军,他们靠在威尼斯商行接任务过活.这支军队的成份比较复杂,不同的人往往具有不同的技能,有的人还拥有多项技能.威尼斯商行的任务也参差不齐,有的需要几 ...
- PAT-乙级-1015. 德才论 (25)
1015. 德才论 (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Li 宋代史学家司马光在<资治通鉴&g ...
- Educational Codeforces Round 5 B
Problem B:http://codeforces.com/contest/616/problem/B B. Dinner with Emma 题意:一对夫妻要去餐厅吃晚饭,Emma 想去最豪华( ...