Uva 10650 - Determinate Prime

/*

    Verdict : Accepted
    Link: Determinate Prime Solution Link

*/


#include<bits/stdc++.h>

#define limit 32009
#define sf scanf
#define pf printf

using namespace std;

typedef long long LL;
typedef vector<int>vi;

int prm[limit], plen=1;

bitset<limit> bs;

void sieve()
{
    LL i,j;
    bs.set();
    bs[1] = bs[0] = 0;

    for(i=2; i<limit; i++)
    {
        if(bs[i])
        {
            if(i != 2)
            {
                prm[plen++] = i;
            }

            for(j=i*i;j<limit;j+=i)
            {
                bs[j] = 0;
            }
        }
    }
}


struct dprime
{
    int dwn, up, dist;
};

dprime ar[limit];

void ek()
{
    int i,pd;

    for(i=1; i<plen; i++)
    {
        pd = prm[i+1] - prm[i];
        ar[i].dwn = prm[i];
        ar[i].up = prm[i+1];
        ar[i].dist = pd;
    }

    //for(i=1; i<=10; i++)cout << ar[i].dwn << " " << ar[i].up << "-" <<ar[i].dist << "\n";
}

struct get_sol
{
    int from, to, sdist;
};

get_sol series[limit];

int len=1;

void dui()
{
    int pd, d, i , c=0 , m=1;

    for(i=1; i<plen; i++)
    {
        pd = ar[i].dist;
        d = ar[i+1].dist;

        if(pd == d)
        {
            c++;

            if(c > 1)
            {
                series[len].from = ar[i-m].dwn;
                m++;
                len--;
            }

            else
            {
                series[len].from = ar[i].dwn;
            }

            series[len].to = ar[i+1].up;
            series[len].sdist = pd;
            len++;
        }

        else
        {
            c=0;
            m=1;
        }
    }

    //for(i=1; i<=10; i++)cout << series[i].from << " " << series[i].to << "-" << series[i].sdist << "\n";
    //cout << "len = " << len;
}

//int Search_Binary(int lo, int hi, int itm)
//{
//    int mid;
//
//    while(lo <= hi)
//    {
//        mid = (lo + hi) / 2;
//
//        if(series[mid].to == itm) return mid;
//        else if(series[mid].to > itm) hi=mid-1;
//        else if(series[mid].to < itm) lo = mid+1;
//    }
//
//    return lo;
//}

int main()
{
    sieve();
    ek();
    dui();
    int x,y;
    while(~sf("%d %d",&x,&y))
    {
        if(!x and !y)break;

        if(x > y)
        {
            swap(x,y);
        }

        int in, i , tmp , first, last, bebodhan;

        bool f=false;

        for(i=1; i<=len-1; i++)
        {
            if(series[i].to > y)
            {
                break;
            }

            else
            {
                first = series[i].from;
                last = series[i].to;
                bebodhan = series[i].sdist;

                if(first >= x and last <= y)
                {
                    //cout << first << " " << last << "\n";

                    f=false;

                    while(first <= last)
                    {
                        if(!f)
                        {
                            pf("%d",first);
                            f=true;
                        }

                        else
                        {
                            pf(" %d",first);
                        }

                        first+=bebodhan;
                    }

                    pf("\n");
                }
            }
        }
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LeetCode Palindrome Number