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 < ...
随机推荐
- Review PHP设计模式之——注册模式
注册模式: class DbConnections{ var $_store = array(); public function isValid($key) { return isset($this ...
- Delphi Variant oleVariant
The OleVariant type exists on both the Windows and Linux platforms. The main difference between Vari ...
- linux内核驱动中_IO, _IOR, _IOW, _IOWR 宏的用法与解析(引用)
在驱动程序里, ioctl() 函数上传送的变量 cmd 是应用程序用于区别设备驱动程序请求处理内容的值.cmd除了可区别数字外,还包含有助于处理的几种相应信息. cmd的大小为 32位,共分 4 个 ...
- 【POJ2104】kth num
You are working for Macrohard company in data structures department. After failing your previous tas ...
- C# Windows - ListView
ListView控件的属性 属性 说明 Activation 控制用户在列表视图中激活选项的方式Standard - 用户为自己的计算机选择的值OneClick – 单击一个选项,激活它TwoClic ...
- Jplayer(转)
Jplayer必须要加载 1.样式 jplayer.blue.monday.css 2.jq jquery.1.6.2.min.js 当前最新版本为1.6.2 3.jplayer的js jquery ...
- nginx配置location [=|~|~*|^~] /uri/ { … }用法
版权声明:https://github.com/wusuopubupt ====== nginx location语法 基本语法:location [=|~|~*|^~] /uri/ { … } = ...
- No qualifying bean of type [com.shyy.web.service.TreeMapper] found for dependency
异常信息: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.sp ...
- ExtJS4.2学习(11)可拖放的表格(转)
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-18/180.html --------------- ...
- mysql 获取季度的第一天 本月的第一天,本周的第一天sql语句(转)
感谢:http://www.111cn.net/database/110/d45124323da8d2d87b80f78319987eda.htm 查看同主题的另一篇博客:http://blog.cs ...