Codeforces Almost Prime

/*

    Verdict: Accepted
*/

#include<iostream>
#include<cstdio>
#include<cmath>
#include<bitset>

#define sf scanf
#define pf printf
#define limit 3005

using namespace std;

int prm[limit], plen=0;
int ans[limit];

typedef long long LL;
typedef bitset<limit>bs;

bs btst;

void sieve()
{
    LL i,j;

    btst.set();
    btst[0] = btst[1] = 0;

    for(i=2; i<limit; i++)
    {
        if(btst[i])
        {
            for(j=i*2; j<limit; j+=i)
            {
                if(!(j % i))
                {
                    ans[j]+=1; // Count which number has 2 Distinct Prime factors
                }

                btst[j] = 0;
            }
        }
    }

    //for(i=2; i<=30; i++)cout<<i<<"-"<<ans[i]<<"; ";

    for(i=2; i<limit; i++)
    {
        if(ans[i] == 2)
        {
            ans[i] = 1; // overlapped by 1 where got 2.
        }

        else
        {
            ans[i] = 0; // otherwise 0
        }
    }

    // Cumulative Sum

    for(i=2; i<limit; i++)
    {
        ans[i]+=ans[i-1];
    }
    //for(i=2; i<=50; i++)cout<<i<<"-"<<ans[i]<<"; ";
}

int main()
{
    sieve();
    int num;
    while(~sf("%d",&num))
    {
        pf("%d\n",ans[num]);
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number