Showing posts with label SPOJ Solutions. Show all posts
Showing posts with label SPOJ Solutions. Show all posts

Tuesday, September 19, 2017

BDOI16B - Beautiful Factorial Game

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

#include <bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pii pair <int,int>
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int
#define endl "\n"

const int MOD = 1000000007;
const int MAX = 10005;
bool prime[MAX];
vector <int> v;

void sieve ()
{
    int i,j;

    v.pb(2);
    prime[0] = prime[1] = true;

    for (i=4; i<MAX; i+=2)
        prime[i] = true;

    for (i=3; i*i<=MAX; i+=2)
        if (!prime[i])
            for (j=i*i; j<MAX; j+=2*i)
                prime[j] = true;

    for (i=3; i<MAX; i+=2)
        if (!prime[i])
            v.pb(i);
}

int fun (int n, int p, int f)
{
    int sum = 0;

    while (n != 0)
    {
        n /= p;
        sum += n;
    }

    sum /= f;

    return sum;
}

int main (void)
{
    /*
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
    fast;
    */
    sieve ();

    int tc,pos,m,n,t,l,i,j,val,mini;

    cin >> tc;

    for (pos=1; pos<=tc; pos++)
    {
        cin >> n >> m;

        cout << "Case " << pos << ": ";

        vector <int> P,F;
        t = m;

        int len = v.size();

        for (i=0; i<len; i++)
        {
            if (t % v[i] == 0)
            {
                P.pb(v[i]);
                j = 0;

                while (t % v[i] == 0)
                {
                    t /= v[i];
                    j++;
                }

                F.pb(j);
            }
        }

        if (t > 1)
        {
            P.pb(t);
            F.pb(1);
        }

        l = P.size(), mini = 1e9, val = 0;

        for (i=0; i<l; i++)
        {
            val = fun (n,P[i],F[i]);

            mini = min (val,mini);
        }

        cout << mini << endl;
    }

    return 0;
}

Sunday, September 10, 2017

TDKPRIME - Finding the Kth Prime

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

#include <bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pb push_back
#define in insert
#define lld long long

const lld MAX = 86500001;
bool check[MAX];
vector <lld> v;

void sieve ()
{
    v.pb (2);

    lld i,j;

    check[0] = check[1] = true;

    for (i=4; i<=MAX; i+=2)
        check[i] = true;

    for (i=3; i*i<=MAX; i+=2)
    {
        if (check[i] == false)
        {
            v.pb(i);

            for (j=i*i; j<MAX; j+=2*i)
                check[j] = true;
        }
    }

    for (i*i==MAX; i<MAX; i+=2)
        if (check[i] == false)
            v.pb(i);
}

int main (void)
{
    sieve ();

    int tc,n;

    sf ("%d",&tc);

    while (tc--)
    {
        sf ("%d",&n);
        pf ("%lld\n",v[n-1]);
    }

    return 0;
}

NDIV - n-divisors

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

#include <bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int

const int MAX = 31700;
bool prime[MAX];
vector <int> v;

void sieve ()
{
    int i,j;

    prime[0] = prime[1] = true;
    v.pb(2);

    for (i=4; i<MAX; i+=2)
        prime[i] = true;

    for (i=3; i*i<=MAX; i+=2)
        if (!prime[i])
            for (j=i*i; j<MAX; j+=2*i)
                prime[j] = true;

    for (i=3; i<MAX; i++)
        if (!prime[i])
            v.pb(i);
}

int countfact (int n)
{
    int i,sum=1,k;

    for (i=0; v[i]*v[i]<=n; i++)
    {
        if (n % v[i] == 0)
        {
            k = 1;

            while (n % v[i] == 0)
            {
                n /= v[i];
                k++;
            }

            sum *= k;
        }
    }

    if (n > 1)
        sum *= 2;

    return sum;
}

int main (void)
{
    /*freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);*/
    sieve ();

    int a,b,n,i,k=0;

    sf ("%d %d %d",&a,&b,&n);

    if (a > b)
    {
        a = a^b;
        b = a^b;
        a = a^b;
    }

    for (i=a; i<=b; i++)
        if (countfact(i) == n)
            k++;

    pf ("%d\n",k);

    return 0;
}

INTEST - Enormous Input Test

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

#include <bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int

int main (void)
{
    /*freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);*/
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);

    unsigned n,k,val,c=0;

    sf ("%u %u",&n,&k);

    while (n--)
    {
        sf ("%u",&val);

        if (val % k == 0)
            c++;
    }

    pf ("%u\n",c);

    return 0;
}

COMDIV - Number of common divisors

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

#include <bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int

int gcd (int a, int b)
{
    if (b == 0)
        return a;
    else
        return gcd (b, a%b);
}

int main (void)
{
    /*freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);*/
    int tc,a,b,g,i,k;

    sf ("%d",&tc);

    while (tc--)
    {
        sf ("%d %d",&a,&b);

        g = gcd (a,b);
        k = 0;

        for (i=1; i*i<=g; i++)
        {
            if (g % i == 0)
            {
                if (i*i == g)
                    k++;
                else
                    k += 2;
            }
        }

        pf ("%d\n",k);
    }

    return 0;
}

PRINT - Prime Intervals

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

#include <bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int

const long long MAX = 1000000;
vector <long long> v,sg,P;
bool prime[MAX];

void simple_sieve ()
{
    long long i,j;

    prime[0] = prime[1] = true;
    v.pb(2);

    for (i=4; i<MAX; i+=2)
        prime[i] = true;

    for (i=3; i*i<=MAX; i+=2)
    {
        if (!prime[i])
        {
            v.pb(i);

            for (j=i*i; j<=MAX; j+=2*i)
                prime[j] = true;
        }
    }

    for (i=1001; i<=MAX; i+=2)
        if (!prime[i])
            v.pb(i);
}

void segmented_sieve (long long l,long long u)
{
    long long i,j,start,x;

    for (i=l; i<=u; i++)
        sg.pb(i);

    if (l == 0)
        sg[1] = 0;
    else if (l == 1)
        sg[0] = 0;

    for (i=0; v[i]*v[i]<=u; i++)
    {
        x = v[i];
        start = x*x;

        if (start < l)
            start = ((l+x-1)/x)*x;

        for (j=start; j<=u; j+=x)
            sg[j-l] = 0;
    }
}

int main (void)
{
    /*freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);*/
    simple_sieve ();

    int tc,l,u,i;

    sf ("%lld",&tc);

    while (tc--)
    {
        sf ("%lld %lld",&l,&u);

        segmented_sieve (l,u);

        for (i=l; i<=u; i++)
            if (sg[i-l] != 0)
                pf ("%lld\n",sg[i-l]);

        sg.clear ();
    }

    return 0;
}