Title Link: http://lightoj.com/volume_showproblem.php?problem=1197
Give you A and b the number of A to B.
First in the cell between the prime sieve, the large interval is similar to the idea of a prime sieve, A to B is not a prime mark out. Because B-a is up to 1e5 in size, the time complexity of each group of data is up to O (1e5 log1e5).
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intMAXN = 2e5 +5;6typedefLong LongLL;7 BOOLPRIME[MAXN], VIS[MAXN];8LL P[MAXN/5];9 Ten voidinit () { Oneprime[1] =true; A intCont =0; - for(inti =2; i < MAXN; i++) { - if(!Prime[i]) { theP[++cont] =i; - for(intj = i *2; J < Maxn; J + =i) -PRIME[J] =true; - } + } - } + A intMain () at { - init (); - intT, a, B; -scanf"%d", &t); - for(intCA =1; CA <= T; ca++) { -scanf"%d%d", &a, &b); in intres =0; - if(b <MAXN) { to for(inti = A; I <= b; i++) { + if(!Prime[i]) -res++; the } * } $ Else {Panax Notoginsengmemset (Vis,false,sizeof(Vis)); - for(inti =1; P[i]*p[i] <= b; i++) { theLL k = A/P[i]; + if(K*p[i] <a) Ak++; the if(k = =1)//at this point a%p[i]==0 && a/p[i]==1, stating that A is just a prime number +k++; - while(k * P[i] <= b) {//filter for a~b that are not primes $Vis[k*p[i]-a] =true; $k++; - } - } the for(inti = A; I <= b; i++) { - if(!vis[i-A])Wuyires++; the } - } Wuprintf"Case %d:%d\n", CA, res); - } About}
Light OJ 1197-help Hanzo (Prime sieve technique)