Tuesday, December 6, 2016

UVa 12555

#include<bits/stdc++.h>

using namespace std;

int main()
{
 int t,sz,a;
 int kase=1;
 double res;
 string inp;

 cin>>t;

 while(t--){
  
  res=0;
  cin>>a;
  cin>>inp;
  
  sz=inp.size();
  
  if(sz <= 4) res = a * .5;
  else res = (inp[3]-'0') * .05 + a * .5;
  
  cout<<"Case "<<kase<<": ";
  cout<<res<<endl;
  kase++;

 }

 return 0;

}

//translation chinese to english :D
//.5kg =  jin 
//.05 = two
// so "5 jin 2 two" consider it :)

Wednesday, November 16, 2016

UVa 10105

#include<bits/stdc++.h>

using namespace std;

int main()
{
 int n,k;
 int a[15];
 a[0]=1;

 for(int i=1; i<15; i++)
  a[i]=a[i-1]*i;

 while(cin>>n>>k)
 {
  int result=a[n];

  while(k--)
  {
   int x;
   cin>>x;
   result/=a[x];
  }

  cout<<result<<endl;
 }
 return 0;
}

UVa 494

#include<bits/stdc++.h>

using namespace std;

int main()
{
 string l,ll;

 while(getline(cin,l) && getline(cin,ll))
 {
  
  set<string>s1,s2;
  string num;
  istringstream str1(l.c_str());

  while(str1>>num)
  {
   s1.insert(num);
  }

  istringstream str2(ll.c_str());

  while(str2>>num)
  {
   s2.insert(num);
  }

  set<string>:: iterator it,ix;

  int ab=0;

  for(it=s1.begin(); it !=s1.end(); it++)
  {
   ix=s2.find(*it);
   if(ix!=s2.end()) ab++;
  }

  int ba=0;

  for(it=s2.begin(); it!=s2.end(); it++)
  {
   ix=s1.find(*it);
   if(ix!=s1.end()) ba++;
  }

   if(ab==0 && ba==0)  cout<<"A and B are disjoint"<<endl;
    else if(ab==ba && (ab==s1.size() && ba==s2.size()))  cout << "A equals B" << endl;
     else if(ab==s1.size() && (s1.size()<s2.size()))  cout << "A is a proper subset of B" << endl;
      else if(ba==s2.size() && (s2.size()<s1.size()))  cout << "B is a proper subset of A" << endl;
       else cout << "I'm confused!" << endl; 

 }

 return 0;
}

UVa 694

#include<bits/stdc++.h>
#define ll long long
#define kase(i) printf("Case %d: ",i)

using namespace std;

int main()
{
 ll a,b,c,i;
 int j=1;

 while(cin>>a>>b)
 {
  c=a;
  if(a<0 && b<0) break;

  for(i=1;;i++)
  {
   if(a==1) break;

   if(a%2==0) a=a/2;
   else a=3*a+1;

   if(a>b) break;
  }

  kase(j);
  cout<<"A = "<<c<<", Limit = "<<b<<", number of terms = "<<i<<endl;
  j++;

 }

 return 0;
}

Tuesday, November 8, 2016

UVa 11715

#include<bits/stdc++.h>

using namespace std;

int main() {
  int T;
  int c = 0;
  double u, v, t, a, s;

  while (cin >> T) {
    if (T == 0)
      break;

    c++;

    cout << "Case " << c << ": ";

    switch (T) {
      case 1:
        cin >> u >> v >> t;
        a = (v - u) / t;
        s = u * t + 0.5 * a * t * t;
        cout << setprecision(3) << fixed << s << " " << a << endl;
        break;
      case 2:
        cin >> u >> v >> a;
        t = (v - u) / a;
        s = u * t + 0.5 * a * t * t;
        cout << setprecision(3) << fixed  << s << " " << t << endl;
        break;
      case 3:
        cin >> u >> a >> s;
        t = (-u + sqrt(u * u + 2 * a * s)) / a;
        v = a * t + u;
        cout << setprecision(3) << fixed  << v << " " << t << endl;
        break;
      case 4:
        cin >> v >> a >> s;
        t = (-v + sqrt(v * v - 2 * a * s)) / -a;
        u = v - a * t;
        cout << setprecision(3) << fixed  << u << " " << t << endl;
        break;
    }
  }

  return 0;
}

Wednesday, November 2, 2016

UVa 1230

import java.util.Scanner;
import java.math.BigInteger;

class Main { 

  public static void main(String args[])
   {

    Scanner sc = new Scanner(System.in);
    int c = sc.nextInt();

    while (c-- > 0) {

         BigInteger x = BigInteger.valueOf(sc.nextInt()); 
         BigInteger y = BigInteger.valueOf(sc.nextInt()); 
         BigInteger n = BigInteger.valueOf(sc.nextInt()); 
         System.out.println(x.modPow(y, n)); 
   }
  } 
}

UVa 343

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

class Main{

  void sol()
  {
   Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));

    while(in.hasNext())
    {
      String a=in.next();
      String b=in.next();
      BigInteger b1,b2;
      boolean f=false;

       for(int i=2; i<=36; i++)
       {
        try{
         b1=new BigInteger(a,i);
         }

         catch(Exception e)
         {
          continue;
          }

          for(int j=2; j<=36; j++)
          {
           try{
            b2= new BigInteger(b,j);
           }
           catch(Exception e)
           {
            continue;
           }

           if(b1.compareTo(b2)==0){
            f=true;
            System.out.println(a + " (base " + i + ") = " + b + " (base " + j + ")");
            break;

           }

          }

          if(f) break;
        }
         if(!f) System.out.println(a + " is not equal to " + b + " in any base 2..36");


     }


   }


   public static void main(String args[])
   {
    Main uva343 = new Main();
    uva343.sol();
   }


 }

UVa 10302

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{ ll a[50005];
 a[1]=1;
 for(ll i=2; i<=50005; i++)
 {
  a[i]=a[i-1]+(i*i*i);
 }

 ll n;
 while(cin>>n)
 {
  cout<<a[n]<<endl;
 }
 return 0;
}

UVa 392

#include<bits/stdc++.h>
#define M 9

using namespace std;

int main()
{
 int a[M],fr,i;

    while(cin>>a[0])
    {
        for(i=1;i<M;i++)
            cin>>a[i];

        fr=8;
        for(i=0;i<M;i++)
            if(a[i]!=0)
                {
                    fr=i;
                    break;
                }

        if(fr==8)
            cout<<a[fr];
        else
        {
            if(a[fr]==-1)
                cout<<"-";
            else if(a[fr]==1)
                cout<<"";
            else
                cout<<a[fr];
            cout<<"x";

            if(fr!=7)
                cout<<"^"<<8-fr;

            for(i=fr+1;i<M;i++)
                if(a[i]!=0)
                    {
                        if(a[i]<0)
                            cout<<" - ";
                        else
                            cout<<" + ";

                        if((a[i]!=-1 && a[i]!=1) || i==8)
                            {

                                if(a[i]<0)
                                    cout<<a[i]*(-1);
                                else
                                    cout<<a[i];
                            }

                        if(i!=8)
                            cout<<"x";
                        if(i!=7 && i!=8)
                            cout<<"^"<<8-i;
                    }
        }
        cout<<endl;

}
 return 0;
}

Wednesday, October 26, 2016

UVa 913

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{

 long long N,x;

 while(cin>>N)
 {
  x = 2*(((N/2)+1)*((N/2)+1)) -3;
  cout<<3*x<<endl;
 }

 return 0;
}

Monday, October 24, 2016

UVa 10499

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{

 ll n,p;

 while(cin>>n)
 {
  if(n<0) break;
  p=(n*100)/4;
  if(n==1) cout<<0<<"%"<<endl;
  else cout<<p<<"%"<<endl;
 }

 return 0;
}

UVa 10170

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
 ll s,d,n,i;

 while(cin>>s>>d)
 {
  n=0;
  for(i=s;;i++){

   n+=i;
   if(n>d || n==d) {
    cout<<i<<endl;
    break;
   }
  }
 }
 return 0;
}

UVa 12027

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
 
 string a;
 while(cin>>a)
 {
  if(a=="0") break;
  int l,num;

  l=a.length();
  num=a[0]-'0';
  if(l>1 && l%2==0) num = num*10 + a[1]-'0';

  l = (l-1)/2;
  cout<<int(sqrt(num));

  for(int i=0; i<l; i++){
   cout<<0;
  }
  cout<<endl;
  
 }

 return 0;

}

Thursday, October 13, 2016

UVa 371 - Ackermann Functions

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
 ll x, y;
 
 int j,k;
 
 while(cin>>x>>y)
 {
   if(x==0 || y==0) break;
   
   vector<ll>a;
   vector<ll>b;
   vector<ll>c;
   
  j=k=0;
  if(x>y) swap(x,y);
  
  for(ll i=x; i<=y; i++ )
  {
   
   ll ti=0;
   ll tn=i;
   
   if(i==1)
   {
    ti=3;
   }
   
   else {
    while(tn!=1)
   {
    if(tn%2==0){
      tn= tn/2;
      ti++;
    }
    else {
     tn=3*tn+1;
     ti++;
    }
   }
   }
   
   a.push_back(i);
   b.push_back(ti);
   c.push_back(ti);
   
  }
   sort(b.begin(),b.end());
  
   for(ll l=0; l<c.size(); l++)
   {
    if(b[b.size()-1]==c[l]) {
     
     cout<<"Between "<<x<<" and "<<y<<", "<<a[l];
     cout<<" generates the longest sequence of "<<b[b.size()-1]<<" values."<<endl;
     break;
    }
   }
 
 }
 return 0;
}

UVa 12149 - Feynman

#include<bits/stdc++.h>
#define ll long long

using namespace std;

ll fynman(ll n)
{
 if(n==0) return 0;
 else return n*n + fynman(n-1);
}

int main()
{
 ll n;
 
 while(cin>>n && n)
 {
  ll f=fynman(n);
  cout<<f<<endl;
 }
 
 return 0;
}

UVa 10469 - To Carry or not to Carry

#include<bits/stdc++.h>

using namespace std;

int  main()
{
 int x,y;
 
 while(cin>>x>>y)
 {
  int c=x^y;
  cout<<c<<endl;
 }
 
 return 0;
}

Tuesday, October 4, 2016

CF 723A

#include<bits/stdc++.h>

using namespace std;

int main()
{
 int x,xx,xxx;
 cin>>x>>xx>>xxx;
 
 int result = (max(x,max(xx,xxx))) -( min(x,min(xx,xxx)));
 
 cout<<result<<endl;
 
 return 0;
}

Sunday, October 2, 2016

Link List - Insertion and Print inserted data

#include<bits/stdc++.h>  

using namespace std;

struct node
 {
  int data;
  int key;
  struct node *next;
 };

struct node *head = NULL;
struct node *current = NULL;

void InsertFirst(int key,int data)
 {
  struct node *link = (struct node*) malloc(sizeof(struct node));
  link->key=key;
  link->data=data;
 
  link->next = head;
  head = link;
 
 } 

void printList()
 { 

  struct node *ptr=head;
 
  while(ptr != NULL)
  {
   cout<<"("<<ptr->key<<","<<ptr->data<<")";
   ptr=ptr->next;
  }
   cout<<endl;
 } 


int main()
{
 int data,key;
 for(int i=0; i<4; i++)
 {
  cin>>data>>key;
  
  InsertFirst(data,key);
 }
 
 printList();
 
 return 0;  
}

Saturday, September 3, 2016

UVa - 10892 LCM Cardinality

#include<bits/stdc++.h>
#include<vector>
#include<cmath>
#define ll long long

using namespace std;

ll gcd(ll a,ll b)
{
 return b? gcd(b,a%b):a; 
}

ll lcm(ll a,ll b)
{
 return (a*b/gcd(a,b));
}

int main()
{
 ll n,sq,m,count;
 


 while(cin>>n)
 {
  vector<ll>fact;
  if(n==0) break;
  sq = sqrt(n);
  
  for(ll i=1; i<=sq; i++)
  {
   if(n%i==0)
   {
    fact.push_back(i);
    fact.push_back(n/i);
   }
  }
  
  if(sq*sq==n) fact.pop_back();
  
  count=0;
  ll vsize = fact.size();
  
  for(ll i=0; i<vsize; i++)
  {
   for(ll j=i; j<vsize; j++)
   {
    if(lcm(fact[i],fact[j])==n) count++;
   }
  }
  
  cout<<n<<" "<<count<<endl;

  
 }
}

Monday, August 29, 2016

UVa 10235 - Simply Emirp

#include<bits/stdc++.h>
#define ll long long

using namespace std;

bool isPrime(ll n)
{
 ll d= sqrt(n);
 for(ll i=2; i<=d; i++)
 {
  if(n%i==0) return false;
 }
 return true;
}
int main()
{
 ll n;
 bool b,c;

 while(cin>>n)
 {
   b=false;
   c=false;
 
  b=isPrime(n);
  
  if(b)
  {
   ll rn =0;
            for(ll i =n;i!=0;i=i/10){
                rn = rn*10+i%10;
    }
    
   c= isPrime(rn);
   
   if(c && rn!=n) cout<<n<<" is emirp."<<endl;
   else cout<<n<<" is prime."<<endl;
  }
  
  else cout<<n<<" is not prime."<<endl;
 }
}

UVa 10219 - Find the ways !

#include<bits/stdc++.h>
#include<cmath>
#define ll long long

using namespace std;

int main()
{
  unsigned ll n,k;
  long double sum;
  
 while(cin>>n>>k)
 {
  sum=0;
  
  for( unsigned ll i=n-k+1; i<=n; i++)
  {
   sum+=log10((double)i);
  }
  
   for( unsigned ll j=1; j<=k; j++)
   {
    sum-=log10((double)j);
   }
   
   cout<<(ll)sum+1<<endl;
  } 
 return 0;
}

Saturday, August 27, 2016

UVa 443 Humble Numbers

#include<bits/stdc++.h>
#include<algorithm>
#define ll long long

using namespace std;


int main()
{
 ll a[5842+1];
 ll n;
 ll  two,three,five,seven;
 memset(a,0,sizeof(a));
 
 two=three=five=seven=0;
 a[0]=1;
 for(ll i=1; i<=5842+1; i++)
 {
  a[i]=min(min(2*a[two],3*a[three]),min(5*a[five],7*a[seven]));
  
  if(a[i]==2*a[two]) two++;
  if(a[i]==3*a[three]) three++;
  if(a[i]==5*a[five]) five++;
  if(a[i]==7*a[seven]) seven++;
 }
 
 while(cin>>n && n)
 {
  cout<<"The "<<n;
  if(n%100>=10 && n%100<=19) cout<<"th humble number is ";
  else if(n%10==1) cout<<"st humble number is ";
  else if(n%10==2) cout<<"nd humble number is ";
  else if(n%10==3) cout<<"rd humble number is ";
  else cout<<"th humble number is ";
  
  cout<<a[n-1]<<"."<<endl;
 }
}

UVa 10346 - Peter's Smokes

#include<bits/stdc++.h>

using namespace std;

int main()
{
 int n,k,temp,sum;
 
 while(cin>>n>>k)
 {
  temp=n;
  while(n>=k)
  {
   temp=temp+(n/k);
   n=n/k + (n%k);
  }
  cout<<temp<<endl;
 }
}

UVa 1225 - Digit Counting

#include<bits/stdc++.h>

using namespace std;

int main()
{
 
 int n,i,a[10],digit,t;

 
 while(cin>>t)
 {
  while(t--)
 {
   memset(a,0,sizeof(a));
   cin>>n;
   for(int j=1; j<=n; j++)
   {
    int temp=j;
   
  while(temp)
  {
   digit=temp%10;
   a[digit]++;
   temp=temp/10;
  }
  
  
 }
 
 for(i=0; i<10; i++)
  {
   if(i<9)
   cout<<a[i]<<" ";
   else cout<<a[i]<<endl;
  }
  
 }

}
 return 0;
}

UVa 12024 - Hats

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
 ll a[13],b[13];
 int i,t,n;
 
 a[2]=1;
 a[3]=2;
 
 for(i=4; i<=12; i++)
 {
  a[i] = (i-1) * (a[i-1]+a[i-2]);
 }
 
 b[0]=1;
 
 for(i=1; i<=12; i++)
 {
  b[i]=i* b[i-1];
 }
 
 while(cin>>t)
 {
  while(t--)
  {
   cin>>n;
   cout<<a[n]<<"/"<<b[n]<<endl;
  }
 }
}

UVa 12461 - Airplane

#include<bits/stdc++.h>

using namespace std;

int main()
{
 int n;
 while(cin>>n)
 {
  if(n==0) break;
  cout<<"1/2"<<endl;
 }
 return 0;
}

900 - Brick Wall Patterns

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
 ll a[100],i,n;

 
 while(cin>>n)
 {
  if(n==0) break;
  memset(a,0,sizeof(a));
    a[1]=1;
  a[2]=2;
  for(i=3; i<=n; i++)
  {
   a[i]= a[i-1]+a[i-2];
  }
  
  cout<<a[n]<<endl;
 }
}

UVa 10935 Throwing cards away I

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
 queue<ll>card;
 ll n,temp;
 
 while(cin>>n)
 {
  if(n==0) break;
  
  for(ll i=1;  i<=n; i++)
  {
   card.push(i);
  }
  cout<<"Discarded cards:";
  while(card.size() > 1)
  {
   cout<<" "<<card.front();
   card.pop();
   temp = card.front();
   card.pop();
   if(!card.empty())
   {
    cout<<",";
   }
   card.push(temp);
   
  }
  cout<<endl<<"Remaining card: "<<card.front()<<endl;
  card.pop();
 }
 
 return 0;
}

Wednesday, August 24, 2016

UVa 900 Brick Wall Patterns

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
 ll a[100],i,n;

 
 while(cin>>n)
 {
  if(n==0) break;
  memset(a,0,sizeof(a));
    a[1]=1;
  a[2]=2;
  for(i=3; i<=n; i++)
  {
   a[i]= a[i-1]+a[i-2];
  }
  
  cout<<a[n]<<endl;
 }
}

UVa 369 Combinations

#include<bits/stdc++.h>
#define ll long long

using namespace std;

ll combi(ll n,ll m)
{
 ll r,i;
 r=1;
 if(n-m<m) m=n-m;
 
 for(i=1; i<m+1; i++,n--)
 {
  r=r*n;
  r=r/i;
 }
 
 return r;
}

int main()
{
 ll m,n;
 ll c;
 
 while(cin>>n>>m)
 {
  if(n==0 && m==0) break;
  c=combi(n,m);
  cout<<n<<" things taken "<<m<<" at a time is "<<c<<" exactly."<<endl;
 }
 
 return 0;
}

Monday, August 15, 2016

UVa 686 - Goldbach's Conjecture (II)

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

bool isPrime(ll n)
{
 for(ll i=3; i<=(ll)sqrt(n); i++)
 {
  if(n%i==0) return false;
 }
 
 return true;
}

int main()
{
 ll n,fp,lp,a,ans,i,j;
 bool fip,sep;
 
while(cin>>n)
{
 if(n==0) break;
 ans=0;
 for(i=3; ; i+=2)
 {
  fp=i;
  lp=n-i;
  fip=true;
  sep=true;
  j=i;
  
  if(fp>lp) break;
  if(fp%2!=0)
  {
   fip=isPrime(fp);
   
   if(fip)
   {
    if(lp%2!=0)
    {
     sep=isPrime(lp);
     if(sep)
     {
      ans++;
     }
    }
   }
  }
 }

 
 if(n==4) cout<<1<<endl;
 else cout<<ans<<endl;
}
 return 0;
}

Sunday, July 31, 2016

UVa 991 - Safe Salutations

#include<iostream>
using namespace std;


int catalan(int n)
{

 if (n <= 1) return 1;

 int res = 0;
 for (int i=0; i<n; i++)
  res += catalan(i)*catalan(n-i-1);

 return res;
}

int main()
{
 int n;
 bool b=false;
 while(cin>>n)
{
 if(b) cout<<endl;
 b=true;
  cout<<catalan(n)<<"\n";
}
 
 return 0;
}

Saturday, July 23, 2016

UVa 11827 - Maximum GCD

#include<bits/stdc++.h>

using namespace std;

int gcd(int a, int b)
{
 
 if(a%b==0) return b;
 return gcd(b,a%b);
}

int main()
{
 
 int tc,N,a[99],ans;
 string s;
 
 while(cin>>tc)
 {
  getchar();
 while(tc--){
  
  N=0;
  ans=0;
  
  getline(cin,s);
  istringstream is(s);
 
  
  while(is>>a[N]){
    ++N;
  }
  
  for(int i = 0;i<N;++i)
   for(int j = i+1;j<N;++j)
    ans = max(ans,gcd(a[i],a[j]));
  
  cout<<ans<<endl;
 }
 }
 
 
 
 return 0;
}

istringstream Example in C++

#include<bits/stdc++.h>

using namespace std;

int main()
{
 string s;
 getline(cin,s);
 int a[10],n,i;
 n=0;
 
 istringstream p(s);
 
 while(p>>a[n]) n++;
 
 for(i=0; i<n; i++)
 {
  cout<<a[i]+5<<" ";
 }
 cout<<endl;
}