jueves, 2 de junio de 2011

Bytelandian gold coins

 In Byteland they have a very strange monetary system.

Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit).

You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins.

You have one gold coin. What is the maximum amount of American dollars you can get for it?
Input

The input will contain several test cases (not more than 10). Each testcase is a single line with a number n, 0 <= n <= 1 000 000 000. It is the number written on your coin.
Output

For each test case output a single line, containing the maximum amount of American dollars you can make. 

Example
Input:
12
2

Output:
13
2

You can change 12 into 6, 4 and 3, and then change these into $6+$4+$3 = $13. If you try changing the coin 2 into 3 smaller coins, you will get 1, 0 and 0, and later you can get no more than $1 out of them. It is better just to change the 2 coin directly into $2.



https://www.spoj.pl/problems/COINS/



import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Bytelandiangoldcoins 
{
   static long[][] tablaHash = new long[ 5003 ][ 2 ];
 
   public static void main( String args[] ) throws Exception
   {  
      BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
  
      String cadena = br.readLine();
      int num;
  
      while( cadena != null )
      {
         num = Integer.parseInt( cadena );
         System.out.println( maximo( num ) );  
         cadena = br.readLine();
      }
   } 
   /**
   * El algoritmo basicamente se basa en los principios de
   * progración dinámica y Divide y venceras.
   *
   * Definimos F( n ) como la cantidad maxima de coins que 
   * se pueden conseguir a partir de n coins.
   * Luego: F( n ) = F( n/2 ) + F( n/3 ) + F( n/4 )
   *
   * El caso base sera cuando n < n/2 + n/3 + n/4, ya que en
   * este caso el maximo valor de coins que se puede conseguir
   * es simplemente n.
   *
   * Almacenamos los valores previamente calculados en un tabla hash.
   *
   * No se puede guardar los valores calculados de la siguiente forma:
   * vector[ n ] = F( n ), ya que algunos valores de n son muy grandes, 
   * lo cual origina que sea imposible crear un vector de tal tamanio.
   */
   public static long maximo( long n )
   {
      long cantCoins = buscar( n );
  
      if( cantCoins != -1 )
         return cantCoins;
  
      else
      {
         long a = n/2, b = n/3, c = n/4;
         cantCoins = a + b + c; 
   
         if( cantCoins > n )
         {
            cantCoins = maximo( c ) + maximo( b ) + maximo( a );
            insertar( n, cantCoins );
            return cantCoins;
         }
         else
         {
            insertar( n, n );
            return n;
         }
      }
   }
 
   /*
   * Tabla hash de exploracion lineal
   * Cada elemento de la tabla es un vector de tamanio 2
   * donde la primera celda es para almacenar n y la segunda
   * celda almacena F( n ):
   * vector[ posicionHash ][ 0 ] = n
   * vector[ posicionHash ][ 1 ] = F( n )
   */
   public static long buscar( long n )
   {
      int pos =(int)( n % 5003 );
      int inicio = pos;
  
      if( tablaHash[ pos ][ 0 ] == 0 )
         return -1;
  
      if( tablaHash[ pos ][ 0 ] == n )
         return tablaHash[ pos ][ 1 ];
  
      else
      {
         pos = pos + 1;
   
         if( pos == 5003 )
            pos = 0;
   
         while( tablaHash[ pos ][ 0 ] != 0 && pos != inicio )
         {
            if( tablaHash[ pos ][ 0 ] == n )
               return tablaHash[ pos ][ 1 ];
    
            pos = pos + 1;
      
            if( pos == 5003 )
               pos = 0;
         }
      } 
      return -1;
   }
 
   public static void insertar( long n, long val )
   {
      int pos =(int)( n % 5003 );
  
      while( tablaHash[ pos ][ 0 ] != 0 )  
      {
         pos = pos + 1;
   
         if( pos == 5003 )
            pos = 0;
      }

      tablaHash[ pos ][ 0 ] = n;
      tablaHash[ pos ][ 1 ] = val;
   }
}

No hay comentarios:

Publicar un comentario