Monday, April 25, 2016

UVA 10970

#include<iostream>

using namespace std;

int main()
{
 long long m,n,c;
 
 while(cin>>m>>n)
 {
  c= m * n -1;
  cout<<c<<endl;
  
 }
 
 
 return 0;
}

UVA 10523

import java.math.*;
import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);

        int i,a,n;
        BigInteger sum = new BigInteger("0");
        BigInteger A = new BigInteger("0");

        while(in.hasNext())
        {
            n = in.nextInt();
            a = in.nextInt();

            sum = new BigInteger("0");
            A = BigInteger.valueOf(a);

            for(i=1; i<=n; i++)
            {
                sum = sum.add(BigInteger.valueOf(i).multiply(A.pow(i)));
            }
            System.out.println(sum);
        }
    }


}

UVA 495

import java.math.*;
import java.util.*;

public class Main{

 public static void main(String[] args){

  BigInteger ZERO=new BigInteger("0");
  BigInteger ONE=new BigInteger("1");

  BigInteger[] F=new BigInteger[5050];

  F[0]=ZERO;
  F[1]=ONE;

  int i=0,N=0;
  Scanner in=new Scanner(System.in);

  for(i=2;i<5050;i++)
   F[i]=F[i-1].add(F[i-2]);

  while(in.hasNext()){
   N=in.nextInt();
   System.out.printf("The Fibonacci number for %d is %d\n",N,F[N]);
  }
 }
}

UVA 12798

#include<iostream>

using namespace std;

int main()
{
 long long m,n,a,i,j;
 long long count;
 
 while(cin>>m>>n)
 {
  //cin>>n;
  count=0;
  for(i=0; i<m; i++)
  {
   a=0;
   bool mg = false;
  
   for(j=0; j<n; j++)
   {
    cin>>a;
    if(!a) mg=true;
    
   }
   if(!mg) count++;
  }
  cout<<count<<endl;
 }
 return 0;
 
}

Sunday, April 17, 2016

CF: Watermelon

#include<iostream>

using namespace std;

int main()
{
 long long a;
 
 cin>>a;
 if(a<=2 || a%2!=0) cout<<"NO"<<endl;
 else cout<<"YES"<<endl;
 return 0;
}

Tuesday, April 12, 2016

UVA 11636

#include<iostream>

using namespace std;

int main()
{
 long long a,b,i,j,sum;
  j=1;
 while(cin>>a)
 {
 
  if(a<0) break;
   sum=0;
  if(a==1) cout<<"Case "<<j<<": 0"<<endl;
  else{
   for(i=2; i<=a; i=i*2)
  {
   sum+=1;
  }
  b=a - i/2;
  if(b<i && b!=0) sum+=1;
  
  cout<<"Case "<<j<<": "<<sum<<endl;
   
  }
  j++;
 
  
 }
 
 
 return 0;
}

Sunday, April 10, 2016

UVA 146

#include<bits/stdc++.h>

using namespace std;

int main()
{
 char s[100];
 
 while(cin>>s)
 {
  if(s[0] == '#') break;
  long long l=strlen(s);
  
  if(next_permutation(s,s+l))
  {
   cout<<s<<endl;
  }
  else cout<<"No Successor"<<endl;
 }
 
 return 0;
}

Friday, April 8, 2016

CF - A and B and Compilation Errors

#include<iostream>

using namespace std;

int main()
{
 long long fl,sl,tl;
 long long tc,n1,n2,n3,i;
 long long r1,r2;
 
 while(cin>>tc)
 {
  fl=0,sl=0,tl=0;
  for(i=0; i<tc; i++)
  {
   cin>>n1;
   fl=fl+n1;
  }
  
  for(i=0; i<tc-1; i++)
  {
   cin>>n2;
   sl=sl+n2;
  }
  
  for(i=0; i<tc-2; i++)
  {
   cin>>n3;
   tl=tl+n3;
  }
  
  r1=fl-sl;
  r2=sl-tl;
  
  cout<<r1<<endl<<r2<<endl;
 }
 
  
 
 return 0;
}

Thursday, April 7, 2016

UVA 11764

#include<iostream>

using namespace std;

int main()
{

    int a[100];
    int t,n,ele,i,j,hi,low;


    while(cin>>t)
    {
        ele=1;
        while(t--)
        {
            hi=0;
            low=0;

            cin>>n;
            for(i=0; i<n; i++)
            {
                cin>>a[i];
            }

            if(n==1) cout<<"Case "<<ele<<": "<<"0 0"<<endl;

            else if(n==2) {
                if(a[0]<a[1]) cout<<"Case "<<ele<<": "<<"1 0"<<endl;
                else if(a[0]>a[1]) cout<<"Case "<<ele<<": "<<"0 1"<<endl;
            }

            if(n>2)
            {
                for(j=0; j<n-1; j++)
                {
                    if(a[j+1]>a[j]) hi+=1;
                    else if(a[j+1]<a[j]) low+=1;
                }

            cout<<"Case "<<ele<<": "<<hi<<" "<<low<<endl;

            }

            ele++;

        }

    }

}

UVA 1727

#include<iostream>

using namespace std;

int main()
{

int i,n,m,d;
string mmth[100],dday[100];
string mth,day;
while(cin>>n)
{
    for(i=0; i<n; i++)
    {
        cin>>mmth[i]>>dday[i];

    }

    for(i=0; i<n; i++)
    {
        mth=mmth[i];
        day=dday[i];



        if(mth=="FEB") d=28;

        else if(mth=="JAN" || mth=="MAR" || mth=="MAY" ||mth=="JUL" || mth=="AUG" || mth=="OCT" || mth=="DEC") d=31;
        else if(mth=="APR" || mth=="JUN" || mth=="SEP" || mth=="NOV") d=30;

        if(day=="SUN") m=1;
        else if(day=="MON") m=2;
        else if(day=="TUE") m=3;
        else if(day=="WED") m=4;
        else if(day=="THU") m=5;
        else if(day=="FRI") m=6;
        else if(day=="SAT") m=7;

        //logic block :)

        if(d==28 && (m==2 || m==3 || m==4 || m==5 || m==1 || m==7)) cout<<"8"<<endl;
        if(d==28 && m==6) cout<<"8"<<endl;

        if((m==1 || m==2 || m==3) && (d==30 || d==31)) cout<<"8"<<endl;
        if(m==4 && d==30) cout<<"8"<<endl;
        if(m==4 && d==31) cout<<"9"<<endl;

        if(m==5 && d==30) cout<<"9"<<endl;
        if(m==5 && d==31) cout<<"10"<<endl;
        if((m==6) && (d==30 || d==31)) cout<<"10"<<endl;
        if((m==7) && (d==30 || d==31)) cout<<"9"<<endl;

    }
}

return 0;
}

Monday, April 4, 2016

UVA 12626

#include<bits/stdc++.h>

using namespace std;

int main()
{

    string word;
    int tc;
    int m,a,r,g,t,ii,min;
    while(cin>>tc)
    {
        while(tc--)
        {
            m=a=r=g=t=ii=0;
            min=600;
            cin>>word;
            int l = word.length();
            for(int i=0; i<l; i++)
            {
                if(word[i]=='A') a=a+1;
                else if(word[i]=='M') m=m+1;
                else if(word[i]=='R') r = r+1;
                else if(word[i]=='G') g=g+1;
                else if(word[i]=='I') ii=ii+1;
                else if(word[i]=='T') t=t+1;
            }

                a=a/3;
                r=r/2;

                if(a<min) min=a;
                if(m<min) min =m;
                if(r<min) min=r;
                if(g<min) min =g;
                if(ii<min) min=ii;
                if(t<min) min=t;
                cout<<min<<endl;
        }

    }

}

Saturday, April 2, 2016

UVA 10391

#include<bits/stdc++.h>
#include<vector>
#include<algorithm>
#include<cstdio>

using namespace std;

int main()
{
 vector<string>v;
 string shobdo;
 string fs;
 string ss;
 int n,m;
 
 while(cin>>shobdo)
 {
  v.push_back(shobdo);
  }
 
 sort(v.begin(),v.end());
 
 for(int i=0; i<v.size(); i++)
 {
  if(v[i].size()==1) continue;
  
  for(int j=1; j<v[i].size(); j++)
  {
   fs=v[i].substr(0,j);
   ss=v[i].substr(j,v[i].size()-1);
   
   if((binary_search(v.begin(),v.end(),fs)) && (binary_search(v.begin(),v.end(),ss)))
   {
     cout<<v[i]<<endl;
     break;
   }
    
  }
  
 }

 return 0;
}

STL next_permutation(array,array+size)

#include<bits/stdc++.h>

using namespace std;

int main()
{
 char a[10];
 int l;
 cin>>a;
 l=strlen(a);
 sort(a,a+l);
 do{
  cout<<a<<endl;
 }
 while(next_permutation(a,a+l));
 return 0;
}

UVA 10098

#include<bits/stdc++.h>

using namespace std;

int main()
{
 char a[11];
 int n;
 
 while(cin>>n)
 {
  while(n--)
  {
   cin>>a;
   int l = strlen(a);
   sort(a,a+l);
   
   do{
    cout<<a<<endl;
   }
   while(next_permutation(a,a+l));
   cout<<endl;
   
  }
 }
 
 return 0;
}