Computer Programming Contest Preparation

ToolBox - Source for: 3/371/a.c



/home/toolbox/public_html/solutions/3/371/a.c
    1 
    2 /*
    3  *  Author: Isaac Traxler
    4  *    Date: 2021-02-04
    5  * Purpose: fun
    6  * Problem: 371 - Ackermann Functions
    7  */
    8 
    9 #include <stdio.h>
   10 #include <string.h>
   11 #include <sys/types.h>
   12 #include <sys/stat.h>
   13 #include <fcntl.h>
   14 #include <stdint.h>
   15 #include <math.h>
   16 #include <stdlib.h>
   17 
   18 #define DEBUG if (1 == 0)
   19 
   20 #define TRUE  (1 == 1)
   21 #define FALSE (1 != 1)
   22 #define MININT 0
   23 #define MAXSIZE 4000000
   24 #define ulong unsigned long int
   25 
   26 ulong ary[MAXSIZE+5] = {0};
   27 ulong best;
   28 ulong start;
   29 ulong stop;
   30 
   31 void dump()
   32 {
   33     /* BEGIN FUNCTION dump */
   34     ulong i;
   35 
   36     printf("\n");
   37     for (i=1; i < MAXSIZE; i++)
   38         {
   39             /* for */
   40             if (0 != ary[i])
   41                 {
   42                     /* if */
   43                     printf("ary[%lu] = (%lu)\n", i, ary[i]);
   44                 } /* if */
   45         } /* for */
   46 } /* BEGIN FUNCTION dump */
   47 
   48 int getInput()
   49 {
   50     /* BEGIN FUNCTION getInput */
   51     int moreToDo;
   52     ulong tmp;
   53 
   54     scanf(" %lu %lu ", &start, &stop);
   55     moreToDo = ((0 < start) && (0 < stop));
   56     if (moreToDo)
   57         {
   58             /* then */
   59             if (start > stop)
   60                 {
   61                     /* then */
   62                     tmp = start;
   63                     start = stop;
   64                     stop = tmp;
   65                 } /* then */
   66         } /* then */
   67     return moreToDo;
   68 } /* END FUNCTION getInput */
   69 
   70 ulong calcOne(ulong in)
   71 {
   72     /* BEGIN FUNCTION calcOne */
   73     ulong retval = 1;
   74 
   75     if (1 != in)
   76         {
   77             /* then */
   78             /* retval = (0 == (in % 2)) ? in / 2 : 3 * in + 1 ; */
   79             retval = (0 == (in & 1)) ? in / 2 : 3 * in + 1 ;
   80         } /* then */
   81     return retval;
   82 } /* END FUNCTION calcOne */
   83 
   84 ulong calc(ulong in)
   85 {
   86     /* BEGIN FUNCTION calc */
   87     ulong retval;
   88     ulong cnt = 1;
   89     ulong tmp;
   90     ulong t1;
   91     ulong xx = 0;
   92 
   93     retval = in;
   94     while (1 != retval)
   95         {
   96             /* while */
   97             if (MAXSIZE > retval)
   98                 {
   99                     /* then */
  100                     if (0 != ary[retval])
  101                         {
  102                             /* then */
  103                             cnt = cnt + ary[retval] - 1;
  104                         } /* then */
  105                     else
  106                         {
  107                             /* else */
  108                             tmp = calcOne(retval);
  109                             t1 = calc(tmp);
  110                             cnt = cnt + t1;
  111                             ary[retval] = cnt - xx;
  112                         } /* else */
  113                     retval = 1;
  114                 } /* then */
  115             else
  116                 {
  117                     /* else */
  118                     retval = calcOne(retval);
  119                     cnt++;
  120                     xx++;
  121                 } /* else */
  122         } /* while */
  123     return (cnt);
  124 } /* BEGIN FUNCTION calc */
  125 
  126 ulong process()
  127 {
  128     /* BEGIN FUNCTION process */
  129     ulong i;
  130     ulong max;
  131     ulong tmp;
  132 
  133     max = MININT;
  134     /* following hack needed because 1 is defined to be 3, if you start at 1 */
  135     if (1 == start)
  136         {
  137             /* range includes 1 */
  138             max = 3;
  139             best = 1;
  140         } /* range includes 1 */
  141     for (i=start; i <= stop; i++)
  142         {
  143             /* for */
  144             tmp = calc(i) - 1;  /* all of my counts are 1 to high */
  145             if (max < tmp)
  146                 {
  147                     /* better guess */
  148                     best = i;
  149                     max = tmp;
  150                 } /* better guess */
  151         } /* for */
  152 
  153     return max;
  154 } /* END FUNCTION process */
  155 
  156 int main ()
  157 {
  158     /* main */
  159     int moreToDo;  /* tell me when to stop */
  160     ulong ans;       /* calculated answer */
  161 
  162     ary[1] = 1;
  163     moreToDo = getInput();
  164     while (moreToDo)
  165         {
  166             /* while */
  167             ans = process();
  168             printf("Between %lu and %lu, %lu generates the longest sequence of %lu values.\n", start, stop, best, ans);
  169             moreToDo = getInput();
  170         } /* while */
  171     return EXIT_SUCCESS;
  172 } /* main */
  173 
  174