Light OJ 1197-help Hanzo (Prime sieve technique)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.