31 January 2018

UVA 10014 - Simple calculations

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647


int main()
{
    int test,n,cnt;
    float a, an,ans,m,sum;
    sc("%d",&test);

    for(int i=1 ; i<=test ; i++)
    {
        sc("%d",&n);
        sc("%f %f",&a,&an);
        cnt = 2*n;
        sum = 0;
        for(int j=1 ; j<=n ; j++)
        {
            sc("%f",&m);
            sum = sum + (m*cnt);
            cnt = cnt -2;
        }
        ans = ((n*a) + an - sum)/(n+1);
        pf("%.2f\n",ans);
        if(i!=test)
            pf("\n");
    }

    return 0;
}

30 January 2018

UVA 10170 - The Hotel with Infinite Rooms Python Solution

from sys import stdin
scan = lambda : stdin.readline()

while True:
    n,k = map(int, scan().split())
    k = k
    sum = 0
    while True:
        sum += n
        if(sum >= k):
            print(n)
            break
        n += 1
   
   

UVA 10170 - The Hotel with Infinite Rooms

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647


int main()
{
    long long s, d;
    while(scanf("%lld %lld", &s, &d) == 2)
    {
        long long i = s, sum = 0;
        while(1)
        {
            sum += i;
            if(sum >= d)
            {
                printf("%lld\n", i);
                break;
            }
            i++;
        }
    }
    return 0;
}

29 January 2018

UVA 10940 - Throwing cards away II

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647


int main()
{
    int ans[500000 + 5];

        ans[1] = 1;
        ans[2] = 2;

        int next = 2;

        for(int i = 3; i <= 500000; i++ )
        {
            if ( i < next ) next = 2;
            ans [i] = next;
            next += 2;
        }

        int n;

        while( sc ("%d", &n) && n )
        {
            pf("%d\n", ans [n]);
        }

    return 0;
}

UVA 10499 - The Land of Justice

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647


int main()
{

   long long int n;
   char ch = '%';
   while(sc("%lld",&n)==1 && n>=0)
   {
       if(n==1)
        pf("0%%\n");
       else if(n>1)
        pf("%lld%%\n",(n*25));
   }
    return 0;
}

UVA 11503 - Virtual Friends

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647


int rx[1000001], p[1000001];
map<string,int> R;

int findF(int x)
{
   return p[x] == x ? x : (p[x]=findF(p[x]));
}

int makeFriend(int x, int y)
{
    int u = findF(x);
    int v = findF(y);
    if(u!=v)
    {
        if(rx[u] > rx[v])
        {
            rx[u] += rx[v];
            p[v]=u;
            return rx[u];
        }
        rx[v] += rx[u];
        p[u] = v;
        return rx[v];
    }

    return rx[u];
}


int main()
{

  int test,n,tx,ty;
  char x[30], y[30];

  sc("%d",&test);

  while(test--)
  {
      sc("%d",&n);
      int cnt =0;
      for(int i=0 ; i<=min(2*n,100000) ; i++)
      {
          p[i] =i;
          rx[i]=1;
      }

      while(n--)
      {
         sc("%s %s",&x,&y);
         tx = R[x];
         ty = R[y];
         if(tx==0)
         {
             R[x] = ++cnt;
             tx = cnt;
             //pf("#%d\n",cnt);
         }
         if(ty==0)
         {
             R[y] = ++cnt;
             ty = cnt;
              //pf("#%d\n",cnt);
         }

         pf("%d\n",makeFriend(tx,ty));

      }

      R.clear();
  }

    return 0;
}

13 January 2018

UVA 10608 - Friends

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647

vector<int>data[30010];
int visited[30010],cnt;


void dfs(int u)
{
   visited[u]=1;
   cnt++;
   for(int i=0 ; i<data[u].size() ; i++)
   {
       int v = data[u][i];
       if(!visited[v])
       {
           dfs(v);
       }
   }
   return;
}


int main()
{

   int test,n,m,u,v;
   sc("%d",&test);

   for(int tc=1 ; tc<=test ; tc++)
   {
       sc("%d %d",&n,&m);

       memset(visited,0,sizeof(visited));
       for(int i=1 ; i<=m ; i++)
       {
           sc("%d %d",&u,&v);
           data[u].pb(v);
           data[v].pb(u);
       }
       int mx =0 ;

       for(int i=1 ; i<=n ; i++)
       {
           if(!visited[i])
           {
               cnt=0;
               dfs(i);
               mx = max(mx,cnt);
           }
       }
       pf("%d\n",mx);
       for(int i=1 ; i<=n ; i++)
        data[i].clear();
   }
    return 0;
}

793 - Network Connections

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647

int parent[100001], rank_n[100001];

void makePair(int x)
{
    parent[x] =x;
    rank_n[x] =0;
}

int find_n(int x)
{
    if(parent[x]!=x)
        parent[x] = find_n(parent[x]);
    return parent[x];
}

void makeUnion(int x, int y)
{
    int px = find_n(x);
    int py = find_n(y);

    if(px==py)
        return;
    if(rank_n[px] > rank_n[py])
        parent[py] = px;
    else
    {
        parent[px] = py;
        if(rank_n[px] == rank_n[py])
            rank_n[px]++;
    }
}


int main()
{
    int test,n,u,v,cnt1,cnt2;
    char ch, str[10001];
    sc("%d",&test);

   for(int tc=1 ; tc<=test ; tc++)
    {
        sc("%d",&n);
        memset(parent,0,sizeof(parent));

        for(int i=1 ; i<=n ; i++)
            makePair(i);
        getchar();
        cnt1 = cnt2 =0;
        while(1)
        {
           gets(str);
           if(strcmp(str,"")==0 || feof(stdin))
            break;
           sscanf(str,"%c %d %d",&ch,&u,&v);
           if(ch == 'c')
            makeUnion(u,v);
           else if(ch =='q')
             if(find_n(u) == find_n(v))
                cnt1++;
            else
                cnt2++;
        }
        if(tc!=1)
            pf("\n");
        pf("%d,%d\n",cnt1,cnt2);
    }

    return 0;
}

12 January 2018

Making tooltip in GUI by python

import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):
   
     def __init__(self):
         super(Example,self).__init__()
         self.initUI()
 
     def initUI(self):
         QtGui.QToolTip.setFont(QtGui.QFont('SansSerif',10))
         self.setToolTip('This is a <b>QWidget</b> widget')
         
         btn = QtGui.QPushButton('Button',self)
         btn.setToolTip('This is a <b>QPushButton</b> widget')
         btn.resize(btn.sizeHint())
         btn.move(10,10)
         
         self.setGeometry(10,10,300,300)
         self.setWindowTitle('ToolTips')
         self.show()


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
   main()

11 January 2018

Set Window Icon in Python Gui Programing

import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):
   
    def __init__(self):
       super(Example,self).__init__()
       self.initUi()

    def initUi(self):
       self.setGeometry(10,10,300,300)
       self.setWindowTitle('Windo ICON')
       self.setWindowIcon(QtGui.QIcon('icon.png'))
       self.show()



def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Python Basic Gui

#To make a simple gui follow the instructions. You need to at first install PyQt4 in your system. If you use linux run the code sudo apt-get install python3-pyqt4. For another system you just need to setup an application file. Google it for this. After installing pyqt4 modulo just write down the foling code. it will show you a simple gui.

import sys
from PyQt4 import QtGui


def main():
   
    app = QtGui.QApplication(sys.argv)

    w = QtGui.QWidget()
    #w.resize(250, 150)
    #w.move(300, 300)
    w.setGeometry(10,10,400,400)
    w.setWindowTitle('Simple GUI in Python')
    w.show()
   
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

UVA 755 - 487--3279

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647

map<string,int> my_map ;
map<string,int>::iterator it;


char getChar(char ch)
{
    if(ch=='A' || ch=='B' || ch=='C')
        return '2';
    else if(ch=='D' || ch=='E' || ch=='F')
        return '3';
    else if(ch=='G' || ch=='H' || ch=='I')
        return '4';
    else if(ch=='J' || ch=='K' || ch=='L')
        return '5';
    else if(ch=='M' || ch=='N' || ch=='O')
        return '6';
    else if(ch=='P' || ch=='R' || ch=='S')
        return '7';
    else if(ch=='T' || ch=='U' || ch=='V')
        return '8';
    else if(ch=='W' || ch=='X' || ch=='Y')
        return '9';
    else if(ch>='0' && ch<='9')
        return ch;
    return 'Q';
}


string convert_str(string s)
{
    string result,ans;

    for(int i=0 ; i<s.size() ; i++)
    {
        if(s[i]!='-')
        {
            result += getChar(s[i]);
        }
    }

    for(int i=0 ; i<result.size() ; i++)
    {
        if(i==3)
            ans += '-';
        ans += result[i];
    }
    return ans;
}


int main()
{
    int test,n;
    string str,s;
    sc("%d",&test);
    for(int cs=1 ; cs<=test ; cs++)
    {
        sc("%d",&n);

        while(n--)
        {
            cin>>str;
            s = convert_str(str);
            my_map[s]++;
        }
        if(cs>1)
            pf("\n");
        bool get = false ;
        for(it = my_map.begin() ; it!=my_map.end() ; it++)
        {
            if(it->second >1)
            {
                get = true;
                cout<<it->first<<" "<<it->second<<endl;
            }
        }
        if(!get)
            pf("No duplicates.\n");
        my_map.clear();
    }

    return 0;
}

10 January 2018

UVA 10789 - Prime Frequency

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647

int isPrime(int n)
{
    int root;
    if(n ==0 || n==1)
        return 0;
    else if(n ==2 )
        return 1;
    else if(n%2==0)
        return 0;
    else
    {
        root = sqrt(n);
        for(int i=3 ; i<=root ; i+=2)
        {
            if(n%i ==0)
                return 0;
        }
        return 1;
    }
}

int main()
{
    char str[3001];
    int data[150];
    vector<int>value;
    int test,n,cs=0;

    sc("%d",&test);
    getchar();
    while(test--)
    {
        gets(str);
        memset(data,0,sizeof(data));
        for(int i=0 ; i<strlen(str) ; i++)
        {
            n = str[i];
            if(data[n]==0)
                value.pb(n);
            data[n]++;
            //pf("%d",data[n]);
        }

        sort(value.begin(),value.end());
        bool t = false ;
        pf("Case %d: ",++cs);
        for(int i=0 ; i<value.size() ; i++)
        {
            if(isPrime(data[value[i]]))
        {
            pf("%c",value[i]);
                t = true;
            }
        }

        if(t == false)
            pf("empty");
        value.clear();
        pf("\n");

    }

    return 0;
}

UVA 10921 - Find the Telephone

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647


int main()
{
   char str[10001];

   while(gets(str))
   {
       for(int i=0 ; i<strlen(str) ; i++)
       {
           if(str[i]=='1' || str[i]=='0' || str[i]=='-')
           {
               pf("%c",str[i]);
               continue;
           }
           else if(str[i]>='A' && str[i]<='C')
            pf("2");
           else if(str[i]>='D' && str[i]<='F')
            pf("3");
           else if(str[i]>='G' && str[i]<='I')
            pf("4");
           else if(str[i]>='J' && str[i]<='L')
            pf("5");
           else if(str[i]>='M' && str[i] <='O')
            pf("6");
           else if(str[i]>='P' && str[i]<='S')
            pf("7");
           else if(str[i]>='T' && str[i]<='V')
            pf("8");
           else if(str[i] >='W' && str[i]<='Z')
            pf("9");
       }
       pf("\n");
   }

    return 0;
}

09 January 2018

UVA 280 - Vertex

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647

int n,a,b,m,d,v;
vector<int>graph[1001] ;
int visited[10001];
queue<int>q ;


void check_graph(int p)
{
    memset(visited,0,sizeof(visited));
    q.push(p);
    while(!q.empty())
    {
        int u = q.front();
        q.pop() ;
        for(int i=0 ; i<graph[u].size() ; i++)
        {
            v = graph[u][i];
            if(!visited[v])
            {
                visited[v] =1;
                q.push(v);
            }
        }
    }

    int cnt =0;
    for(int i=0 ; i<n ; i++)
        if(!visited[i]) cnt++;

    pf("%d",cnt);
    for(int i=0 ; i<n ; i++)
        if(!visited[i])
        pf(" %d",i+1);
    pf("\n");
}


int main()
{

    while(1)
    {
        sc("%d",&n);
        if(n==0)
            break;
        while(1)
        {
            sc("%d",&a);
            if(a==0)
                break;
            while(1)
            {
                sc("%d",&b);
                if(b==0)
                    break;
                graph[a-1].pb(b-1);
            }
        }

        sc("%d",&m);
        for(int i=0 ; i<m ; i++)
        {
            sc("%d",&d);
            check_graph(d-1);
        }

        for(int i=0 ; i<n ; i++)
            graph[i].clear();
    }
    return 0;
}

06 January 2018

UVA 900 - Brick Wall Patterns

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647

ll fibo(ll n)
{

    if(n==1)
        return 1;
    if(n==2)
        return 2;
    return fibo(n-1)+fibo(n-2);
}

int main()
{
    ll n,m;
    ll data[100001];
    data[1] =1;
    data[2] =2;
    while(sc("%lld",&n))
    {
        if(n==0)
            break;
        if(n==1 || n==2){
            pf("%lld\n",n);
            continue;
        }

        for(int i=3 ; i<=n ; i++)
        {
            data[i] = data[i-1] + data[i-2];
        }

        pf("%lld\n",data[n]);
    }

    return 0;
}

04 January 2018

UVA 371 - Ackermann Functions

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647


int main()
{

    ll n,m,mx,mx_num,i,x,cnt;
    while(sc("%lld %lld",&n,&m))
    {
       if(n==0 && m==0)
       break;
       mx =0;
       if(n>m)
      swap(n,m);
       for(i=n ; i<=m ; i++)
       {
         x = i;
         cnt=1;
         if(x%2==0)
         x = x/2;
         else if(x%2!=0)
         x = (3*x)+1;
       
         while(x!=1)
         {
           if(x%2==0)
           {
             x = x/2;
             cnt++;
           }
           else if(x%2!=0)
           {
             x = (3*x)+1;
             cnt++;
           }
         }
        if(mx < cnt)
        {
           mx = cnt;
           mx_num = i;
        }
       }
  cout<<"Between "<<n<<" and "<<m<<", "<<mx_num<<" generates the longest sequence of "<<mx<<" values."<<endl;
    }
    return 0;
}

03 January 2018

UVA 11150 - Cola

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647


int main()
{

    int n,r,p,q,m,b,total;
    while(sc("%d",&n)==1)
    {
      m = n+1;
      total = n;
      while(true)
      {
        b = m/3;
        if(b==0)
        break;
        q = m%3;
        total += b;
        m = b+q;
      }
     pf("%d\n",total);
    }
    return 0;
}

UVA 10346 - Peter's Smokes

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647


int main()
{

   int n,k,total,r,q;
   while(sc("%d %d",&n,&k)==2)
   {
      total = n ;
      while(true)
      {
        r = n/k;
        if(r==0)
         break;
        q = n%k;
        total += r;
        n = r+q;
      }
     pf("%d\n",total);
   }
    return 0;
}

UVA 11689 - Soda Surpler

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647


int main()
{

    int test,n,m,p,r,total,b,q;
    sc("%d",&test);
    while(test--)
    {
      sc("%d %d %d",&n,&m,&p);
      r = n+m;
      total =0;
      while(true)
      {
        b = r/p;
        if(b==0)
        break;
        q = r%p;
        total += b;
        r = b+q;
      }
      pf("%d\n",total);
    }
    return 0;
}

UVA 1225 - Digit Counting

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647


int data[20];

void count_digits(int n)
{
   int c;
   while(n!=0)
   {
     c = n%10;
     data[c]++;
     n = n/10;
   } 

}



int main()
{
    int n,test;
    sc("%d",&test);
    while(test--)
    {
      sc("%d",&n);
      memset(data,0,sizeof(data));
      for(int i=1 ; i<=n ; i++)
      {
         count_digits(i);
      }

      for(int i=0 ; i<=9 ; i++)
      {
        pf("%d",data[i]);
        if(i<9)
         pf(" ");
      }
      pf("\n");
    }

    return 0;
}

UVA 834 - Continued Fractions

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647


int main()
{
    ll a,b,c,n,x,y;
    while(sc("%lld %lld",&a,&b)==2)
    {
      n = a/b;
      pf("[%lld;",n);
      c = a%b;
      while(c!=0)
      {
        x = b;
        y=c;
        n = x/y;
        pf("%lld",n);
        c = x%y;
        if(c>0)
        {
          pf(",");
        }
        b = y;

      }
      pf("]\n");
    }

    return 0;
}

02 January 2018

10293 - Word Length and Frequency

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size)  for(int i=0 ; i<size ; i++) cin>>array[i]  ;
#define new_line  pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647

int data[101];
char str[1001];
 int cnt=0;

bool isNotAlpha(char ch)
{
    return (ch==' ' || ch=='?' || ch=='.'|| ch=='!'|| ch==',');
}

void count_word()
{
    int ln = strlen(str);

    for(int i=0 ; i<ln ; i++)
    {
        if(isalpha(str[i]))
            cnt++;
        else if(isNotAlpha(str[i]))
        {
            data[cnt]++;
            cnt=0;
        }
    }
    if(str[ln-1]!='-')
    {
        data[cnt]++;
        cnt=0;
    }

}

int main()
{
    memset(data,0,sizeof(data));
    while(gets(str))
    {
       if(str[0]!='#')
       {
         count_word();
         continue;
       }
       for(int i=1 ; i<100 ; i++)
       {
           if(data[i])
            pf("%d %d\n",i,data[i]);
       }
       pf("\n");
      memset(data,0,sizeof(data));
    }
    return 0;
}

UVA 10679 - I Love Strings!!