... HAVE U TRIED .... AT LEAST 3 TIMES ... OK U CAN SEE THIS ...
Showing posts with label Ad Hoc. Show all posts
Showing posts with label Ad Hoc. Show all posts

Thursday, May 15, 2014

UVA- 623 : 500!

//Problem  link >>http://uva.onlinejudge.org/external/6/623.html

//  very  familiar  string  problem  ... i've  seen  many  solution  in other  blogs  those  were  huge  code ... i've  learnt  this  from  somewhere ... this  string  and  integer  multiplication technic  is  very  effective ... that  makes  this  code  too  short  to  see ... beware  of  the  fact  that  the  carry  will  be  a  huge string .. so manage  it  rightly ...  i've  got  WA's  for  this ...

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

string  call (string a , int n)
{
    reverse(a.begin() , a.end()) ;
    string b ;
    int  i  , carry =0 ;
    for (i=0 ; i<a.size() ; i++)
    {
        int x=(a[i]-'0')*n+carry ;
        b+=(x%10)+'0' ;
        carry=x/10 ;

    }
    while (carry>0)
    {
        b+=(carry%10)+'0' ;
        carry/=10 ;
    }
    reverse(b.begin(),b.end()) ;

    return b ;
}

int main ()
 {
    string  a[1090] ;

       a[0]="1" ;
       a[1]="1" ;
        for (int  i =2 ; i<=1050 ; i++)
        {
            a[i]= call (a[i-1],i) ;
        }

        int sb ;
        while (cin>>sb)
        {
            cout<<sb<<"!\n"<<a[sb]<<endl ;
        }

     return 0 ;
 }

UVA- 100 : The 3n + 1 problem

//Problem  link >>http://uva.onlinejudge.org/external/1/100.html

/// nothing  to  say ...  our  most  honorable  UVA  problem  (as  stays  right  in  the  first  of  giant  uva-problem  list )...  i solved  that  very  lately ... ignoring  the first !!

#include <iostream>
#include <cstdio>
using namespace std ;
int main ()
{
        int a , b , i ;
        while (cin>>a>>b)
        {
            cout<<a<<" "<<b ;
            if (a>b) swap(a,b) ;

           long long int res=0 ;
            for (i=a ; i<=b ;i++)
            {
               long long  int x = i , cnt =1  ;
                while (x>1)
                {
                    if (x%2)  x=3*x+1 ;
                    else x=x/2 ;
                    cnt++ ;
                }

                res=max(res,cnt) ;
            }
           cout<<" "<<res<<endl ;
       }

    return 0 ;
}

UVA- 694 : The Collatz Sequence

//Problem  link >>http://uva.onlinejudge.org/external/6/694.html

... Simple  Adhoc problem ....

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

int main ()
{
    long long int a , b ,cas=0 ,x ,y  ;
    while (cin>>a>>b )
    {
        if (a==-1 && b==-1) break ;
        int  cnt =0 ;
         x=a  ; y= b ;
        while (x<=y && x!=1)
        {
            if (x%2) x=x*3+1 ;
            else  x=x/2 ;cnt++ ;
             if (x==1)cnt++ ;
         
        }
        cout<<"Case "<<++cas<<": "<<"A = "<<a<<", limit = "<<b<<", number of terms = " ;
        cout<<cnt<<endl ;
    }

    return 0 ;
}

UVA- 10684 : The jackpot

// Problem  link >>http://uva.onlinejudge.org/external/106/10684.html

... Adhoc  problem .... Tricky ...

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

 vector<int>v ;
 int n ;

int main ()
{
    int  i , x , a ,mx ;

    while (cin>>n && n!=0)
    {
        mx=0 ;a=0 ;
        for (i=1 ; i<=n ; i++)
        {
            cin>>x ;
            a=a+x ;
            if (a<0) a=0 ;
            mx=max(a,mx) ;

        }

     if (mx==0) cout<<"Losing streak."<<endl ;
     else cout<<"The maximum winning streak is "<<mx<<".\n" ;

    }

    return 0 ;
}

Monday, February 3, 2014

UVA - 11244 :: Counting Stars

//Problem  link >>http://uva.onlinejudge.org/external/112/11244.html

#include <bits/stdc++.h>
using namespace std ;
int main ()
{
    int c ,r , i , j ;
    while (cin>>r>>c && r||c)
    {
        char s[1000][105] ;

        int cnt=0 ;

        for (i=0 ; i<r ; i++)
        {
            for (j=0 ; j<c ; j++)
            {
                cin>>s[i][j] ;

            }
        }
            int op=0 ;
            for (i=0 ; i<r ; i++)
            {
                for (j=0 ; j<c ; j++)
                {
                    if (s[i][j]=='*')
                    {
                        if (s[i-1][j-1]=='*' && i-1>=0 && j-1>=0) op++ ;
                        if (s[i-1][j]=='*' && i-1>=0 ) op++ ;
                        if (s[i-1][j+1]=='*' && i-1>=0 && j+1<c ) op++ ;
                        if (s[i][j-1]=='*'  && j-1>=0 ) op++ ;
                        if (s[i][j+1]=='*'  && j+1<c ) op++ ;
                        if (s[i+1][j-1]=='*' && i+1<r && j-1>=0 ) op++ ;
                        if (s[i+1][j]=='*' && i+1<r ) op++ ;
                        if (s[i+1][j+1]=='*' && i+1<r && j+1<c) op++ ;
                           if (op==0) cnt++ ;
                     else op=0 ;

                    }

                }
            }
            cout<<cnt<<endl ;

    }

    return 0 ;
}

UVA - 424 :: Integer Inquiry

//Problem link >>http://uva.onlinejudge.org/external/4/424.html

#include <bits/stdc++.h>
#include <string.h>
using namespace std ;
int main ()
{
    string a="" , b="" ;
    int i , j ,cn =0 ;
    while (getline(cin,a))
    {
        if (a=="0") break ;

        if (cn==0)
        {
            for (i=0 ; i<a.size() ; i++) b+=a[i] ;
        }

        if (cn==1)
        {
             int l1=a.size() ;
        int l2=b.size() ;

         if (l1>l2) b.insert(0,l1-l2,'0') ;
         else a.insert(0,l2-l1,'0') ;

            reverse(a.begin() , a.end()) ;
            reverse(b.begin() , b.end()) ;
            l1=a.size() ;

              int sum=0 , carry=0 ,p ;
              string res="" ;
            for (i=0 ,p=0; i<l1 ; i++)
            {
                sum=(a[i]-'0') + (b[i]-'0') + carry ;
                if (sum>9)
                {
                    carry=1 ;
                   sum=sum%10 ;
                }
                else carry=0 ;
               res+=(sum+'0') ;
            }
            if (carry) res+='1' ;

           b="" ;
            for (i=res.size()-1 ; i>=0 ; i--) b+=res[i] ;

        }
         cn=1 ;

    }
     cout<<b<<endl ;

    return 0 ;
}

Friday, January 24, 2014

UVA-12704 : Little Masters

//Problem link>>http://uva.onlinejudge.org/external/127/12704.html

#include <bits/stdc++.h>
using namespace std ;
int main ()
{
    int t , i , a ,b ;
    cin>>t ;
    for (i=1 ; i<=t ; i++)
    {
        double x ,r ;
        cin>>a>>b>>r ;
        x=sqrt(a*a+b*b) ;
     
        printf ("%.2lf %.2lf\n",r-x , r+x) ;
    }

    return 0 ;
}

UVA-10633 - Rare Easy Problem

//Problem link>>http://uva.onlinejudge.org/external/106/10633.html

#include <bits/stdc++.h>
using namespace std ;
int main ()
{
   unsigned  long long d ,  n  ;
    while (cin>>d && d)
    {
        n=(d*10)/9 ;
        if (d%9==0) cout<<n-1<<" "<<n<<endl ;
        else cout<<n<<endl ;

    }

    return 0 ;
}

Sunday, January 12, 2014

UVA-11728 :: Alternate Task

//Problem link>>http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2828

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

int go(int n)
{
     int x=1 , i ;
        for (i=n ; i>1 ; i--)
        {
            if (n%i==0)
            {
                x=x+i ;
            }
        }

        return x ;
}

int main ()
{
    int i , s ,n ,cas=1 ;
    while (cin>>s && s !=0)
    {
       for (i=s ; i>=0 ; i--)
       {
           if (go(i)==s) {cout<<"Case "<<cas++<<": "<<i<<endl ; break ;}
           else if (i==0) cout<<"Case "<<cas++<<": -1"<<endl ;
       }

    }

    return 0 ;
}

UVA-374 :: Big Mod

//Problem  link>>http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=310


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

long long int b , p ,m , i ,x ;
long long bigmod (long long b , long long p)
{
    if (p==1) return b ;
    if (p==0) return 1 ;

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

}

int main ()
{
    while (cin>>b>>p>>m)
    {
        cout<<bigmod(b,p)<<endl<<endl ;
    }

    return 0 ;
}

UVA-12114 :: Bachelor Arithmetic

//Problem link>>http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3266

//probability  greater  than  1  is impossible ... so  increase  event  is  impossible ...

#include <bits/stdc++.h>
using namespace std ;
int main ()
{
    long long int b ,s ,cas=1 ;

    while (cin>>b>>s && b||s)
    {
            b=b-1 ;
            s=s-1 ;
        if (b==0 ) cout<<"Case "<<cas<<": "<<":-\\"<<endl ;
        else if (s==b || s>b ) cout<<"Case "<<cas<<": "<<":-|"<<endl ;
        else if (s<b) cout<<"Case "<<cas<<": "<<":-("<<endl ;

        cas++ ;
    }

    return 0 ;
}

UVA-12696 :: Cabin Baggage

//Problem link>>http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=4434


#include <bits/stdc++.h>
using namespace std ;
int main ()
{
    double l, w , h , d , wgt  ;
    int t ,cnt=0 ;
    cin>>t ;
    while (t--)
    {
        d=0.0 ;
        cin>>l>>w>>h>>wgt ;
        d=l+w+h ;

        if (l<=56.0 && w<=45.0 && h<=25.0)
        {
            if (d<=126.0 && wgt<=7.0)
            {
                cout<<"1"<<endl ;
                cnt++ ;
            }
            else cout<<"0"<<endl ;

        }

        else
        {
            if (d<=125.0 && wgt<=7.0)
            {
                cout<<"1"<<endl ;
                cnt++ ;
            }
            else cout<<"0"<<endl ;
        }

    }
cout<<cnt<<endl ;

    return  0 ;
}

UVA-12541 :: Birthdates

//Problem link>>http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3986


#include <bits/stdc++.h>
using namespace std ;
int main ()
{
    int n , i ,j , d ,m , y ,x ,a[150] ,pos[150];
    string s[150] ;
    cin>>n ;
    for (i=0 ; i<n ; i++)
    {
        cin>>s[i]>>d>>m>>y ;
        x=(y*365)+(m*30)+d ;
        a[i]=x ;
        pos[i]=i ;
    }

     for (i=0 ; i<n-1 ; i++)
     {
         for (j=i+1 ; j<n ; j++)
         {
             if (a[i]>a[j])
             {
                 swap(a[i],a[j]) ;
                swap(pos[j],pos[i]) ;
             }
         }
     }

        cout<<s[pos[n-1] ]<<endl<<s[pos[0] ]<<endl ;

    return 0 ;
}

UVA-11371 :: Number Theory for Newbies

//Problem link>>http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2366


#include <bits/stdc++.h>
#define ll long long int
using namespace std ;
int main ()
{
     ll  n  , i ,mn ,mx ,d, x ,op ,q ,v ;
    char a[1000000]  ;
    while (cin>>n)
    {
        sprintf (a,"%lld",n) ;
        ll  len=strlen(a) ;
        sort(a,a+len) ;

           mx=0 ;
        for (i=len-1 ; i>=0 ; i--)
        {
            mx=mx*10+(a[i]-'0') ;
        }

        op=0 , q ,v ;
        for (i=0 ; i<len ; i++)
        {
            if (op==1) break ;
            if (a[i]=='0' && op==0) continue ;
            else
            {
                op=1 ;
                q=i ;
                v=a[i] ;
            }
        }

        if (a[0]=='0')
        {
            a[0]=v ;
            a[q]='0' ;
        }

        mn=0 ;
        for (i=0 ; i<len ; i++)
        {
            mn=mn*10+(a[i]-'0') ;
        }

        d =mx-mn ;
        x=d/9 ;

    cout<<mx<<" - "<<mn<<" = "<<d<<" = "<<"9 * "<<x<<endl ;

    }

    return 0 ;
}

UVA-10127 :: Ones

//Problem link>>http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1068

#include <bits/stdc++.h>
using namespace std ;
int main ()
{
    long long  n , sum ,cnt ,i  ;
    while (cin>>n)
    {
        sum=0 ; cnt=0 ;
        for (i=0 ; ; i++)
        {
            sum=(sum*10+1)%n ;
            if (sum==0) break ;
            cnt++ ;
        }
        cout<<++cnt<<endl ;
    }

    return 0 ;
}

Monday, January 6, 2014

LOJ-1328 :: A Gift from the Setter

//Probelm link>>http://lightoj.com/volume_showproblem.php?problem=1328


#include <bits/stdc++.h>
#define ll long long int
#define mod 1000007
using namespace std ;

long long GetDiffSum(long long int a[], int n )
{
    long long int  sum=0 ,ans=0 ;
    int i, j;
    for (i=0 ; i<n ; i++) sum=sum+a[i] ;
    j=n-1 ;
    for (i=0 ; i<n ; i++)
    {
        sum=sum-a[i] ;
        ans=ans+ abs( (a[i]*j )-sum) ;
        j-- ;
    }
   return ans ;
}

int main ()
{
    ll k , c , n , t ,it=0 ,i , x  , a[100004];

    cin>>t ;
    while (it<t)
    {
        it++ ;
        memset(a,0,sizeof (a)) ;
        cin>>k>>c>>n>>a[0] ;

        for (i=1 ; i<n ; i++)
        {
            a[i]= ( (k*a[i-1] ) %mod + c) % mod ;

        }
         sort(a , a+n) ;
         x= GetDiffSum ( a , n ) ;
         cout<<"Case "<<it<<": "<<x<<endl ;

    }

    return 0 ;
}

LOJ-1088 :: Points in Segments

//Problem link>>http://lightoj.com/volume_showproblem.php?problem=1088


#include <bits/stdc++.h>
using namespace std ;
int main ()
{
    int t , it , i ,n ,q ,a[100005] ,x ,y ,low ,up ;

     scanf ("%d",&t) ;
    for (it=1 ; it<=t ; it++)
    {
       scanf ("%d %d",&n , &q) ;
        for (i=0 ; i<n ; i++) scanf ("%d",&a[i]) ;

       printf ("Case %d:\n",it) ;
        for (i=1 ; i<=q ; i++)
        {
            scanf ("%d %d",&x,&y) ;
            low=lower_bound(a, a+n ,x)-a ;
            up=upper_bound(a, a+n ,y)-a ;
            int ans=up-low ;

           printf ("%d\n",ans) ;

        }

    }

    return 0 ;
}

LOJ-1189 :: Sum of Factorials

//Problem link>>http://www.lightoj.com/volume_showproblem.php?problem=1189


#include <bits/stdc++.h>
#define mx 1000000000000000000

  long long int  a[100] , i ,fact=1 , n ;

using namespace std ;
int main ()
{
      map<long long int , long long int> mp ;

      int t , it=0 , y ;

    for (i=1 ; fact<=mx ; i++)
    {
        fact=fact*i ;
        a[i]=i ;
        mp[i]=fact ;
        y=i ;
    }
    mp[0]=1 ;
    a[0]=0 ;

    cin>>t ;
    while (it<t)
    {
        it++ ;
        cin>>n ;
        vector<long long int>v ;

        int p=0 , ny=y ;
        while (n)
        {
           int k =0 ;
            for (i=0 ; i<ny ; i++)
            {
                if (mp[i]>n)  break  ;
                k=i ;
            }
             v.push_back(k) ;
             ny=k ;
             n=n-mp[k] ;

             if (n>mp[k])
             {
                 p=1 ; break ;
             }
        }
        reverse (v.begin() , v.end()) ;

        cout<<"Case "<<it<<": " ;
       if (p==0)
       {
            for (i=0 ; i<v.size() ; i++)
            {
                if (i==0) printf ("%lld!",v[i]) ;
                else printf ("+%lld!",v[i]) ;
            }
            cout<<endl ;
       }

        else cout<<"impossible"<<endl ;

    }

    return 0 ;
}


Saturday, January 4, 2014

UVA-11389 :: The Bus Driver Problem

//Problem link>>http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2384

#include <bits/stdc++.h>
using namespace std ;
int main ()
{
    int n , d , r , i ,mor[105] , ev[105] ,x ,sum ;
    while (cin>>n>>d>>r  && n || d || r )
    {
        for (i=0 ; i<n ; i++) cin>>mor[i] ;
        for (i=0 ; i<n ; i++) cin>>ev[i] ;

        sort(mor , mor+n) ;
        sort(ev , ev+n) ;
        reverse (ev,ev+n) ;

         sum =0 ;
        for (i=0; i<n ; i++)
        {
            if (mor[i]+ev[i]>d) sum+= (mor[i]+ev[i]) - d ;
        }

        cout<<sum*r<<endl ;
    }

    return 0 ;
}

UVA-10041 :: Vito's Family

//Problem link>>http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=982

#include <bits/stdc++.h>

using namespace std ;
int main ()
{
    int t , i ,r , a[1000] ,mid ;
    cin>>t ;
    while (t--)
    {
        cin>>r ;
        for (i=0 ; i< r ; i++) cin>>a[i] ;
        sort (a , a+r) ;

        if (r%2==1) mid =a[r/2] ; //  as  array  starts  with 0
        else mid = a[r/2-1] ;

        int sum=0 ;
       for (i=0 ; i<r ; i++) sum=sum+ abs(mid-a[i]) ;
       cout<<sum<<endl ;

    }

    return 0 ;
}