UVA - 10539 Almost Prime Numbers (几乎是素数)
题意:输入两个正整数L、U(L<=U<1012),统计区间[L,U]的整数中有多少个数满足:它本身不是素数,但只有一个素因子。
分析:
1、满足条件的数是素数的倍数。
2、枚举所有的素数,以及其倍数,将满足条件且小于等于n的个数计算出来,solve(u) - solve(l - 1)即可。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b) {
if(fabs(a - b) < eps) return 0;
return a < b ? -1 : 1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1e6 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int vis[MAXN];
vector<LL> prime;
void init(){
for(int i = 2; i < MAXN; ++i){
if(!vis[i]){
prime.push_back(i);
for(int j = 2 * i; j < MAXN; j += i){
vis[j] = 1;
}
}
}
}
LL solve(LL n){
LL ans = 0;
int len = prime.size();
for(int i = 0; i < len; ++i){
LL tmp = prime[i] * prime[i];
if(tmp > n) break;
while(tmp <= n){
++ans;
tmp *= prime[i];
}
}
return ans;
}
int main(){
init();
int T;
scanf("%d", &T);
while(T--){
LL l, u;
scanf("%lld%lld", &l, &u);
printf("%lld\n", solve(u) - solve(l - 1));
}
return 0;
}
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 1415 - Gauss Prime(数论,高斯素数拓展)
UVA 1415 - Gauss Prime 题目链接 题意:给定a + bi,推断是否是高斯素数,i = sqrt(-2). 思路:普通的高斯素数i = sqrt(-1),推断方法为: 1.假设a或 ...
- How many prime numbers(求素数个数)
How many prime numbers Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 2138 How many prime numbers (判素数,米勒拉宾算法)
题意:给定一个数,判断是不是素数. 析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法. 代码如下: #include <iostream> #include <c ...
- 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 ...
- UVa 1210 (高效算法设计) Sum of Consecutive Prime Numbers
题意: 给出n,求把n写成若干个连续素数之和的方案数. 分析: 这道题非常类似大白书P48的例21,上面详细讲了如何从一个O(n3)的算法优化到O(n2)再到O(nlogn),最后到O(n)的神一般的 ...
- 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 素数打表
第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...
随机推荐
- [排错] VO对象和POJO对象的关系
这或许是一个很蠢的笔记吧...... 这次项目中, 作为一个新人, 没少被这两个概念虐得死去活来的, 现在特别做一次记录, 关于它们二者之间在项目中的应用. 在这里呢, 就不再赘述 VO(view o ...
- HTTP协议调试工具汇总
前言 本文收集了大量抓包工具,近40款,涵盖了各种开发语言(Java,C#,Delphi,C,C++,Objective-C,Node.js,Go,Python).各类前端(GUI,TUI,CUI,W ...
- jenkins 2.204.2 安装, 使用国内源安装, 并且跳过插件界面, 更新成国内插件源.
需要java环境支持,自行百度. jenkins 安装源在国外, 下载会比较慢, 尤其在linux下, 使用yum或者apt install jenkins方式安装时,经常会下载失败. 由于yum或者 ...
- 700k把web端程序包装为桌面程序
electron因为自带cef所以体积巨大,还不是因为windows没有chromium的webview嘛,现在有了新edge后,这个项目通过依赖各个平台的webview,并依赖.net core,做 ...
- JavaWeb开发:从购买服务器到简单demo运行
写这篇文章的目的: 一个是为了记录实施过程,方便自己日后查阅: 另一个是给项目组成员提供一个参考,方便他们以后搭建自己的项目环境: 当然若能帮助到更多的朋友,那就再好不过了:D 需要注意: 我本身也是 ...
- 小程序canvas 变换
var ctx = wx.createCanvasContext('base'); var centerX = 375/ 2; var centerY = 200; var rotate = 90; ...
- Jquery实现列表增删改
//需求:对列表的增删改 //代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- 判断ES数据是否更新成功
参考:https://stackoverflow.com/questions/38928991/how-to-detect-if-a-document-update-in-elasticsearch- ...
- oracle根据一张表更新另外一张表
知道是两张表进行更新,之前作过mysql的,直接就写了: update a,b set a.code = b.code wehre a.id = b.id 然后就报错了,上网查了下知道oracle不能 ...
- MongoDB_01
解释:MongoDB可应对 --三高需求 High performance-对数据库高并发读写的需求 Huge Storage -对海量数据的高效率存储和访问的需求 High Scalability ...