UVa 10539 (筛素数、二分查找) Almost Prime Numbers

题意:

求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1

分析:

首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序。

对于L和U,二分查找出小于U和L的最大数的下标,作差即可得到答案。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <algorithm>
 4
 5 typedef long long LL;
 6
 7 const int maxn = 1000000;
 8 const int maxp = 80000;
 9
10 bool vis[maxn + 10];
11 int prime[maxp], cntp = 0;
12 LL a[maxn], cnt = 1;
13
14 void Init()
15 {
16     int m = 1000;
17     for(int i = 2; i <= m; ++i) if(!vis[i])
18         for(int j = i * i; j <= maxn; j += i) vis[j] = true;
19     for(int i = 2; i <= maxn; ++i) if(!vis[i]) prime[cntp++] = i;
20
21     for(int i = 0; i < cntp; ++i)
22     {
23         LL temp = (LL)prime[i] * prime[i];
24         while(temp <= 1000000000000LL)
25         {
26             a[cnt++] = temp;
27             temp *= prime[i];
28         }
29     }
30     std::sort(a, a + cnt);
31 }
32
33 int binary_search(LL n)
34 {
35     LL L = 0, R = cnt;
36     while(L < R)
37     {
38         LL mid = ((L + R + 1) >> 1);
39         if(a[mid] <= n) L = mid;
40         else R = mid - 1;
41     }
42     return L;
43 }
44
45 int main()
46 {
47     Init();
48     int T;
49     scanf("%d", &T);
50     while(T--)
51     {
52         LL hehe, haha;
53         scanf("%lld%lld", &hehe, &haha);
54         int t = binary_search(hehe), k = binary_search(haha);
55         int ans = k - t;
56         if(a[t] == hehe) ans++;
57         printf("%d\n", ans);
58     }
59
60     return 0;
61 }

代码君

时间: 2024-10-21 21:47:08

UVa 10539 (筛素数、二分查找) Almost Prime Numbers的相关文章

UVa 1644 (筛素数 + 二分) Prime Gap

题意: 给出一个整数n,如果n是素数输出0,否则输出它后一个素数与前一个素数的差值. 分析: 首先用筛法把前十万个素数都筛出来,然后放到数组里.用二分找到不大于n的最大的素数的下标,如果这个素数等于n,则直接输出0,否则输出它后一个素数与它本身的差值. 1 #include <cstdio> 2 #include <cmath> 3 4 const int maxn = 1299709; 5 const int maxp = 100000; 6 bool vis[maxn + 10

UVA Solve It(二分查找)

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x+ q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and ter

UVA Closest Sums(二分查找)

Problem D Closest Sums Input: standard input Output: standard output Time Limit: 3 seconds Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two distinct numbers from the set, which is clo

poj 3518 Prime Gap 二分查找下界和素数筛法

/* 题意:输入有多组数据,每组数据一个n,如果n是素数,输出0否则输出离n最近的两个素数的积,第100000个素数是1299709,所有的素数都在这个范围内 思路:素数筛法加二分查找下界 */ #include<stdio.h> int a[1299720],pri[100005]; int Serch(int v)//二分查找下界 { int mid,x=0,y=100001; while(x<y) { mid=(y+x)/2; if(pri[mid]>=v) y=mid; e

UVA 10539 Almost Prime Numbers( 素数因子)

Problem AAlmost Prime NumbersTime Limit: 1 second 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 with

uva 10539 - Almost Prime Numbers(数论)

题目链接:uva 10539 - Almost Prime Numbers 题目大意:给出范围low~high,问说在这个范围内有多少个数满足n=pb,(p为素数). 解题思路:首先处理出1e6以内的素数,然后对于每个范围,用solve(high)?solve(low?1),solve(n)用来处理小于n的满足要求的数的个数.枚举素数,判断即可. #include <cstdio> #include <cstring> typedef long long ll; const int

POJ-2689 Prime Distance (两重筛素数,区间平移)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

题目传送门 1 /* 2 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 5e2 + 10; 10 const int MAXM = 1e6 + 10; 11 const int INF = 0

uva:10487 - Closest Sums(二分查找)

题目:10487 - Closest Sums 题目大意:给出一组数据,再给出m个查询的数字.要求找到这组数据里的两个数据相加的和最靠近这个查询的数据,输出那两个数据的和. 解题思路:二分查找,这样找到的话,就输出查询的数值,但是要注意找不到的情况:这里最靠近的值不一定是在找不到的时刻的前一次数据,所以要维护最靠近的要查询数的数值. 代码: #include <stdio.h> #include <algorithm> #include <stdlib.h> using