UVA 10539 Almost Prime Numbers
2016-09-26 17:24
330 查看
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 numbers
within a certain range.
Input
First line of the input file contains an integer N (N ≤ 600) which indicates how many sets of inputs
are there. Each of the next N lines make a single set of input. Each set contains two integer numbers
low and high (0 < low ≤ high < 1012).
Output
For each line of input except the first line you should produce one line of output. This line contains
a single integer, which indicates how many almost prime numbers are within the range (inclusive)
low … high.
Sample Input
3
1 10
1 20
1 5
Sample Output
3
4
1
求[l,r]中有多少个数满足它本身不是质数,但只有一个质因子。
先打出1-1e6的质数表。
每一个质数x,他最大贡献就是x^p<=n,就是log(x,n)。
In this problem your job is to write a program which finds out the number of almost prime numbers
within a certain range.
Input
First line of the input file contains an integer N (N ≤ 600) which indicates how many sets of inputs
are there. Each of the next N lines make a single set of input. Each set contains two integer numbers
low and high (0 < low ≤ high < 1012).
Output
For each line of input except the first line you should produce one line of output. This line contains
a single integer, which indicates how many almost prime numbers are within the range (inclusive)
low … high.
Sample Input
3
1 10
1 20
1 5
Sample Output
3
4
1
求[l,r]中有多少个数满足它本身不是质数,但只有一个质因子。
先打出1-1e6的质数表。
每一个质数x,他最大贡献就是x^p<=n,就是log(x,n)。
#include<iostream> #include<cstdio> #include<cmath> using namespace std; const int N=1000005; int n,cnt,pri ; bool vis ; long long query(long long pos) { long long ans=0; for(int i=1;i<=cnt&&(long long)pri[i]*pri[i]<=pos;i++) ans+=log(pos)/log(pri[i])-1; return ans; } int main() { vis[1]=1; for(int i=2;i<=1000000;i++) { if(!vis[i]) pri[++cnt]=i; for(int j=1;j<=cnt&&i*pri[j]<=1000000;j++) { vis[i*pri[j]]=1; if(i%pri[j]==0) break; } } scanf("%d",&n); while(n--) { long long l,r; scanf("%lld%lld",&l,&r); printf("%lld\n",query(r)-query(l-1)); } return 0; }
相关文章推荐
- Almost Prime Numbers UVA - 10539——打表+lower_bound()查找
- Uva 10539 Almost Prime Numbers
- Uva 10539 Almost Prime Numbers
- uva10539 Almost Prime Numbers
- UVA - 10539 Almost Prime Numbers
- UVa10539 Almost Prime Numbers
- UVA 10539 Almost Prime Numbers( 素数因子)
- UVA 10539 Almost Prime Numbers( 素数因子)
- UVA 10539 Almost Prime Numbers(唯一分解定理)
- uva 10539 - Almost Prime Numbers(数论)
- UVa 10539 (筛素数、二分查找) Almost Prime Numbers
- UVa 10539 - Almost Prime Numbers(筛选求素数)
- UVA10539 - Almost Prime Numbers
- UVa 10539 Almost Prime Numbers
- UVa10539 - Almost Prime Numbers
- UVA 10539 Almost Prime Numbers
- UVA 10539(p343)----Almost Prime Numbers
- UVA 10539 Almost Prime Numbers
- UVA 10539 - Almost Prime Numbers(数论)
- UVA-10539 Almost Prime Numbers