Light oj 1197 - Help Hanzo (素数筛技巧)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1197

给你a和b求a到b之间的素数个数。

先在小区间素数筛,大区间就用类似素数筛的想法,把a到b之间不是素数的标记出来。因为b-a最多1e5的大小,所以每组数据的时间复杂度最多就o(1e5 log1e5)。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int MAXN = 2e5 + 5;
 6 typedef long long LL;
 7 bool prime[MAXN] , vis[MAXN];
 8 LL p[MAXN / 5];
 9
10 void init() {
11     prime[1] = true;
12     int cont = 0;
13     for(int i = 2 ; i < MAXN ; i++) {
14         if(!prime[i]) {
15             p[++cont] = i;
16             for(int j = i * 2 ; j < MAXN ; j += i)
17                 prime[j] = true;
18         }
19     }
20 }
21
22 int main()
23 {
24     init();
25     int t , a , b;
26     scanf("%d" , &t);
27     for(int ca = 1 ; ca <= t ; ca++) {
28         scanf("%d %d" , &a , &b);
29         int res = 0;
30         if(b < MAXN) {
31             for(int i = a ; i <= b ; i++) {
32                 if(!prime[i])
33                     res++;
34             }
35         }
36         else {
37             memset(vis , false , sizeof(vis));
38             for(int i = 1 ; p[i]*p[i] <= b ; i++) {
39                 LL k = a / p[i];
40                 if(k*p[i] < a)
41                     k++;
42                 if(k == 1) //此时a%p[i]==0 && a/p[i]==1,说明a刚好是一个素数
43                     k++;
44                 while(k * p[i] <= b) { //筛选a~b中不是素数的
45                     vis[k*p[i] - a] = true;
46                     k++;
47                 }
48             }
49             for(int i = a ; i <= b ; i++) {
50                 if(!vis[i - a])
51                     res++;
52             }
53         }
54         printf("Case %d: %d\n" , ca , res);
55     }
56 }
时间: 2024-08-25 19:20:28

Light oj 1197 - Help Hanzo (素数筛技巧)的相关文章

Help Hanzo (素数筛+区间枚举)

Help Hanzo 题意:求a~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000).     (全题在文末) 题解: a~b枚举必定TLE,普通打表MLE,真是头疼.. b - a ≤ 100000 是关键. 类似素数筛的方法: 1.初始化vis[]=0 ; 2.素数的倍数vis[]=1; 3.  b较小时,素数筛解决   b很大时,素数筛的vis[]会MLE,此时用vis2[i-a]保存vis[i]就不会MLE 了.. #include<iostream>

Light OJ 1197 1197 - Help Hanzo(大区间素数筛选)

Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angry. But he i

light_oj 1197 区间素数筛

light_oj 1197 区间素数筛 M - Help Hanzo Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1197 Description Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason

Light OJ 1356 Prime Independence 最大独立集+素数筛选

题目来源:Light OJ 1356 Prime Independence 题意:给你n个数 选出最多的数构成一个集合使得任何2个数不是另外一个数的质数倍 x!=k*y 思路:矛盾的2个数连边 并且所有数分成质因子数为奇数和偶数两部分 以质因子奇偶不同构建二分图 同奇或者同偶的数一定不是另外一个数的质数倍 判断矛盾 首先对每个数因子分解 例如x 有a1个p1质因子 a2个p2质因子...an个pn质因子 x的质因子个数为a1+a2+...+an 记为sum 判断是否存在x/p1  x/p2 ..

Light OJ 1054 Efficient Pseudo Code 求n^m的约数和

题目来源:Light OJ 1054 Efficient Pseudo Code 题意:求n的m次这个数的所有的约数和 思路:首先对于一个数n = p1^a1*p2^a2*p3^a3*-*pk^ak  约束和s = (p1^0+p1^1+p1^2+-p1^a1)(p2^0+p2^1+p2^2+-p2^a2)-(pk^0+pk^1+pk^2+-pk^ak) 然后就是先求素数表 分解因子 然后求p1^0+p1^1+p1^2+-p1^a1 这是一个以1开始的等比数列 就是(1-q^n)/(1-q) 最

Light OJ 1278 Sum of Consecutive Integers N拆分成连续整数和

题目来源:Light OJ 1278 Sum of Consecutive Integers 题意:N拆分成连续整数和的方案数 思路:奇因数的个数 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; //筛素数 const int maxn = 10000010; bool vis[maxn]; int prime[10

Light OJ 1341 Aladdin and the Flying Carpet

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi

Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是完全平方数 求有多少种方案 思路:每个数分解因子 每隔数可以选也可以不选 0 1表示 然后设有m种素数因子 选出的数组成的各个因子的数量必须是偶数 组成一个m行和n列的矩阵 每一行代表每一种因子的系数 解出自由元的数量 #include <cstdio> #include <cstring> #include <algorithm&g

light oj 1236 【大数分解】

给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,--em, 则结果为((1+2*e1)*(1+2*e2)--(1+2*em)+1)/2. //light oj 1236 大数分解素因子 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #i