Thursday 5 January 2017

Project Euler #47: Distinct primes factors

Program:-

#include <bits/stdc++.h>
using namespace std;
 
void SieveOfEratosthenes(int n, int k,int m)
{
    vector<int>prime(n+1,0);
    for (int p=2; p<=n; p++)
    {
        if (prime[p] == 0)
        {
            for (int i=p*2; i<=n; i += p)
                prime[i]++;
        }
    }
    
    vector<int>vec;
    for (int p=2; p<=n; p++){
       if (prime[p]==k){ 
         if(vec.size()==0) {vec.push_back(p);}
           else if(vec[vec.size()-1]==p-1) vec.push_back(p);
               else {vec.resize(0);vec.push_back(p);}
       }
        if(vec.size()==k && vec[0]<=m){
            cout<<vec[0]<<endl;
            vec.erase(vec.begin(),vec.begin()+1);
        }
    }
}
 
int main()
{
    int n,k;
    cin>>n>>k;
    SieveOfEratosthenes(n+k+5,k,n);
    return 0;
}

No comments:

Post a Comment