Wednesday 30 September 2015

UVA 12148 - Electricity (UVALive - 4214, SPOJ - SAMER08E, Regionals 2008 >> Latin America - South America)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

bool isLeapYear(int y)
{
    return ((y%4==0 && y%100!=0) || y%400==0);
}

bool isPrevious(int date, int month, int year, int pdate, int pmonth, int pyear)
{
    if(date>1)
    {
        if(pdate+1==date && month==pmonth && year==pyear) return true;
    }
    else if(year==pyear+1 && month==1 && pmonth==12 && pdate==31) return true;
    else if(year==pyear && month==pmonth+1)
    {
        if(month==2 && pdate==31) return true;
        if(month==3 && pdate==28+isLeapYear(year)) return true;
        if(month==4 && pdate==31) return true;
        if(month==5 && pdate==30) return true;
        if(month==6 && pdate==31) return true;
        if(month==7 && pdate==30) return true;
        if(month==8 && pdate==31) return true;
        if(month==9 && pdate==31) return true;
        if(month==10 && pdate==30) return true;
        if(month==11 && pdate==31) return true;
        if(month==12 && pdate==30) return true;
    }
return false;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n,kwh, date, pdate, month, pmonth, year, pyear, pkwh;
    string s;
    char ch;
    while(cin>>n && n)
    {
        int cnt=0, tot=0;
        for(int i=0;i<n;i++)
        {
            cin>>date>>month>>year>>kwh;
            if(i)
            {
                if(isPrevious(date, month, year, pdate, pmonth, pyear))
                {
                    cnt++, tot+=(kwh-pkwh);
                }

            }
            pdate=date, pmonth=month, pyear=year, pkwh=kwh;
        }
        cout<< cnt << " "<<tot<<"\n";
    }
return 0;
}

UVA 1185 Big Number (UVALive 2697, HDU 1018, POJ 1423, ZOJ 1526, SCU 3094, Regionals 2002 >> Asia - Dhaka)

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

double digit[10000000+10];

void precal()
{
    digit[1]=log10(1);
    for(int i=2;i<=10000000;i++)
        digit[i]=digit[i-1]+log10(i);
return;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n, tc;
    precal();
    cin>>tc;
    while(tc--)
    {
        cin>>n;
        cout<<(int)digit[n]+1<<"\n";
    }
return 0;
}

UVA 10099 - The Tourist Guide

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int cost[102][102];

void FloydWarshallFlow(int n)
{
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                cost[i][j]=max(cost[i][j], min(cost[i][k], cost[k][j])); //modified
return;
}


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, m, u, v, w;
    string s;
    char ch;
    while(cin>>n>>m && n)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                cost[i][j]=0;
        while(m--)
        {
            cin>>u>>v>>w;
            cost[u][v]=w;
            cost[v][u]=w;
        }
        FloydWarshallFlow(n);
        cin>>u>>v>>w;
        cout<<"Scenario #"<<kk++<<"\n";
        //cout<<cost[u][v]<<endl;
        cout<<"Minimum Number of Trips = ";
        cout<< ceil((double)w/(double)(cost[u][v]-1))<<"\n\n";
    }
return 0;
}

UVA 640 Self Numbers (UVALive 5326, POJ 1316, HDU 1128, ZOJ 1180, SCU 2843, Regionals 1998 >> North America - Mid-Central USA)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 1000010

bool status[MX];

int func(int n)
{
    int nn=n;
    while(n)
    {
        nn+=n%10;
        n/=10;
    }
return nn;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    string s;
    char ch;
    for(int i=1;i<=1000000;i++)
        if(!status[i])
        {
            cout<<i<<"\n";
            for(int j=i;j<=1000000;)
            {
                j = func(j);
                if(status[j]) break;
                status[j]=true;
            }
        }
return 0;
}

Monday 28 September 2015

Kids Love Candies (UVALive 6321, SPOJ MIDOZ, Regionals 2012 >> Africa/Middle East - Arab Contest)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, k, m, x, y, a, b, c;
    string s;
    cin>>tc;
    while(tc--)
    {
        cin>>n>>k;
        int tot=0;
        for(int i=0;i<n;i++)
        {
            cin>>x;
            tot+=x/k;
        }
        cout<< tot <<"\n";
    }
return 0;
}