UVA
来源:互联网 发布:淘宝联华文具 编辑:程序博客网 时间:2024/06/09 19:15
题解:
求区间[l,r]内这个数不是素数且素因子只有一个的个数。用筛法求素数将素数筛出来,然后然后将一个素数累乘存入容器中,然后用二分求区间内的个数。
Almost prime numbers are the non-prime numbers which are divisible by only a single prime number.In this problem your job is to write a program which finds out the number of almost prime numberswithin a certain range.
Input
First line of the input file contains an integerN (N≤ 600) which indicates how many sets of inputsare there. Each of the nextN lines make a single set of input. Each set contains two integer numberslowand high(0 < low≤ high <1012).
Output
For each line of input except the first line you should produce one line of output. This line containsa single integer, which indicates how many almost prime numbers are within the range (inclusive)low...high.
Sample Input
3
1 101 2015
Sample Output
341
#include <iostream>#include <stdio.h>#include <cmath>#include <algorithm>#include <iomanip>#include <cstdlib>#include <string>#include <memory.h>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <ctype.h>#include<time.h>#define INF 1000000using namespace std;bool _prime[1000010];long long primes[78500];vector<long long> ans;int cnt=0;void make_prime(){ memset(_prime,false,sizeof(_prime)); for(int i=2;i<=1000;i++) { if(!_prime[i]) { for(int j=i*i;j<=1000005;j+=i) { _prime[j]=true; } } } for(int i=2;i<=1000005;i++) { if(!_prime[i]) primes[++cnt]=i; }}long long find(long long n,int a,int b){ if(n<ans[0]) return 0; if(n>ans[ans.size()-1]) return ans.size(); int mid=(a+b)/2; if(ans[mid]==n) return mid; if(a+1==b) return mid+1; if(n>ans[mid]) return find(n,mid,b); else return find(n,a,mid);}int main(){ make_prime(); for(int i=1;i<=cnt;i++){ long long tmp=primes[i]*primes[i]; while(tmp<=1000000000000LL){ ans.push_back(tmp); tmp*=primes[i]; } } sort(ans.begin(),ans.end()); int t; cin>>t; long long L,R; while(t--){ cin>>L>>R; int l1=find(L,0,ans.size()); int r1=find(R,0,ans.size()); cout<<r1-l1<<endl; } return 0;}
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- 【算法】欧几里德、毕达哥拉斯、帕斯卡和蒙特去公园已知三点组成平行四边形,求第四点
- CIDR地址块及其子网划分(内含原始IP地址分类及其子网划分的介绍)
- java算法之快速排序
- Vue2 全局-Vue.extend构造器拓展
- python里的协程
- UVA
- 线索二叉树(复习)
- (欧) 第18章 计算机网络及分布式系统
- 网络流24题-14
- VUE开发扫雷
- selenium自动化测试
- 【C】C语言核心知识点总结(Reference Manual)
- 学习MongoDB 七: MongoDB索引(索引基本操作)(一)
- es6中模块化:export和export default的区别