237C - Primes on Interval - CodeForces Solution


binary search number theory two pointers *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define faster() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const int N=1e6;
//int dp[N];
using namespace std;
int main()
{
    ll a,b,k,i,j,cn=0,ans=0;
    cin>>a>>b>>k;
    vector<ll>v;
    v.pb(0);
    for(i=a;i<=b;i++)
    {
        bool c=0;
        if(i==1)
        {
            v.pb(0);
            continue;
        }
        for(j=2;j<=sqrt(i);j++)
        {
            if(i%j==0)
            {
                c=1;
                break;
            }
        }
        if(!c)
           cn++;
        v.pb(cn);
    }
    ll n=v.size();
    /*for(i=0;i<v.size();i++)
    {
        cout<<v[i]<<" ";
    }*/
    if(cn<k)
    {
        cout<<-1<<endl;
        return 0;
    }
    for(i=0;i<n-1;i++)
    {
        ll x=v[i]+k;
        auto it=lower_bound(v.begin(),v.end(),x)-v.begin();
        ans=max(ans,(it-i));

    }
    cout<<ans<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion
946A - Partition
1068B - LCM
1692E - Binary Deque
679A - Bear and Prime 100
488A - Giga Tower
14A - Letter
1150A - Stock Arbitraging
1552A - Subsequence Permutation
1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You
988B - Substrings Sort
312A - Whose sentence is it
513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra