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

Saturday, September 30, 2017

1282 - Leading and Trailing

/***

            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 = 1000005;

U bigmod (U b, U p, U m)
{
    U p1,p2;

    if (p == 0)
        return 1;

    if (p & 1)
    {
         p1 = b % m;
         p2 = bigmod (b,p-1,m) % m;
         return (p1 * p2) % m;
    }
    else
    {
        p1 = bigmod (b,p/2,m) % m;
        return (p1 * p1) % m;
    }
}

int main (void)
{
    /*
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
    */
    U tc,pos;
    llu y,z;
    double a,b,x;

    sf ("%u",&tc);

    for (pos=1; pos<=tc; pos++)
    {
        sf ("%lf %lf",&a,&b);

        x = b*log10(a) - floor(b*log10(a));
        y = pow (10,x) * 100;
        z = bigmod (a,b,1000);

        pf ("Case %u: %llu %03llu\n",pos,y,z);
    }

    return 0;
}

Thursday, September 28, 2017

1067 - Combinations

/***

            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 llu MOD = 1000003;
llu fact[1000001];

llu bigmod (llu b,llu p,llu m)
{
    llu A,B;

    if (p == 0)
        return 1;

    if (p & 1)
    {
        A = b % m;
        B = bigmod (b,p-1,m) % m;
        return (A*B) % m;
    }
    else
    {
        A = bigmod (b,p/2,m) % m;
        return (A*A) % m;
    }
}

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

    llu tc,pos,n,k,i,u,d,res;

    fact[0] = 1;

    for (i=1; i<1000001; i++)
        fact[i] = ((fact[i-1] % MOD) * (i % MOD)) % MOD;

    sf ("%llu",&tc);

    for (pos=1; pos<=tc; pos++)
    {
        sf ("%llu %llu",&n,&k);

        u = fact[n];
        d = ((fact[k] % MOD)*(fact[n-k] % MOD)) % MOD;
        res = bigmod (d,MOD-2,MOD);
        res = ((u % MOD) * (res % MOD)) % MOD;

        pf ("Case %llu: %llu\n",pos,res);
    }

    return 0;
}

Monday, September 11, 2017

1305 - Area of a Parallelogram

/***

            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

int main (void)
{
    int t,pos;
    double ax,ay,bx,by,cx,cy,dx,dy,x,y,ab,ac,ad,bc,cd,s,a1,a2;

    sf ("%d",&t);

    for (pos=1; pos<=t; pos++)
    {
        sf ("%lf %lf %lf %lf %lf %lf",&ax,&ay,&bx,&by,&cx,&cy);

        x = (ax + cx) / 2;
        y = (ay + cy) / 2;

        dx = 2*x - bx;
        dy = 2*y - by;

        ab = sqrt ((ax-bx)*(ax-bx) + (ay-by)*(ay-by));
        bc = sqrt ((bx-cx)*(bx-cx) + (by-cy)*(by-cy));
        ac = sqrt ((ax-cx)*(ax-cx) + (ay-cy)*(ay-cy));
        ad = sqrt ((ax-dx)*(ax-dx) + (ay-dy)*(ay-dy));
        cd = sqrt ((cx-dx)*(cx-dx) + (cy-dy)*(cy-dy));

        s = (ab+bc+ac) / 2;
        a1 = sqrt (s * (s-ab) * (s-bc) * (s-ac));

        s = (ad+ac+cd) / 2;
        a2 = sqrt (s * (s-ad) * (s-ac) * (s-cd));

        printf ("Case %d: %0.lf %0.lf %0.lf\n",pos,dx,dy,a1+a2);
    }

    return 0;
}

1354 - IP Checking

/***

            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

int conversion (int n)
{
    int b = 0, i = 1, rem;

    while (n != 0)
    {
        rem = n % 2;
        n /= 2;
        b += rem * i;
        i *= 10;
    }

    return b;
}

int main (void)
{
    int t,pos,d1,d2,d3,d4,b1,b2,b3,b4;
    char c;

    cin >> t;

    for (pos=1; pos<=t; pos++)
    {
        cin >> d1 >> c >> d2 >> c >> d3 >> c >> d4;
        cin >> b1 >> c >> b2 >> c >> b3 >> c >> b4;

        if (conversion(d1) == b1 && conversion(d2) == b2 && conversion(d3) == b3 && conversion(d4) == b4)
            pf ("Case %d: Yes\n",pos);
        else
            pf ("Case %d: No\n",pos);
    }

    return 0;
}

1227 - Boiled Eggs

/***
            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
int main (void)
{
    int t,pos,n,p,q,arr[35],i;
    cin >> t;
    for (pos=1; pos<=t; pos++)
    {
        cin >> n >> p >> q;
        int sum = 0;
        for (i=0; i<n; i++)
            cin >> arr[i];
        for (i=0; i<n; i++)
        {
            sum += arr[i];
            if (i >= p || sum > q)
            {
                pf ("Case %d: %d\n",pos,i);
                break;
            }
            else if (i == n-1 || sum == q)
            {
                pf ("Case %d: %d\n",pos,i+1);
                break;
            }
        }
    }
    return 0;
}

1249 - Chocolate Thief

/***

            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

int main (void)
{
    int t,pos,n,i,l,w,h,p,q;
    char str[105][25];
    bool check;

    cin >> t;

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

        vector <int> v;

        for (i=0; i<n; i++)
        {
            cin >> str[i] >> l >> w >> h;

            v.push_back(l*w*h);
        }

        int size = v.size(), check = true;

        for (i=0; i<size-1; i++)
        {
            if (v[i] != v[i+1])
            {
                check = false;
                break;
            }
        }

        pf ("Case %d: ",pos);

        if (check == true)
            pf ("no thief\n");
        else
        {
            int max = v[0], p = 0;

            for (i=1; i<size; i++)
            {
                if (v[i] > max)
                {
                    max = v[i];
                    p = i;
                }
            }

            int min = v[0], q = 0;

            for (i=1; i<size; i++)
            {
                if (v[i] < min)
                {
                    min = v[i];
                    q = i;
                }
            }

            pf ("%s took chocolate from %s\n",str[p],str[q]);
        }
    }

    return 0;
}

1225 - Palindromic Numbers (II)

 /***

            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

int main (void)
{
    int t,pos,len,i,half;
    char str[10];
    bool check;

    sf ("%d",&t);

    for (pos=1; pos<=t; pos++)
    {
        sf ("%s",str);

        len = strlen (str);
        half = len / 2;
        len--;
        check = true;

        for (i=0; i<half; i++)
        {
            if (str[i] != str[len-i])
            {
                check = false;
                break;
            }
        }

        pf ("Case %d: ",pos);

        if (check == true)
            pf ("Yes\n");
        else
            pf ("No\n");
    }

    return 0;
}

1107 - How Cow

/***

            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

int main (void)
{
    int t,pos,x1,y1,x2,y2,i,x,y;

    cin >> t;

    for (pos=1; pos<=t; pos++)
    {
        cin >> x1 >> y1 >> x2 >> y2 >> i;

        pf ("Case %d:\n",pos);

        while (i--)
        {
            cin >> x >> y;

            if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
                cout << "Yes" << endl;
            else
                cout << "No" << endl;
        }
    }

    return 0;
}

1212 - Double Ended Queue

/***

            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;

int main ()
{
    int t,pos,n,m,val;
    string str;
    deque <int> q;

    cin >> t;

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

        cout << "Case " << pos << ":\n";

        while (m--)
        {
            cin >> str;

            if (str == "pushLeft" || str == "pushRight")
            {
                cin >> val;

                if (q.size() == n)
                    cout << "The queue is full" << endl;
                else
                {
                    if (str == "pushLeft")
                    {
                        q.push_front(val);
                        cout << "Pushed in left: " << val << endl;
                    }
                    else
                    {
                        q.push_back(val);
                        cout << "Pushed in right: " << val << endl;
                    }
                }
            }
            else
            {
                if (q.empty())
                    cout << "The queue is empty" << endl;
                else
                {
                    if (str == "popLeft")
                    {
                        cout << "Popped from left: " << q.front() << endl;
                        q.pop_front();
                    }
                    else
                    {
                        cout << "Popped from right: " << q.back() << endl;
                        q.pop_back();
                    }
                }
            }
        }

        while (!q.empty())
                q.pop_back();
    }

    return 0;
}

1116 - Ekka Dokka

/***

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

***/

#include <iostream>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <iterator>
#include <sstream>
#include <iomanip>
using namespace std;

#define sf scanf
#define pf printf

int main ()
{
    int t,pos;
    long long n,i,div;

    cin >> t;

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

        pf ("Case %d: ",pos);

        if (n & 1)
            cout << "Impossible" << endl;
        else
        {
            for (i=2; i<=n; i+=2)
            {
                div = n / i;

                if (div * i == n && div & 1)
                {
                    cout << div << " " << i << endl;
                    break;
                }
            }
        }
    }

    return 0;
}

1214 - Large Division

import java.util.Scanner;
import java.math.BigInteger;

public class Main
{
    public static void main (String Args[])
    {
        int t,pos;
        BigInteger a,b,c,zero;

        Scanner in = new Scanner (System.in);

        t = in.nextInt();

        for (pos=1; pos<=t; pos++)
        {
            a = in.nextBigInteger();
            b = in.nextBigInteger();

            zero = BigInteger.valueOf(0);
            c = a.remainder(b);

            if (c.equals(zero))
                System.out.println ("Case " +pos+ ": divisible");
            else
                System.out.println ("Case " +pos+ ": not divisible");
        }
    }
}

1113 - Discover the Web

/***

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

***/

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <iterator>
#include <sstream>
#include <iomanip>
using namespace std;

#define sf scanf
#define pf printf

string com,url;
int t,pos;

int main ()
{
    cin >> t;

    for (pos=1; pos<=t; pos++)
    {
        stack <string> backw,forw;
        backw.push("http://www.lightoj.com/");
     
        pf ("Case %d:\n",pos);

        while (cin >> com)
        {
            if (com == "QUIT")
                break;
            else if (com == "VISIT")
            {
                cin >> url;
                backw.push(url);
                cout << backw.top() <<endl;

                if (!forw.empty())
                {
                    while (!forw.empty())
                       forw.pop();
                }
            }
            else if (com == "BACK")
            {
                if (backw.size() <= 1)
                    cout << "Ignored" << endl;
                else
                {
                    forw.push(backw.top());
                    backw.pop();
                    cout << backw.top() << endl;
                }
            }
            else
            {
                if (forw.empty())
                    cout << "Ignored" << endl;
                else
                {
                    backw.push(forw.top());
                    forw.pop();
                    cout << backw.top() << endl;
                }
            }
        }
    }

    return 0;
}

1182 - Parity

/***

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

***/

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <iterator>
#include <sstream>
#include <iomanip>
using namespace std;

#define sf scanf
#define pf printf

int main ()
{
    int t,pos,n,rem,count;

    cin >> t;

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

        count = 0;

        while (n != 0)
        {
            if (n % 2 == 1)
                count = count + 1;
            n /= 2;
        }

        pf ("Case %d: ",pos);

        if (count & 1)
            pf ("odd\n");
        else
            pf ("even\n");
    }

    return 0;
}

1387 - Setu

#include <iostream>
#include <cstdio>
using namespace std;

int main ()
{
    int t,pos,i,n,sum;
    string str;

    cin >> t;

    for (pos=1; pos<=t; pos++)
    {
        sum = 0;

        cin >> i;
     
        cout << "Case " << pos << ":" << endl;

        while (i--)
        {
            getchar ();

            cin >> str;

            if (str == "donate")
            {
                cin >> n;
                sum = sum + n;
            }
            else
                cout << sum << endl;
        }
    }

    return 0;
}

1069 - Lift

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

int main ()
{
    int t,i,a,b;

    cin >> t;

    for (i=1; i<=t; i++)
    {
        cin >> a >> b;

        if (a < b)
            cout << "Case " << i << ": " << 19+4*b << endl;
        else if (b <= a)
            cout << "Case " << i << ": " << 19+4*a+4*(a-b) << endl;
    }

    return 0;
}

1053 - Higher Math

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

int main ()
{
    int t,i,a,b,c;

    cin >> t;

    for (i=1; i<=t; i++)
    {
        cin >> a >> b >> c;

        if ((a*a == b*b + c*c) || (b*b == c*c + a*a) || (c*c == a*a + b*b))
            cout << "Case " << i << ": yes" << endl;
        else
            cout << "Case " << i << ": no" << endl;
    }

    return 0;
}

1022 - Circle in Square

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

int main ()
{
    int t,i;
    double r;

    double pi = 2 * acos(0.0);

    cin >> t;

    for (i=1; i<=t; i++)
    {
        cin >> r;

        cout << "Case " << i << ": " << fixed << setprecision(2) << 4*r*r-pi*r*r << endl;
    }

    return 0;
}

1015 - Brush (I)

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

int main ()
{
    int t,i,n,x;

    cin >> t;

    for (i=1; i<=t; i++)
    {
        cin >> n;

        int sum=0;

        while (n--)
        {
            cin >> x;

            if (x < 0)
                x = 0;

            sum = sum+x;
        }

        cout << "Case " << i << ": " << sum << endl;
    }

    return 0;
}

1001 - Opposite Task

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

int main ()
{
    int t,x,a,b;

    cin >> t;

    while (t--)
    {
        cin >> x;

        if (x <= 10)
        {
            a = x;
            b = x-a;
        }

        else
        {
            a = x-10;
            b = x-a;
        }

        cout << a << " " << b << endl;
    }

    return 0;
}