Uva 11415 - Count the Factorials

#include<bits/stdc++.h>
#define sze 10000010
#define out(s) cout << s << "\n"
#define outs(s) cout << s << " "
using namespace std;

typedef long long LL;

bitset<sze+9>bs;

LL nod[sze+9], p = sze+9;

void sieve()
{
    LL i,j;

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

    nod[1] = 0;

    for(i=2; i<=p; i++)
    {
        if(bs[i])
        {
            nod[i] = 1;

            for(j=2*i; j<=p; j+=i)
            {
                nod[j] = 1 + nod[j / i];
                bs[j] = 0;
            }
        }
    }

    //for(i=2; i<=20 ; i++)cout << i << " " << nod[i] << "\n";
}

void cumulative_sum()
{
    LL i;

    for(i=3; i<=p; i++)
    {
        nod[i] += nod[i-1];
    }

    //for(i=2; i<=10; i++)outs(nod[i]);
}

LL bin_srch(LL target)
{
    LL lo=1,hi=p,mid;

    while(lo <= hi)
    {
        mid = lo + (hi - lo) / 2;

        if(nod[mid] == target)
        {
            return mid;
        }

        else if(nod[mid] <= target)
        {
            lo = mid+1;
        }

        else
        {
            hi = mid-1;
        }
    }

    return hi;
}

int main()
{
    sieve();
    cumulative_sum();
    int t;
    cin >> t;
    while(t--)
    {
        LL n;
        cin >> n;
        LL res=bin_srch(n);
        cout << res+1 << "\n";
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number