- #include <bits/stdc++.h>
- using namespace std;
- #define maxx 100005
- bitset<maxx>vis;
- vector<int>prime;
- void sieve()
- {
- int x=maxx/2, y=sqrt(maxx)/2;
- for(int i=1;i<=y;i++)
- {
- if(vis[i])
- {
- for(int j=(i*(i+1))*2;j<=x;j+=(2*i)+1)
- vis[j]=1;
- }
- }
- prime.pb(2);
- for(int i=3;i<maxx;i+=2)
- if(vis[i/2]==0)
- prime.pb(i);
- }
- int segmented_sieve(int a, int b){
- vis=0;
- if(b<2) return 0;
- if(a<2) a=2;
- int xx=sqrt((double)b)+1;
- for(ll i=0;i<SZ(prime) && prime[i]<=xx;i++){
- ll j=(a/prime[i])*prime[i];
- if(j <a) j+=prime[i];
- if(j<(ll)(prime[i]+prime[i])) j=prime[i]+prime[i];
- for(;j<=b;j+=prime[i])
- vis[j-a]=1;
- }
- int cnt=0;
- for(ll i=a;i<=b;i++)
- if(vis[i-a]==0) cnt++;
- return cnt;
- }
- int main()
- {
- ///freopen("in.txt","r",stdin);
- ///freopen("out.txt","w",stdout);
- sieve();
- int t;
- sf(t);
- TEST_CASE(t)
- {
- ll a,b;
- sffl(a,b);
- PRINT_CASE;
- printf("%d\n",segmented_sieve(a,b));
- }
- return 0;
- }
Saturday 29 July 2017
Lightoj 1197 - Help Hanzo
Subscribe to:
Post Comments (Atom)
Most Featured Post
Lightoj 1159 - Batman
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
-
Problem link: Problem Analysis: It is actually a basic Bisection problem , as we can see here we can not actually find a formula fo...
-
http://lightoj.com/volume_showproblem.php?problem=1382 Problem analysis: This is a rare problem i wrote about so far. After much strugg...
No comments:
Post a Comment