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;
}