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