Computer Programming Contest Preparation

ToolBox - Source for: 114/11417/a.c



/home/toolbox/public_html/solutions/114/11417/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2022-10-04
   19  * Purpose: fun
   20  * Problem: 11417
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define TOP 501
   28 
   29 int ans[TOP+1];
   30 int n;
   31 
   32 int gcd(int a, int b)
   33 {
   34     /* FUNCTION gcd */
   35     /* this uses Euclid's method */
   36     int toReturn;
   37 
   38     if (0 == b)
   39         toReturn = a;
   40     else
   41         toReturn = gcd(b, (a % b));
   42     return toReturn;
   43 } /* FUNCTION gcd */
   44 
   45 void init()
   46 {
   47     /* FUNCTION init */
   48     int i;
   49     int j;
   50     int tmp;
   51 
   52     ans[0] = 0;
   53     ans[1] = 0;
   54     ans[2] = 1;
   55     for (j=3; TOP>j; j++)
   56         {
   57             /* calculate all 500 of them */
   58             tmp = ans[j-1];
   59             for (i=1; j>i; i++)
   60                 {
   61                     /* for i */
   62                     tmp = tmp + gcd(j, i);
   63                 } /* for i */
   64             DEBUG printf("%d: %d\n", j, tmp);
   65             ans[j] = tmp;
   66         } /* calculate all 500 of them */
   67 } /* FUNCTION init */
   68 
   69 void dump()
   70 {
   71     /* FUNCTION dump */
   72 } /* FUNCTION dump */
   73 
   74 int getInput()
   75 {
   76     /* FUNCTION getInput */
   77     int dataReadFlag;
   78 
   79     scanf(" %d ", &n);
   80     dataReadFlag = 0 != n;
   81     return (dataReadFlag);
   82 } /* FUNCTION getInput */
   83 
   84 void process()
   85 {
   86     /* FUNCTION process */
   87     printf("%d\n", ans[n]);
   88 } /* FUNCTION process */
   89 
   90 int main()
   91 {
   92     /* main */
   93     int moreToDo;
   94 
   95     init();
   96     moreToDo = getInput();
   97     while (moreToDo)
   98         {
   99             /* while */
  100             process();
  101             moreToDo = getInput();
  102         } /* while */
  103 
  104     return EXIT_SUCCESS;
  105 } /* main */
  106