Wednesday, November 18, 2015

447A. DZY Loves Hash (Codeforces)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
int a[305];

int main()
{
    int p, x, n, rem, i;
    int flag=1, cnf = -1;
    cin >> p >> n;
    for(i=0; i<n; i++)
    {
        cin >> x ;
        rem = x%p;
        if(a[rem]!=0 && flag==1)
        {
           cnf = i+1;
           flag =0;
        }
        a[rem]++;
    }
    cout << cnf << endl;
    return 0;
}

Friday, October 9, 2015

Find the Factorial of a Number (Iterative way)

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

int fact(int n)
{
    int i, ans=1;
    for(i=1; i<=n; i++)
        ans*=i;
    return ans;
}
int main()
{
    int n;
    cin>> n;
    cout << fact(n) << endl;
}

Find Factorial of a Number (Recursively)

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

int fact(int n)
{
    if(n==1) return 1;
    return n*fact(n-1);
}

int main()
{
    int n;
    cin>> n;
    cout << fact(n) << endl;
}

1214 - Large Division (LightOJ)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;

int main()
{
    //freopen("in.txt" , "r", stdin);
    string a;
    long long b, rem;
    int test, t, i;
    cin>> test;
    for(t=1; t<=test; t++)
    {
        cin >> a >> b;
        if(b<0)
            b = b*(-1);
        int len = a.length();
        rem=0;
        for(i=0; i<len; i++)
        {
            if(a[i]=='-')
                i=1;
            rem*=10;
            rem+=(a[i]-'0');
            rem%=b;
        }
        if(rem==0)
            printf("Case %d: divisible\n", t);
        else
            printf("Case %d: not divisible\n", t);
    }
}

Saturday, September 12, 2015

1082 - Array Queries (Light OJ)

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define MX 10005

const int N = 100000;
int data[N+10], save[4*N+10];
int inf = 1 << 28;
int n;

void build_tree(int node, int st, int en, int a[], int A[])
{
    if(st==en)
    {
        A[node] = a[st];
        return;
    }
    int mid = st+ ((en-st)>>1);
    int left = node<<1;
    int right = (node<<1)+1;
    build_tree(left, st, mid, a, A);
    build_tree(right, mid+1, en, a, A);
    A[node] = min(A[left], A[right]);
}
int query_tree(int node, int st, int en, int a, int b, int A[])
{
    if(a>b)
        return inf;

    if(st==a && en==b)
        return A[node];
    int mid = st+ ((en-st)>>1);
    int left = node<<1;
    int right = (node<<1)+1;

    int l = query_tree(left, st, mid, a, min(b, mid), A);
    int r = query_tree(right ,mid+1, en, max(a, mid+1), b, A);
    return min(l, r);
}

void Build(int tot)
{
    build_tree(1, 1, tot, data, save);
}
int query(int a, int b)
{
    return query_tree(1, 1, n, a, b, save);
}

int main()
{
    //freopen("in.txt", "r", stdin);
    int test, t;
    int q, a, b;
    cin >> test;
    for(t=1; t<=test; t++)
    {
        cin >> n >> q;
        for(int i=1; i<=n; i++)
             scanf("%d", &data[i]);
        Build(n);
        cout << "Case " << t  <<  ":" << endl;
        for(int i=1; i<=q; i++)
        {
           scanf("%d %d", &a, &b);
           printf("%d\n",  query(a, b));
        }
    }
    return 0;
}


Wednesday, September 9, 2015

490B - Queue

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<fstream>
#include<map>
using namespace std;
#define sc(n) scanf("%d", &n)
#define sf scanf
#define pf printf
#define MX 100005
#define pb push_back
#define ff first
#define ss second

typedef long long ll;
int freq[1000005];
int arr[200005];

vector < pair<int, int> > VP;
map<int , int> mp;

int main()
{
    //freopen("in4.txt","r",stdin);
    int i, j, a, b, n;
    int check, check2, inEven=2, inOdd=1;
    cin >> n;
   
    for(i=0; i<n; i++)
    {
        cin >> a >> b;
        VP.pb( make_pair(a, b));
        mp[a] = b;
        freq[VP[i].ff]++;
        freq[VP[i].ss]++;
        if(VP[i].ff==0)
        {
            check = VP[i].ss;
            arr[inEven] = check;
            inEven+=2;
        }
    }
   
    for(int i=0; i<n; i++)
    {
      if(freq[VP[i].ff]==1)
      {
          check2 =  VP[i].ff;
          arr[inOdd] = check2;
          inOdd+=2;
      }
    }
   
    for(int i=0; i<=(n/2); i++)
    {
        arr[inEven] = mp[check] ;
        check = arr[inEven];
            inEven+=2;
        arr[inOdd] = mp[check2];
        check2 = arr[inOdd] ;
            inOdd+=2;
    }
   
    for(int i=1; i<=n; i++)
        cout << arr[i] << " " ;

    return 0;
}


Monday, September 7, 2015

488B - Candy Boxes (Codeforces)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define sc(n) scanf("%d", &n)
#define sf scanf
#define pf printf
#define MX 100005
typedef long long ll;
float arr[10];

bool check(float a, float b, float c, float d)
{
    arr[0] = a, arr[1] = b, arr[2]= c, arr[3] = d;
    sort(arr, arr+4);
    a = arr[0], b = arr[1], c = arr[2], d = arr[3];

    float am, median, range;
    am = (a+b+c+d)/4;
    median = (b+c)/2;
    range = d-a;

    if(am==median && median==range)
        return true;
    else
        return false;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int n, i, j, k, lost, flag=0;
    float x1, x2, x3, x4;
    cin >> n;
    lost = 4-n;
    if(lost==0)
    {
        cin >> x1 >> x2 >> x3 >> x4;
        if(check(x1, x2, x3, x4))
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    else if(lost==1)
    {
        cin >> x1 >> x2 >> x3;
        for(i=1; i<=1500; i++)
        {
            if(check(x1, x2, x3, i))
               x4 = i, flag=1;
        }
        if(flag==1)
            cout << "YES" << endl <<  x4 << endl;
        else
            cout << "NO" << endl;
    }
    else if(lost==2)
    {
        cin >> x1 >> x2;
        for(i=1; i<=1500; i++)
        {
            for(j=1; j<=1500; j++)
            {
                if(check(x1, x2, i, j))
                    x3 =i , x4 = j, flag=1; continue;
            }
        }
        if(flag==1)
            cout << "YES" << endl << x3 << endl << x4 << endl;
        else
            cout << "NO" << endl;
    }
    else if(lost==3)
    {
        cin >> x1;
        x2 = x1, x3 =3*x1 , x4 = x3;
        if(check(x1, x2, x3, x4))
            cout << "YES" << endl << x2 << endl << x3 << endl << x4 << endl;
        else
            cout << "NO" << endl;
    }
    else if(lost==4)
    {
        cout << "YES" << endl << "1" << endl << "1" << endl;
        cout << "3" << endl << "3" << endl;
    }
    return 0;
}



Compare equality of two string in C

#include <stdio.h> #include<string.h> int main() {     char* country = "Bangladesh";     char* country2;     ...